Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 572
punkish Avatar asked May 03 '15 22:05

punkish


People also ask

What is match phrase in Elasticsearch?

Match phrase queryeditA phrase query matches terms up to a configurable slop (which defaults to 0) in any order. Transposed terms have a slop of 2. The analyzer can be set to control which analyzer will perform the analysis process on the text.

What is multi match query?

The multi_match query builds on the match query to allow multi-field queries: GET /_search { "query": { "multi_match" : { "query": "this is a test", "fields": [ "subject", "message" ] } } } The query string. The fields to be queried.

How does Elasticsearch match query work?

The match query analyzes any provided text before performing a search. This means the match query can search text fields for analyzed tokens rather than an exact term. (Optional, string) Analyzer used to convert the text in the query value into tokens. Defaults to the index-time analyzer mapped for the <field> .

How do I merge two queries in Elasticsearch?

You need to use the bool query to combine different queries together. You can then choose whether each single query must match, should match (optional), or must not match.


1 Answers

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"}}       ]     }  } } 
like image 109
Jakub Kotowski Avatar answered Sep 30 '22 14:09

Jakub Kotowski