Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do multiple "match" or "match_phrase" values in ElasticSearch

Tags:

So ElasticSearch has the terms query, which lets me supply multiple values and return a ist of documents where field X matches any of those values.

But I want to do the same thing with match_phrase - i.e. return documents where field X contains a case insensitive match for a term with spaces. I currently do it my using an or filter (see below). But that seems like a very verbose way of doing what I want, considering that terms does something similar already.

Current method

It seems ridiculous that a query searching a single field for one of three values should be 33 lines long.

{   "query": {     "filtered": {        "filter": {            "or": {               "filters": [                  {                      "query": {                          "match_phrase": {                             "myField1": "My first search phrase"                          }                      }                  },                  {                      "query": {                          "match_phrase": {                             "myField1": "My second search phrase"                          }                      }                  },                  {                      "query": {                          "match_phrase": {                             "myField1": "My third search phrase"                          }                      }                  }               ]            }        }     }   } } 
like image 381
Maloric Avatar asked Jul 03 '14 09:07

Maloric


People also ask

What is multi match query in Elasticsearch?

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.

What is the difference between match and match phrase in Elasticsearch?

Match phrase query is similar to the match query but is used to query text phrases. Phrase matching is necessary when the ordering of the words is important. Only the documents that contain the words in the same order as the search input are matched.

What is Elasticsearch slop?

Question 1: Slop is the number of words separating the span clauses. So slop 0 would mean they are adjacent. In the example I gave, slop of 1 would match.

What is Multimatch?

Multi-Match® Multi-Match is a lotto-style game. For just $2.00, you get to play 18 numbers with four easy ways to match and win. When you play Multi-Match, for each game you play, you will be able to select your first line of six numbers or you can choose Quick Pick.


2 Answers

After a long night trying to figure this out myself I came up with this:

"query" : {         "bool": {             "should": [                {                    "match_phrase": {                       "myField1": "My first search phrase"                    }                },                {                    "match_phrase": {                       "myField1": "My second search phrase"                    }                }             ]         }     } 

Reference: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html

like image 150
Jonathon Cwik Avatar answered Sep 18 '22 12:09

Jonathon Cwik


Query string would be helpful here something on these lines

   {       "query": {         "query_string": {           "default_field": "myField1",           "query": "\"My first search phrase\" OR \"My second search phrase\" OR \"My third search phrase\""         }       }     } 
like image 41
keety Avatar answered Sep 19 '22 12:09

keety