问题及解决方法
must和should组合查询,should失效。使用must嵌套查询,将should组成的bool查询包含在其中一个must查询中。
文章来源:https://www.toymoban.com/news/detail-638124.html
SearchRequest request = new SearchRequest();
request.indices("function_log");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
if (StringUtils.hasText(schoolName)) {
boolQueryBuilder.must(QueryBuilders.termQuery("schoolName.keyword", schoolName));
}
if (StringUtils.hasText(typeFunction)) {
boolQueryBuilder.must(QueryBuilders.termQuery("typeFunction.keyword", typeFunction));
}
if (StringUtils.hasText(startTime) && StringUtils.hasText(endTime)) {
boolQueryBuilder.must(QueryBuilders.rangeQuery("createDate").gte(startTime + "T00:00:00.000Z").lte(endTime + "T23:59:59.000Z"));
}
BoolQueryBuilder shouldQuery = QueryBuilders.boolQuery();
shouldQuery.should().add(QueryBuilders.termQuery("description.keyword","查询学生信息表"));
shouldQuery.should().add(QueryBuilders.termQuery("description.keyword","获取学校访客数据"));
boolQueryBuilder.must(shouldQuery);
sourceBuilder.query(boolQueryBuilder);
request.source(sourceBuilder);
System.out.println(sourceBuilder.toString());
文章来源地址https://www.toymoban.com/news/detail-638124.html
GET /function_log/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"schoolName.keyword": {
"value": "测试学校",
"boost": 1
}
}
},
{
"term": {
"typeFunction.keyword": {
"value": "教师端功能",
"boost": 1
}
}
},
{
"range": {
"createDate": {
"from": "2023-08-01T00:00:00.000Z",
"to": "2023-08-07T23:59:59.000Z",
"include_lower": true,
"include_upper": true,
"boost": 1
}
}
},
{
"bool": {
"should": [
{
"term": {
"description.keyword": {
"value": "查询学生信息表",
"boost": 1
}
}
},
{
"term": {
"description.keyword": {
"value": "获取学校访客数据",
"boost": 1
}
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
}
}
到了这里,关于Elasticsearch同时使用should和must的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!