当前位置:   article > 正文

ElasticSearch高级操作_searchsourcebuilder

searchsourcebuilder

一、批量操作

1、脚本形式

例:
#删除5号
#新增8号
#更新2号 name为2号

  1. POST _bulk
  2. {"delete":{"_index":"person1","_id":"5"}}
  3. {"create":{"_index":"person1","_id":"8"}}
  4. {"name":"八号","age":18,"address":"北京"}
  5. {"update":{"_index":"person1","_id":"2"}}
  6. {"doc":{"name":"2号"}}

2、java操作

  1. //创建bulkrequest对象,整合所有操作
  2. BulkRequest bulkRequest =new BulkRequest();
  3. /*
  4. # 1. 删除5号记录
  5. # 2. 添加6号记录
  6. # 3. 修改3号记录 名称为 “三号”
  7. */
  8. //1. 删除5号记录
  9. DeleteRequest deleteRequest=new DeleteRequest("person1","5");
  10. bulkRequest.add(deleteRequest);
  11. //2. 添加6号记录
  12. Map<String, Object> map=new HashMap<>();
  13. map.put("name","六号");
  14. IndexRequest indexRequest=new IndexRequest("person1").id("6").source(map);
  15. bulkRequest.add(indexRequest);
  16. //3. 修改3号记录 名称为 “三号”
  17. Map<String, Object> mapUpdate=new HashMap<>();
  18. mapUpdate.put("name","三号");
  19. UpdateRequest updateRequest=new UpdateRequest("person1","3").doc(mapUpdate);
  20. bulkRequest.add(updateRequest);
  21. //执行批量操作
  22. BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT);
  23. System.out.println(response.status());

 3、从数据库写入数据

  1. //1.查询所有数据,mysql
  2. List<Goods> goodsList = goodsMapper.findAll();
  3. //2.bulk导入
  4. BulkRequest bulkRequest=new BulkRequest();
  5. //2.1 循环goodsList,创建IndexRequest添加数据
  6. for (Goods goods : goodsList) {
  7. //2.2 设置spec规格信息 Map的数据 specStr:{}
  8. String specStr = goods.getSpecStr();
  9. //将json格式字符串转为Map集合
  10. Map map = JSON.parseObject(specStr, Map.class);
  11. //设置spec map
  12. goods.setSpec(map);
  13. //将goods对象转换为json字符串
  14. String data = JSON.toJSONString(goods);
  15. IndexRequest indexRequest=new IndexRequest("goods").source(data,XContentType.JSON);
  16. bulkRequest.add(indexRequest);
  17. }
  18. BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT);
  19. System.out.println(response.status());

二、高级查询操作

1、matchAll查询

脚本:

  1. GET goods/_search
  2. {
  3.   "query": {
  4.     "match_all": {}
  5.   },
  6.   "from": 0,
  7.   "size": 20
  8. }

java:

  1. //构建查询请求对象,指定查询的索引名称
  2. SearchRequest searchRequest=new SearchRequest("goods");
  3. //创建查询条件构建器SearchSourceBuilder
  4. SearchSourceBuilder sourceBuilder=new SearchSourceBuilder();
  5. //查询条件
  6. QueryBuilder queryBuilder= QueryBuilders.matchAllQuery();
  7. //指定查询条件
  8. sourceBuilder.query(queryBuilder);
  9. //添加分页信息 不设置 默认10条
  10. sourceBuilder.from(0);
  11. sourceBuilder.size(20);
  12. //添加查询条件构建器 SearchSourceBuilder
  13. searchRequest.source(sourceBuilder);
  14. //查询,获取查询结果
  15. SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
  16. //1、获取命中对象 SearchHits
  17. SearchHits hits = searchResponse.getHits();
  18. //1.1 获取总记录数
  19. Long total= hits.getTotalHits().value;
  20. System.out.println("总数:"+total);
  21. //1.2 获取Hits数据 数组
  22. SearchHit[] hits1 = hits.getHits();
  23. //获取json字符串格式的数据
  24. List<Goods> goodsList = new ArrayList<>();
  25. for (SearchHit searchHit : hits1) {
  26. String sourceAsString = searchHit.getSourceAsString();
  27. //转为java对象
  28. Goods goods = JSON.parseObject(sourceAsString, Goods.class);
  29. goodsList.add(goods);
  30. }
  31. for (Goods goods : goodsList) {
  32. System.out.println(goods);
  33. }

