如何使用Elasticsearch编写条件查询? [英] How to write a conditional query with Elasticsearch?

查看:124
本文介绍了如何使用Elasticsearch编写条件查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Elasticsearch有一个简单的JSON查询,如下所示:

I have a simple JSON query for Elasticsearch that looks like this:

"query": {
  "bool": {
    "must": { "match": { "id": "1" }} ,
    "must": { "match": { "tags.name": "a1"}}
   }
}

如何执行第二个'标准只有当值('a1'在这种情况下)不是空的?

How can I execute the second 'must' criteria ONLY if the value ('a1' in this case) is not empty?

推荐答案

您可以使用以下功能实现 -

You can achieve it using the following -

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "id": "1"
          }
        },
        {
          "bool": {
            "should": [
              {
                "missing": {
                  "field": "tags.name"
                }
              },
              {
                "match": {
                  "tags.name": "a1"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

这篇关于如何使用Elasticsearch编写条件查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