'Multi-"match-phrase" query in Elastic Search
this should be obvious to me but is not. The following two-match only the second phase (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 either phrases exactly as entered.
Solution 1:[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 or should query to match both OR one of the phrases:
PUT phrase/doc/1
{
"text": "St Peter Fm some other text Cape Basin"
}
//Match BOTH
GET phrase/_search
{
"query": {
"bool": {
"must": [
{"match_phrase": {"text": "St Peter Fm"}},
{"match_phrase": {"text": "Cape Basin"}}
]
}
}
}
//Match EITHER ONE
GET phrase/_search
{
"query": {
"bool": {
"should": [
{"match_phrase": {"text": "St Peter Fm"}},
{"match_phrase": {"text": "Cape Basin"}}
]
}
}
}
Solution 2:[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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ricardo |
| Solution 2 | Jim K. |
