ElasticSearch---es用should表示or的逻辑

should

在使用es时,如果需要用到or逻辑,可以使用should。

minimum_should_match

should,可以配合 minimum_should_match 使用。
minimum_should_match是最低匹配度, minimum_should_match为1, 表示should条件中,至少有一项符合。

注意,should和must一起用,should会失效,加上minimum_should_match 就可以了。

示例1

比如, a && (b or c) ,可以如下所示:

{
  "bool" : {
    "filter" : [
      {
        "terms" : {
          "查询条件a" : [
            "a值"
          ],
          "boost" : 1.0
        }
      }
    ],
    "should" : [
      {
        "terms" : {
          "查询条件b" : [
            "b值"
          ],
          "boost" : 1.0
        }
      },
      {
        "terms" : {
          "查询条件c" : [
            "c值"
          ],
          "boost" : 1.0
        }
      }
    ],
    "disable_coord" : false,
    "adjust_pure_negative" : true,
    "minimum_should_match" : "1",
    "boost" : 1.0
  }
}

对应的java代码如下:

BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        boolQuery.filter(QueryBuilders.termsQuery("查询条件a", "a值"));
        boolQuery.should(QueryBuilders.termsQuery("查询条件b", "b值"))
                .should(QueryBuilders.termsQuery("查询条件c", "c值"))
                .minimumShouldMatch(1);

示例2

比如, a && ((b && c) or (d && e)) ,可以如下所示:

{
  "bool" : {
    "filter" : [
      {
        "terms" : {
          "查询条件a" : [
            "值a"
          ],
          "boost" : 1.0
        }
      }
    ],
    "should" : [
      {
        "bool" : {
          "must" : [
            {
              "terms" : {
                "查询条件b" : [
                  "值b"
                ],
                "boost" : 1.0
              }
            },
            {
              "term" : {
                "查询条件c" : {
                  "value" : "值c",
                  "boost" : 1.0
                }
              }
            }
          ],
          "disable_coord" : false,
          "adjust_pure_negative" : true,
          "boost" : 1.0
        }
      },
      {
        "bool" : {
          "must" : [
            {
              "terms" : {
                "查询条件d" : [
                  "值d"
                ],
                "boost" : 1.0
              }
            },
            {
              "term" : {
                "查询条件e" : {
                  "value" : "值e",
                  "boost" : 1.0
                }
              }
            }
          ],
          "disable_coord" : false,
          "adjust_pure_negative" : true,
          "boost" : 1.0
        }
      }
    ],
    "disable_coord" : false,
    "adjust_pure_negative" : true,
    "minimum_should_match" : "1",
    "boost" : 1.0
  }
}

对应的java代码如下:

BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
    boolQuery.filter(QueryBuilders.termsQuery("查询条件a", "值a"));
    boolQuery.should(QueryBuilders.boolQuery().must(QueryBuilders.termsQuery("查询条件b", "值b"))
            .must(QueryBuilders.termQuery("查询条件c", "值c")))
            .should(QueryBuilders.boolQuery().must(QueryBuilders.termsQuery("查询条件d", "值d"))
                    .must(QueryBuilders.termQuery("查询条件e", "值e")))
            .minimumShouldMatch(1);
    System.out.println(boolQuery);

小技巧: 直接写dsl,会比较麻烦,用代码去把dsl打印出来,会方便很多。

参考资料

http://events.jianshu.io/p/84789dd89dcfelasticsearch bool中should must联用问题_My name is Red ^^的博客-CSDN博客

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值