Elasticsearch-"OR"使用匹配查询和词条查询的查询条件 [英] Elasticsearch - "OR" query condition using match query and term query

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

问题描述

我有以下 match 查询字符串:

  curl -XGET'my-es.com/my_indice/_search?pretty'-d'{大小":10,询问" : {布尔":{必须" : [ {比赛" : {状态" : {"query":["ACTIVE","INACTIVE"],"type":布尔值"}}}]}}}' 

我猜这意味着"state" ="ACTIVE"或"state" ="INACTIVE" ,但实际上它执行的是"state" ="INACTIVE" ./p>

然后我尝试了 term 查询字符串:

  curl -XGET'my-es.com/my_indice/_search?pretty'-d'{大小":10,询问" : {布尔":{必须" : [{"terms":{"state":["ACTIVE","INACTIVE"]}}]}}}' 

它执行"state" ="ACTIVE"或"state" ="INACTIVE" ,显示 term 查询通过以下方式支持多个OR条件数组.

我很好奇为什么 match 查询不通过数组支持 OR 条件?而且它不显示任何语法错误.

解决方案

match 查询仅支持指定一个字符串值.在官方 match 文档,但是如果您愿意阅读

I have following match query string:

curl -XGET 'my-es.com/my_indice/_search?pretty' -d '{
  "size" : 10,
  "query" : {
    "bool" : {
      "must" : [ {
        "match" : {
          "state" : {
            "query" : ["ACTIVE", "INACTIVE"],
            "type" : "boolean"
          }
        }
      }]
    }
  }
}'

I guess it means "state" = "ACTIVE" or "state" = "INACTIVE", but actually it performs "state" = "INACTIVE".

Then I tried term query string:

curl -XGET 'my-es.com/my_indice/_search?pretty' -d '{
  "size" : 10,
  "query" : {
    "bool" : {
      "must" : [{
         "terms" : { "state" : ["ACTIVE", "INACTIVE"] }
      }]
    }
  }
}'

It performs "state" = "ACTIVE" or "state" = "INACTIVE", showing term query supports multiple OR conditions via an array.

I'm curious about why match query does not support OR condition via an array? And it does not show any syntax error.

解决方案

The match query only supports a single string value to be specified. It is not explicitly specified in the official match documentation, but if you're willing to read some source code for MatchQueryParser.java, you can see that while parsing the query field, the parser will skip the tokens delimiting the start and end of the array and always override the value with the latest one being parsed, hence why you're seeing what you see, i.e. that state will be matched with INACTIVE only.

What you can do, however, is to put both tokens inside the same string like below, and both tokens will be taken into account:

curl -XGET 'my-es.com/my_indice/_search?pretty' -d '{
  "size" : 10,
  "query" : {
    "bool" : {
      "must" : [ {
        "match" : {
          "state" : {
            "query" : "ACTIVE INACTIVE",
            "type" : "boolean"
          }
        }
      }]
    }
  }
}'

这篇关于Elasticsearch-"OR"使用匹配查询和词条查询的查询条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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