术语筛选具有多个条件



我想在ES.的一次命中中搜索具有2个或多个条件的多个值

CCD_ 1。我使用下面的查询来搜索与这两个字段匹配的结果。

`{
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {
        "and": [
          {
            "terms": {
              "userid": [
                "1","2"
              ]
            }
          },
          {
            "terms": {
              "order": [
                 "A","B"
              ]
            }
          }
        ]
      }
    }
  }
}`

结果匹配满足所有组合的文档(如1&A, 1&B, 2&A, 2&B)。但我只需要按照发送的顺序匹配结果(如1&A, 2&B)。我们可以通过条款过滤器或任何其他选择来实现这一点吗?

您可以始终使用嵌套的"and" s和"or" s:

POST /test_index/_search
{
    "query": {
        "filtered": {
            "query": {
                "match_all": {}
            },
            "filter": {
                "or": {
                    "filters": [
                        { "and": [
                            { "term": { "userid": { "value": "1" } } },
                            { "term": { "order": { "value": "A" } } }
                        ]},
                        { "and": [
                            { "term": { "userid": { "value": "2" } } },
                            { "term": { "order": { "value": "B" } } }
                        ]}
                    ]
                }
            }
        }
    }
}

为了实现这一点,您需要将字段"userid"one_answers"order"放入嵌套的字段中。类似于这种映射:

{
    "index1" : {
        "mappings" : {
            "type1" : {
                "properties" : {
                    "iAmNested" : {
                        "type" : "nested",
                        "properties" : {
                            "userid" : {
                                "type" : "string"
                            },
                            "order" : {
                                "type" : "string"
                            }
                        }
                    }
                }
            }
        }
    }
}

然后您可以使用嵌套过滤器进行查询,信息可以在这里找到:

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-nested-filter.html

最新更新