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

使用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": {}
    }
  }
}

在这里插入图片描述

如果直接使用查询语句 会发现 查询出的结果 只满足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": {}
    }
  }
}

在这里插入图片描述

最终结果

使用多个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": {}
    }
  }
}

在这里插入图片描述

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);
}
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值