2、termQuery词条查询

term查询:不会对查询条件进行分词

脚本:

  1. GET goods/_search
  2. {
  3.   "query": {
  4.     "term": {
  5.       "title": {
  6.         "value": "华为"
  7.       }
  8.     }
  9.   }
  10. }

java:

  1. //构建查询请求对象,指定查询的索引名称
  2. SearchRequest searchRequest=new SearchRequest("goods");
  3. //创建查询条件构建器SearchSourceBuilder
  4. SearchSourceBuilder sourceBuilder=new SearchSourceBuilder();
  5. //查询条件
  6. QueryBuilder queryBuilder= QueryBuilders.termQuery("title","华为");
  7. //指定查询条件
  8. sourceBuilder.query(queryBuilder);
  9. //添加查询条件构建器 SearchSourceBuilder
  10. searchRequest.source(sourceBuilder);
  11. //查询,获取查询结果
  12. SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

3、matchQuery查询

match查询:

•会对查询条件进行分词。

•然后将分词后的查询条件和词条进行等值匹配

•默认取并集(OR),也可指定交集(AND)

脚本:

  1. GET goods/_search
  2. {
  3.   "query": {
  4.     "match": {
  5.       "title": {
  6.                 "query": "华为手机",
  7.                 "operator": "and"
  8.         }
  9.     }
  10.   }
  11. }

 java:

  1. //构建查询请求对象,指定查询的索引名称
  2. SearchRequest searchRequest=new SearchRequest("goods");
  3. //创建查询条件构建器SearchSourceBuilder
  4. SearchSourceBuilder sourceBuilder=new SearchSourceBuilder();
  5. //查询条件
  6. MatchQueryBuilder queryBuilder= QueryBuilders.matchQuery("title","华为");
  7. queryBuilder.operator(Operator.AND);
  8. //指定查询条件
  9. sourceBuilder.query(queryBuilder);
  10. //添加查询条件构建器 SearchSourceBuilder
  11. searchRequest.source(sourceBuilder);
  12. //查询,获取查询结果
  13. SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

4、模糊查询

脚本:

(1)wildcard查询

"*华*"  包含华字的
"华*"   华字后边多个字符
"华?"  华字后边1个字符
"*华"或"?华" 会引发全表(全索引)扫描 注意效率问题

  1. GET goods/_search
  2. {
  3.   "query": {
  4.     "wildcard": {
  5.       "title": {
  6.         "value": "华*"
  7.       }
  8.     }
  9.   }
  10. }

(2)正则查询

  1. GET goods/_search
  2. {
  3.   "query": {
  4.     "regexp": {
  5.       "title": "\\w+(.)*"
  6.     }
  7.   }
  8. }

(3)前缀查询

  1. GET goods/_search
  2. {
  3.   "query": {
  4.     "prefix": {
  5.       "brandName": {
  6.         "value": "三"
  7.       }
  8.     }
  9.   }
  10. }

java:

  1. //模糊查询
  2. WildcardQueryBuilder query = QueryBuilders.wildcardQuery("title", "华*");//华后多个字符
  3. //正则查询
  4. RegexpQueryBuilder query = QueryBuilders.regexpQuery("title", "\\w+(.)*");
  5. //前缀查询
  6. PrefixQueryBuilder query = QueryBuilders.prefixQuery("brandName", "三");

 5、范围与排序查询

脚本:

  1. GET goods/_search
  2. {
  3.   "query": {
  4.     "range": {
  5.       "price": {
  6.         "gte": 2000,
  7.         "lte": 3000
  8.       }
  9.     }
  10.   },
  11.   "sort": [
  12.     {
  13.       "price": {
  14.         "order": "desc"
  15.       }
  16.     }
  17.   ]
  18. }

