querybuilders.filter和querybuilders.must的区别
时间: 2023-11-28 15:05:27 浏览: 59
在Elasticsearch中,QueryBuilders类是用于构建查询的Java类。其中,filter和must都是查询子句,但它们之间有一些重要的区别。
filter是一种不计算相关性得到的查询子句,它只是将符合条件的文档筛选出来。filter通常用于过滤不需要的文档,比如日期范围、地理位置等。filter查询不会影响评分,因为它只是根据条件过滤文档,而不会改变文档与查询的相关性得分。
must查询子句表示查询文档必须满足该条件。如果文档不符合条件,则不会返回。must查询会考虑文档与查询的相关性得分,因此,如果查询子句在搜索结果中出现的越多,文档与查询的相关性得分就越高。
因此,filter适用于那些不需要考虑相关性得分的条件过滤查询,而must适用于需要考虑相关性得分的查询。
相关问题
Z try { SearchRequest request = new SearchRequest(INDEX_NAME); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); if(ids != null && ids.size() > 0){ boolQueryBuilder.must(QueryBuilders.termQuery("id",ids)); }else { // boolQueryBuilder.must(QueryBuilders.matchAllQuery()); } GeoDistanceQueryBuilder geoDistanceQueryBuilder = QueryBuilders.geoDistanceQuery("location") .point(latitude, longitude) .distance(distanceInKm*1000, GeoDistanceQueryBuilder.DEFAULT_DISTANCE_UNIT); // 搜索半径 boolQueryBuilder.filter(geoDistanceQueryBuilder); // sourceBuilder.query(boolQueryBuilder); // sourceBuilder // sourceBuilder request.source().query(boolQueryBuilder); request.source().sort( SortBuilders.geoDistanceSort("location", latitude, longitude) .order(SortOrder.ASC) .unit(DistanceUnit.KILOMETERS) ); request.source().from((currentPage - 1) * PAGE_SIZE).size(PAGE_SIZE); SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT); return handleResponse(searchResponse,currentPage); } catch (IOException e) { throw new RuntimeException(e); }这个代码有什么bug
这段代码可能存在一个潜在的问题,当ids为null或ids.size()为0时,boolQueryBuilder将不会添加任何查询条件,如果此时将boolQueryBuilder作为查询条件传递给request.source().query(),将会抛出NullPointerException异常。为了避免这种情况,可以在ids为null或ids.size()为0时,将boolQueryBuilder设置为一个匹配所有文档的查询条件,如boolQueryBuilder.must(QueryBuilders.matchAllQuery())。另外,代码中的currentPage和PAGE_SIZE应该是预先定义的常量,需要确保其值正确。
有这样一段ES语句{ "query": { "bool": { "must": [ { "match": { "supplierNo": "C2217" } } ] } }, "size": 0, "track_total_hits": false, "aggs": { "my_buckets": { "composite": { "size": 10, "sources": [ { "erpGoodsId": { "terms": { "field": "erpGoodsId", "order": "asc" } } }, { "batchNum": { "terms": { "field": "batchNum", "order": "asc" } } }, { "wmsProductCode": { "terms": { "field": "wmsProductCode", "missing": "null", "order": "asc" } } } ] }, "aggs": { "filter_empty_dest": { "bucket_selector": { "buckets_path": { "count": "_count" }, "script": "params.count != null" } } } } } },请转为searchSourceBuilder相关的JAVA代码
以下是将该 ES 语句转换为 Java SearchSourceBuilder 相关代码的示例:
```java
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.composite.CompositeAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// 设置查询条件
searchSourceBuilder.query(QueryBuilders.boolQuery()
.must(QueryBuilders.matchQuery("supplierNo", "C2217")));
// 设置返回结果数量
searchSourceBuilder.size(0);
// 禁用返回总命中数
searchSourceBuilder.trackTotalHits(false);
// 构建 composite 聚合
CompositeAggregationBuilder compositeAggregation = AggregationBuilders.composite("my_buckets")
.size(10)
.sources(
AggregationBuilders.terms("erpGoodsId").field("erpGoodsId").order("asc"),
AggregationBuilders.terms("batchNum").field("batchNum").order("asc"),
AggregationBuilders.terms("wmsProductCode").field("wmsProductCode").missing("null").order("asc")
);
// 构建 filter 聚合
FilterAggregationBuilder filterAggregation = AggregationBuilders.filter("filter_empty_dest",
QueryBuilders.scriptQuery("params.count != null")
.setParams("count", "_count")
);
// 将 filter 聚合作为 composite 聚合的子聚合
compositeAggregation.subAggregation(filterAggregation);
// 将 composite 聚合作为查询的聚合
searchSourceBuilder.aggregation(compositeAggregation);
```
需要注意的是,以上代码仅提供了将该 ES 语句转换为 Java SearchSourceBuilder 相关代码的思路,具体实现需要根据具体业务场景进行调整和完善。
阅读全文