ElasticSearch:具有“匹配值列表”的MultiMatch

时间:2016-08-26 16:59:52

标签: elasticsearch

如何在单个查询中组合MultiMatch +“匹配值列表”。

即。我想根据字段列表["John","Bas","Peter"]

查询名称列表["first_name","Alias","nick_name","surname"]

- 匹配值列表 -

{
  "query": {
    "filtered" : {
      "filter" : {
        "terms": {
          "first_name": ["John","Bas","Peter"]
        }
      }
    }
  }
}

-MultiMatch -

{
  "multi_match" : {
    "query":    "john", 
    "fields": ["first_name","Alias","nick_name","surname"]
  }
}

1 个答案:

答案 0 :(得分:0)

您也可以向multi_match提供多个搜索字词:

  "query": {
    "multi_match": {
      "query": "John Bas Peter",
      "fields": [
        "first_name",
        "Alias",
        "nick_name",
        "surname"
      ]
    }
  }

此外,multi_match有多种方式可以在内部实际转换查询:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html#multi-match-types

如果你使用_validate API,你可以看到ES究竟是如何重新解释查询的:

GET /_validate/query?explain=true
{
  "query": {
    "multi_match": {
      "query": "John Bas Peter",
      "fields": [
        "first_name",
        "Alias",
        "nick_name",
        "surname"
      ]
    }
  }
}

以上给出:

   "explanations": [
      {
         "index": "test",
         "valid": true,
         "explanation": "((Alias:john Alias:bas Alias:peter) | (surname:john surname:bas surname:peter) | (nick_name:john nick_name:bas nick_name:peter) | (first_name:john first_name:bas first_name:peter))"
      }
   ]