executing a multi-“match-phrase” query in Elastic Search

徘徊边缘 提交于 2020-01-02 00:27:07

问题


this should be obvious to me but is not. The following two match only the second phrase (in this case, "Cape Basin")

"query": {
  "match_phrase": {
    "contents": {
      "query": "St Peter Fm",
      "query": "Cape Basin"
    }
  }
}

"query": {
  "match_phrase": {
    "contents": {
      "query": ["St Peter Fm", "Cape Basin"]
    }
  }
}

while the following croaks with an error

"query": {
  "match_phrase": {
    "contents": {
      "query": "St Peter Fm"
    },
    "contents": {
      "query": "Cape Basin"
    }
  }
}

I want to match all documents that contain both either phrases exactly as entered.

Update: See update immediately above


回答1:


Your first query is not really a valid JSON object because you use the same field name twice.

You can use a bool must query to match both phrases:

PUT phrase/doc/1
{
  "text": "St Peter Fm some other text Cape Basin"
}
GET phrase/_search
{
  "query": {
    "bool": {
      "must": [
         {"match_phrase": {"text":  "St Peter Fm"}},
         {"match_phrase": {"text":  "Cape Basin"}}
      ]
    }
 }
}



回答2:


It turns out you can do this by enabling phrase semantics for multi_match.

To do this you add a type: attribute to the multi_match syntax as below:

GET /_search
{
  "query": {
    "multi_match" : {
      "query":      "quick brown fox",
      "type":       "phrase",
      "fields":     [ "subject", "message" ]
    }
  }
}

Once you think of it that way (vs. enabling "multi" support on other search clauses) it fits in where you'd expect.

ref: https://www.elastic.co/guide/en/elasticsearch/reference/6.5/query-dsl-multi-match-query.html#type-phrase



来源:https://stackoverflow.com/questions/30020178/executing-a-multi-match-phrase-query-in-elastic-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!