Varun Sreedharan
Varun Sreedharan

Reputation: 577

How do I combine multiple queries in ElasticSearch

I am trying to retrieve records that match two queries.

First one is match_phrase

"match_phrase": {
                    "searchstring": {
                    "query": "' . $searchQ . '",
                    "slop":  10
                    }
                }

Second one I am using multi match

"multi_match": {
                    "query":    "' . $searchQ . '",
                    "fields" : ["_all"],
                    "type":       "cross_fields",
                }

how can I do this correctly. Please help

Upvotes: 0

Views: 103

Answers (1)

Val
Val

Reputation: 217424

You can use a bool/must (or bool/should) query in order to combine your two queries:

{
  "query": {
    "bool": {
      "should": [
        {
          "match_phrase": {
            "searchstring": {
              "query": "xyz",
              "slop": 10
            }
          }
        },
        {
          "multi_match": {
            "query": "xyz",
            "fields": [
              "_all"
            ],
            "type": "cross_fields"
          }
        }
      ]
    }
  }
}

Upvotes: 2

Related Questions