java:

  1. //范围查询 以price 价格为条件
  2. RangeQueryBuilder query = QueryBuilders.rangeQuery("price");
  3. //指定下限
  4. query.gte(2000);
  5. //指定上限
  6. query.lte(3000);
  7. sourceBuilder.query(query);
  8. //排序 价格 降序排列
  9. sourceBuilder.sort("price",SortOrder.DESC);

6、queryString多字段查询

queryString 多条件查询

•会对查询条件进行分词。

•然后将分词后的查询条件和词条进行等值匹配

•默认取并集(OR)

•可以指定多个查询字段

query_string:识别query中的连接符(or 、and)

  1. GET goods/_search
  2. {
  3. "query": {
  4. "query_string": {
  5. "fields": ["title","categoryName","brandName"],
  6. "query": "华为 AND 手机"
  7. }
  8. }
  9. }

simple_query_string:不识别query中的连接符(or 、and),查询时会将 “华为”、"and"、“手机”分别进行查询

  1. GET goods/_search
  2. {
  3. "query": {
  4. "simple_query_string": {
  5. "fields": ["title","categoryName","brandName"],
  6. "query": "华为 AND 手机"
  7. }
  8. }
  9. }

java:

  1. QueryStringQueryBuilder query = QueryBuilders.queryStringQuery("华为手机").field("title").field("categoryName")
  2. .field("brandName").defaultOperator(Operator.AND);

7、布尔查询

boolQuery:对多个查询条件连接。连接方式:

•must(and):条件必须成立

•must_not(not):条件必须不成立

•should(or):条件可以成立

•filter:条件必须成立,性能比must高。不会计算得分

得分:即条件匹配度,匹配度越高,得分越高

  1. GET goods/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": [
  6. {
  7. "term": {
  8. "brandName": {
  9. "value": "华为"
  10. }
  11. }
  12. }
  13. ],
  14. "filter":[
  15. {
  16. "term": {
  17. "title": "手机"
  18. }
  19. },
  20. {
  21. "range":{
  22. "price": {
  23. "gte": 2000,
  24. "lte": 3000
  25. }
  26. }
  27. }
  28. ]
  29. }
  30. }
  31. }

java:

  1. //1.构建boolQuery
  2. BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
  3. //2.构建各个查询条件
  4. //2.1 查询品牌名称为:华为
  5. TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("brandName", "华为");
  6. boolQuery.must(termQueryBuilder);
  7. //2.2. 查询标题包含:手机
  8. MatchQueryBuilder matchQuery = QueryBuilders.matchQuery("title", "手机");
  9. boolQuery.filter(matchQuery);
  10. //2.3 查询价格在:2000-3000
  11. RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("price");
  12. rangeQuery.gte(2000);
  13. rangeQuery.lte(3000);
  14. boolQuery.filter(rangeQuery);
  15. sourceBuilder.query(boolQuery);

 8、聚合查询

指标聚合:相当于MySQL的聚合函数。max、min、avg、sum等

桶聚合:相当于MySQL的 group by 操作。不要对text类型的数据进行分组,会失败。

  1. //指标聚合 聚合函数
  2. GET goods/_search
  3. {
  4. "query": {
  5. "match": {
  6. "title": "手机"
  7. }
  8. },
  9. "aggs": {
  10. "max_price": {
  11. "max": {
  12. "field": "price"
  13. }
  14. }
  15. }
  16. }
  17. //桶聚合 分组
  18. GET goods/_search
  19. {
  20. "query": {
  21. "match": {
  22. "title": "手机"
  23. }
  24. },
  25. "aggs": {
  26. "goods_brands": {
  27. "terms": {
  28. "field": "brandName",
  29. "size": 100
  30. }
  31. }
  32. }
  33. }

