首页 > 解决方案 > 弹性搜索结合匹配和应该

问题描述

我想创建一个可以使用匹配的查询,并且该匹配应该只考虑另一个字段值为空或某个特定值的行。

所以像:

where field1 like 'string' and field2 is null or field2 = 123

JS客户端有可能吗?

标签: elasticsearchsearchelasticsearch-dsl

解决方案


您可以结合使用bool/must/should存在通配符查询

{
  "query": {
    "bool": {
      "must": [
        {
          "wildcard": {
            "field1": "string*"
          }
        },
        {
          "bool": {
            "should": [
              {
                "term": {
                  "field2": 123
                }
              },
              {
                "bool": {
                  "must_not": {
                    "exists": {
                      "field": "field2"
                    }
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

推荐阅读