Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ElasticSearch with multi_match AND bool

I try to learn Elasticsearch to add it in my Rails app. I want to perform a multi_match query into 2 fields (as if they were just a single field) and also have a filter to another field (status) that must be equal to 1.

let response = Wine.search({
  query: {
    multi_match: {
      query: "test",
      fields: ["winery", "name"]
    },
    bool: {
      must: {
        term: { status: 1 }
      },
      should: [],
      minimum_should_match: 1
    }
  }     
})

The error is:

"fields\":[\"winery\",\"name\"]},\"bool\":{\"must\":{\"term\":{\"status\":1}},\"should\":[],\"minimum_should_match\":1}}}]]]; nested: ElasticsearchParseException[Expected field name but got START_OBJECT \"bool\"]; }]","status":400}

What is wrong in the request ? How to perform a multi_match AND a BOOL together ?

like image 343
alex.bour Avatar asked Jan 08 '15 08:01

alex.bour


1 Answers

Use a filtered query:

{
    "query": {
        "filtered": {
            "query": {
                "multi_match": {
                    "query": "test",
                    "fields": [
                        "winery",
                        "name"
                    ]
                }
            },
            "filter": {
                "term": {
                    "status": "1"
                }
            }
        }
    }
}

Same query for Elasticsearch 5:

{
    "query": {
        "bool": {
            "must": {
                "multi_match": {
                    "query": "test",
                    "fields": [
                        "winery",
                        "name"
                    ]
                }
            },
            "filter": {
                "term": {
                    "status": "1"
                }
            }
        }
    }
}
like image 62
Dan Tuffery Avatar answered Sep 27 '22 18:09

Dan Tuffery