Elasticsearch判断多列存在、bool条件组合查询示例

and符号判断多列存在:{
  "filter": {
    "and": [
      {
        "exists": {
          "field": "sid"
        }
      },
      {
        "exists": {
          "field": "level"
        }
      }
    ]
  }
}

bool组合

{
  "filter": {
    "and": [
      {
        "or": [
          {
            "match_phrase": {
              "displayname": "s"
            }
          },
          {
            "match_phrase": {
              "displayname": "l"
            }
          }
        ]
      },
      {
        "match_phrase": {
          "displayname": "a"
        }
      },
      {
        "not": {
          "match_phrase": {
            "displayname": "p"
          }
        }
      }
    ]
  }
}

注:类似的and符号替换成 bool must实际上结果是一样的;目前ElasticSearch官方推荐写法是使用bool替代之前的and or语句。

转载于:https://www.cnblogs.com/thaughtZhao/p/5537234.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Elasticsearch的match查询可以用于多条件查询,可以通过使用bool查询来实现。bool查询可以将多个查询条件组合在一起,包括must、should、must_not和filter。 例如,我们想要查询title字段包含“elasticsearch”和content字段包含“java”的文档,可以使用以下查询: ``` { "query": { "bool": { "must": [ { "match": { "title": "elasticsearch" } }, { "match": { "content": "java" } } ] } } } ``` 这个查询使用了bool查询,将两个match查询条件组合在一起,使用must关键字表示这两个条件都必须满足。如果我们想要查询title字段包含“elasticsearch”或content字段包含“java”的文档,可以使用should关键字: ``` { "query": { "bool": { "should": [ { "match": { "title": "elasticsearch" } }, { "match": { "content": "java" } } ] } } } ``` 这个查询使用了should关键字,表示只要满足其中一个条件即可。如果我们想要查询title字段包含“elasticsearch”但是content字段不包含“java”的文档,可以使用must和must_not关键字: ``` { "query": { "bool": { "must": [ { "match": { "title": "elasticsearch" } } ], "must_not": [ { "match": { "content": "java" } } ] } } } ``` 这个查询使用了must和must_not关键字,表示title字段必须包含“elasticsearch”,但是content字段不能包含“java”。如果我们想要查询title字段包含“elasticsearch”并且content字段包含“java”,但是只返回score大于等于0.5的文档,可以使用filter关键字: ``` { "query": { "bool": { "must": [ { "match": { "title": "elasticsearch" } }, { "match": { "content": "java" } } ], "filter": { "range": { "_score": { "gte": 0.5 } } } } } } ``` 这个查询使用了filter关键字,表示只返回score大于等于0.5的文档。注意,filter查询不会影响score的计算,因此可以提高查询效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值