Brian Guan
Brian Guan

Reputation: 323

elasticsearch wrap bool and must around multi_match

I have an existing query, like this:

{
    "from": 0,
    "size": 10,
    "query": {
        "multi_match": {
            "query": "example query", "slop":10,
        "fuzziness": "AUTO"
        }
    }
}

I want to add a filter that only returns results with field "service_or_company": "service", ie only return services as opposed to both services and company. So I updated it to this:

{
    "from": 0,
    "size": 10,
    "query": {
        "multi_match": {
            "query": "example query", "slop":10,
        "fuzziness": "AUTO"
        }
    },
    "filter": {
        "type": "service"
    }
}

However, I get "parsing exception" as according to their documentation, I need to wrap the query and filter inside a "bool" and "must", but how can I do that while still maintaining my "multi_match" query?

Upvotes: 1

Views: 133

Answers (1)

Bhavya
Bhavya

Reputation: 16172

Adding a working example with index data, search query, and search result

Index Data:

{
  "title": "exampl query",
  "service_or_company": "service"
}
{
  "title": "example query",
  "service_or_company": "service"
}
{
  "title": "hello world",
  "service_or_company": "company"
}

Search Query:

{
  "query": {
    "bool": {
      "must": {
        "multi_match": {
          "query": "example query",
          "slop": 10,
          "fuzziness": "AUTO"
        }
      },
      "filter": {
        "term": {                     
          "service_or_company": "service" 
        }
      }
    }
  }
}

Search Result:

"hits": [
      {
        "_index": "65315457",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.79850763,
        "_source": {
          "title": "example query",
          "service_or_company": "service"
        }
      },
      {
        "_index": "65315457",
        "_type": "_doc",
        "_id": "3",
        "_score": 0.6829831,
        "_source": {
          "title": "exampl query",
          "service_or_company": "service"
        }
      }
    ]

Upvotes: 1

Related Questions