Search code examples
elasticsearchcoffeescriptelasticsearch-plugin

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


Solution

  • 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"
              }
            }
          ]
        }
      }
    }