30

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"
                         }
                     }
                 }
              ]
           }
       }
    }
  }
}
Maloric
  • 5,525
  • 3
  • 31
  • 46
  • This is better answer : https://stackoverflow.com/questions/30020178/executing-a-multi-match-phrase-query-in-elastic-search/30020384#30020384 – Devender May 31 '19 at 10:36

3 Answers3

29

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

Jonathon Cwik
  • 987
  • 11
  • 18
16

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\""
        }
      }
    }
keety
  • 17,231
  • 4
  • 51
  • 56
  • You can also look in many fileds and use the AND orperator : `"query_string": { "query": "fieldOne:(\"foo\" OR \"bar\") AND fieldTwo:(\"foobar\")" }` – Sami Mar 31 '21 at 13:19
2

The query in Elastic-Search Domain Specific Language (DSL) is as follows:

{
  "query": {
    "bool": {
      "should": [
        {
          "match_phrase": {
            "platform_id": "GPL13534"
          }
        },
        {
          "match_phrase": {
            "platform_id": "GPL8490"
          }
        }
      ],
      "minimum_should_match": 1
    }
  }
}

To find out stuff like this easily, use the Kibana interface:

enter image description here

  • Hit on the right-bottom enter sign to accept multiple options
  • "Edit as Query DSL"

(In this example myField1 is platform_id and the search phrases are "GPL***")

Ofer Rahat
  • 790
  • 1
  • 9
  • 15