java:

  1. /**
  2. * 聚合查询:桶聚合,分组查询
  3. * 1. 查询title包含手机的数据
  4. * 2. 查询品牌列表
  5. */
  6. //1. 查询title包含手机的数据
  7. MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("title", "手机");
  8. sourceBuilder.query(queryBuilder);
  9. //2. 查询品牌列表 只展示前100条
  10. AggregationBuilder
  11. aggregation=AggregationBuilders.terms("goods_brands").field("brandName").size(100);
  12. sourceBuilder.aggregation(aggregation);
  13. searchRequest.source(sourceBuilder);
  14. SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
  15. //7. 获取命中对象 SearchHits
  16. SearchHits hits = searchResponse.getHits();
  17. //7.1 获取总记录数
  18. Long total= hits.getTotalHits().value;
  19. System.out.println("总数:"+total);
  20. // aggregations 对象
  21. Aggregations aggregations = searchResponse.getAggregations();
  22. //将aggregations 转化为map
  23. Map<String, Aggregation> aggregationMap = aggregations.asMap();
  24. //通过key获取goods_brands 对象 使用Aggregation的子类接收 buckets属性在Terms接口中体现
  25. //Aggregation goods_brands1 = aggregationMap.get("goods_brands");
  26. Terms goods_brands =(Terms) aggregationMap.get("goods_brands");
  27. //获取buckets 数组集合
  28. List<? extends Terms.Bucket> buckets = goods_brands.getBuckets();
  29. Map<String,Object>map=new HashMap<>();
  30. //遍历buckets key 属性名,doc_count 统计聚合数
  31. for (Terms.Bucket bucket : buckets) {
  32. System.out.println(bucket.getKey());
  33. map.put(bucket.getKeyAsString(),bucket.getDocCount());
  34. }
  35. System.out.println(map);

9、高亮查询

高亮三要素:

•高亮字段

•前缀

•后缀

默认前后缀 :em

  1. GET goods/_search
  2. {
  3. "query": {
  4. "match": {
  5. "title": "电视"
  6. }
  7. },
  8. "highlight": {
  9. "fields": {
  10. "title": {
  11. "pre_tags": "<font color='red'>",
  12. "post_tags": "</font>"
  13. }
  14. }
  15. }
  16. }

java:1. 设置高亮 高亮字段 前缀 后缀   2. 将高亮了的字段数据,替换原有数据

  1. // 1. 查询title包含手机的数据
  2. MatchQueryBuilder query = QueryBuilders.matchQuery("title", "手机");
  3. sourceBulider.query(query);
  4. //设置高亮
  5. HighlightBuilder highlighter = new HighlightBuilder();
  6. //设置三要素
  7. highlighter.field("title");
  8. //设置前后缀标签
  9. highlighter.preTags("<font color='red'>");
  10. highlighter.postTags("</font>");
  11. //加载已经设置好的高亮配置
  12. sourceBulider.highlighter(highlighter);
  13. searchRequest.source(sourceBulider);
  14. SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
  15. SearchHits searchHits = searchResponse.getHits();
  16. //获取记录数
  17. long value = searchHits.getTotalHits().value;
  18. System.out.println("总记录数:"+value);
  19. List<Goods> goodsList = new ArrayList<>();
  20. SearchHit[] hits = searchHits.getHits();
  21. for (SearchHit hit : hits) {
  22. String sourceAsString = hit.getSourceAsString();
  23. //转为java
  24. Goods goods = JSON.parseObject(sourceAsString, Goods.class);
  25. // 获取高亮结果,替换goods中的title
  26. Map<String, HighlightField> highlightFields = hit.getHighlightFields();
  27. HighlightField HighlightField = highlightFields.get("title");
  28. Text[] fragments = HighlightField.fragments();
  29. //highlight title替换 替换goods中的title
  30. goods.setTitle(fragments[0].toString());
  31. goodsList.add(goods);
  32. }
  33. for (Goods goods : goodsList) {
  34. System.out.println(goods);
  35. }

10、给索引起别名

例如:给 student_index_v2 起别名 student_index_v11 

POST student_index_v2/_alias/student_index_v11

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/146280
推荐阅读
相关标签
  

闽ICP备14008679号