ElasticSearch Multi Match with Multiple Query Parameters

时间:2018-02-26 17:34:48

标签: elasticsearch

We have the following multi match query in Elastic Search

{
"query": {
    "bool": {
        "must": {
            "multi_match": {
                "query": "90803",
                "type": "cross_fields",
                "fields": [
                    "POSTAL_CODE^5",
                    "ADDRESS",
                    "CITY"
                ],
                "operator": "and"
            }
        }
    }
}}

How can we pass multiple query parameters. For e.g. we want to pass multiple ID in the query to match against the field Postal Code.

2 个答案:

答案 0 :(得分:0)

首先,POSTAL_CODE是分析字段吗?如果不是这样,您可以使用Terms Query

{
    "query": {
        "terms" : { 
            "POSTAL_CODE" : ["90803", "90809"]
        }
    }
}

如果由于某种原因想要使用匹配,则没有匹配多个值的匹配查询,您必须使用Bool Query shouldmust,具体取决于您的使用情况情况下。

must的示例:

{
    "query": {
        "bool": {
            "must": [{
                "match": { "POSTAL_CODE": "90803" },
                "match": { "POSTAL_CODE": "90809" }
            }]
        }
    }
}

答案 1 :(得分:0)

此查询将在字段POSTAL_CODE中查找两个不同的邮政编码值。

{
    "query": {
      "bool": {
        "should": [
          {
            "multi_match": {
              "query": "90803",
              "fields": [
                "POSTAL_CODE"
              ]
            }
          },
          {
            "multi_match": {
              "query": "90809",
              "fields": [
                "POSTAL_CODE"
              ]
            }
          }
        ]
      }
    }
  }