当前位置:   article > 正文

ES中 同时使用should和must 导致只有must生效 解决方案_es must和should混合

es must和should混合
使用ES查询语句的时候 会遇到嵌套多条件查询情况
  • title或者content包含xx(should)
  • type必须是1(must)
  • enabled必须是1(must_not)
只使用should查询
GET _search
{
  "query": {
    "bool": {
      "should": [
        {
          "match_phrase": {
            "title": "疫情期间"
          }
        },
        {
          "match_phrase": {
            "content": "疫情期间"
          }
        }
      ],
      "must_not": [
        {
          "match": {
            "enabled": 0
          }
        }
      ]
    }
  },
  "highlight": {
    "pre_tags": [
      "<font color='red'>"
    ],
    "post_tags": [
      "</font>"
    ],
    "fields": {
      "title": {},
      "content": {}
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

在这里插入图片描述

如果直接使用查询语句 会发现 查询出的结果 只满足must条件 should失效了
GET _search
{
  "query": {
    "bool": {
      "should": [
        {
          "match_phrase": {
            "title": "疫情期间"
          }
        },
        {
          "match_phrase": {
            "content": "疫情期间"
          }
        }
      ],
      "must": [
        {"match": {
          "type": "1"
        }}
      ], 
      "must_not": [
        {
          "match": {
            "enabled": 0
          }
        }
      ]
    }
  },
  "highlight": {
    "pre_tags": [
      "<font color='red'>"
    ],
    "post_tags": [
      "</font>"
    ],
    "fields": {
      "title": {},
      "content": {}
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

在这里插入图片描述

最终结果

使用多个must嵌套查询 将should组成的bool查询包含在其中一个must查询中

在这里插入图片描述

GET _search
{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              {
                "match_phrase": {
                  "title": "疫情期间"
                }
              },
              {
                "match_phrase": {
                  "content": "疫情期间"
                }
              }
            ]
          }
        },
        {
          "match": {
            "type": "1"
          }
        }
      ],
      "must_not": [
        {
          "match": {
            "enabled": 0
          }
        }
      ]
    }
  },
  "highlight": {
    "pre_tags": [
      "<font color='red'>"
    ],
    "post_tags": [
      "</font>"
    ],
    "fields": {
      "title": {},
      "content": {}
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

在这里插入图片描述

Java代码
@Override
public CommonResult<?> search(String keyword, Integer type, Integer pageNum, Integer pageSize) {
    // 处理分页逻辑
    pageNum = Optional.ofNullable(pageNum).orElse(1);
    pageSize = Optional.ofNullable(pageSize).orElse(10);
    // 设置代码高亮
    String preTag = "<font color='#dd4b39'>";
    String postTag = "</font>";
    // 创建查询语句 ES中must和should不能同时使用 同时使用should失效 嵌套多个must 将should条件拼接在一个must中即可
    BoolQueryBuilder shouldQuery = QueryBuilders.boolQuery()
            .should(QueryBuilders.matchPhraseQuery("title", keyword))
            .should(QueryBuilders.matchPhraseQuery("content", keyword));
    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
            .must(shouldQuery)
            .mustNot(QueryBuilders.matchQuery("enabled", 0));
    if (!ObjectUtils.isEmpty(type) && type > 0 && type < 9) {
        boolQueryBuilder.must(QueryBuilders.matchQuery("type", type));
    }
    // 封装查询
    NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder()
            .withQuery(boolQueryBuilder)
            .withPageable(PageRequest.of(Math.max(pageNum - 1, 0), pageSize))
            .withHighlightFields(new HighlightBuilder.Field("title").preTags(preTag).postTags(postTag),
                    new HighlightBuilder.Field("content").preTags(preTag).postTags(postTag));
    // 进行查询操作
    AggregatedPage<SearchMainPageEntity> pages = mRestTemplate.queryForPage(builder.build(), SearchMainPageEntity.class, new CustomEsResultMapper());
    Object resultInfo = CustomEsResultMapper.inflatePageInfo(pages);
    return successBody(resultInfo);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/807319
推荐阅读
相关标签
  

闽ICP备14008679号