Bulk批量操作是将文档的增删改查一系列操作,通过一次请求全都做完,减少网络传输次数。
具体的语法格式,如下图:
批量操作代码,如下图:
执行批量操作之后的结果,如下图:
执行一下Java API操作bulk的代码,如下图:
/**
* 1.批量操作 bulk
* */
@Test
public void testBulk() throws IOException {
//创建bulkrequest对象,整合所有操作
BulkRequest bulkRequest = new BulkRequest();
/**
* 1. 删除1号记录
* 2. 添加6号记录
* 3. 修改3号记录为小王五
* */
DeleteRequest deleteRequest = new DeleteRequest("person", "1");
bulkRequest.add(deleteRequest);
HashMap<String, Object> map = new HashMap<>();
map.put("name","小六子");
map.put("address","奇葩星球");
IndexRequest indexRequest = new IndexRequest("person").id("6").source(map);
bulkRequest.add(indexRequest);
HashMap<String, Object> map2 = new HashMap<>();
map2.put("name","小王五");
UpdateRequest updateRequest = new UpdateRequest("person", "3").doc(map2);
bulkRequest.add(updateRequest);
BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT);
RestStatus status = response.status();
System.out.println(status);
}
检验ES搜索引擎中的结果,如下图:
发现使用Java API进行bulk批量操作成功。
需求:将数据库中Goods表的数据导入到ElasticSearch中
案例:实现步骤
1.创建goods索引
2.查询Goods表数据
3.批量添加到ElasticSearch中
首先来看一下mysql中的goods表,如下图:
PUT goods
{
"mappings": {
"properties": {
"title": {
"type": "text",
"analyzer": "ik_smart"
},
"price": {
"type": "double"
},
"createTime": {
"type": "date"
},
"categoryName": {
"type": "keyword"
},
"brandName": {
"type": "keyword"
},
"spec": {
"type": "object"
},
"saleNum": {
"type": "integer"
},
"stock": {
"type": "integer"
}
}
}
}
title:商品标题
price:商品价格
createTime:创建时间
categoryName:分类名称。如:家电,手机
brandName:品牌名称。如:华为,小米
sprc:商品规格。如:spec:{“屏幕尺寸”,“5寸”,“内存大小”,“128G”}
saleNum:销量
stock:库存量
POST goods/_doc/1
{
"title":"小米手机",
"price":1000,
"createTime":"2019-12-01",
"categoryName":"手机",
"brandName":"小米",
"saleNum":3000,
"stock":10000,
"spec":{
"网络制式":"移动4G",
"屏幕尺寸":"4.5"
}
}
查看es中的结果,如下图:
matchAll查询:查询所有文档
语法:
GET 索引名称/_search
{
"query": {
"match_all": {}
}
}
注意:默认情况下,es一次展示10条数据
那如果我们想要让es显示多条数据该怎么办呢?我们可以加上两个参数,from和size,如下图:
@Test
public void testMatchAll() throws IOException {
//1.查询
SearchRequest searchRequest = new SearchRequest("goods");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
QueryBuilder query = QueryBuilders.matchAllQuery();
sourceBuilder.query(query);
searchRequest.source(sourceBuilder);
//添加分页信息,从第0条数据开始查,查询100条数据
sourceBuilder.from(0);
sourceBuilder.size(100);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = searchResponse.getHits();
//获取总记录数
long value = hits.getTotalHits().value;
System.out.println("总记录数:"+value);
List<Goods> goodsList=new ArrayList<>();
//获取Hits查询到的数据 数组
SearchHit[] hits2 = hits.getHits();
for (SearchHit hit : hits2){
//获取json字符串格式的数据
String sourceAsString = hit.getSourceAsString();
//转为java对象
Goods goods = JSON.parseObject(sourceAsString, Goods.class);
goodsList.add(goods);
}
}
@Test
public void testTermQuery() throws IOException {
SearchRequest searchRequest = new SearchRequest("goods");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//term词条查询
QueryBuilder query=QueryBuilders.termQuery("categoryName","机");
searchSourceBuilder.query(query);
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(searchResponse.getHits().getTotalHits().value);
}
match查询:
会对查询关键字进行分词
然后将分词后的查询条件和词条进行等值匹配
默认取并集
语法:
GET 索引名称/_search
{
"query": {
"match": {
"字段名称": "查询条件"
}
}
}
@Test
public void testMatchQuery() throws IOException {
SearchRequest searchRequest = new SearchRequest("goods");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//match匹配查询
QueryBuilder query=QueryBuilders.matchQuery("brandName","联系起来");
searchSourceBuilder.query(query);
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(searchResponse.getHits().getTotalHits().value);
}
wildcard查询:会对查询条件进行分词。还可以使用通配符?(任意单个字符)和*(0个或多个字符)
@Test
public void testWildcardQuery() throws IOException {
SearchRequest searchRequest = new SearchRequest("goods");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//match匹配查询
QueryBuilder query=QueryBuilders.wildcardQuery("brandName","联系*");
searchSourceBuilder.query(query);
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(searchResponse.getHits().getTotalHits().value);
}
@Test
public void testRangeQuery() throws IOException {
SearchRequest searchRequest = new SearchRequest("goods");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//范围查询
RangeQueryBuilder query=QueryBuilders.rangeQuery("price");
//指定下限
query.gte(3000);
//指定上限
query.lte(4000);
searchSourceBuilder.query(query);
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(searchResponse.getHits().getTotalHits().value);
}
语法格式:
"sort": {
"排序字段": {
"order": "desc/asc"
}
}
概念:queryString查询会对查询关键字进行分词,并且可以一次性查询多个字段,最后把查询到的文档合并。
queryString:
会对查询条件进行分词。
然后将分词后的查询条件和词条进行等值匹配。
默认取并集(OR)
可以指定多个查询字段
GET 索引名称/_search
{
"query": {
"query_string": {
"fields": ["字段1","字段2"...],
"query": "查询条件1 OR 查询条件2"
}
}
}
@Test
public void testQueryStringQuery() throws IOException {
SearchRequest searchRequest = new SearchRequest("goods");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//queryString
QueryStringQueryBuilder query = QueryBuilders.queryStringQuery("华为手机").field("title")
.field("categoryName").field("brandName");
searchSourceBuilder.query(query);
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(searchResponse.getHits().getTotalHits().value);
}
如果我们查询的时候,一次有多个查询条件的话,那么我们可以使用布尔查询。
boolQuery:布尔查询,可以对多个查询条件进行连接。
must(and):条件必须成立
must_not(not):条件必须不成立
should(or):条件可以成立
filter:条件必须成立,性能比must高。不会计算得分。
语法:
GET 索引名称/_search
{
"query": {
"bool": {
"must": [{},{}...],
"filter": [{},{}...],
"must_not": [{},{}...],
"should": [{},{}...]
}
}
}