Vishwanth Iron Heart
Vishwanth Iron Heart

Reputation: 734

Elastic search combining queries and nested queries together

I've been reading elasticsearch queries for some time now and the documentation doesn't seem helpful for some of my queries. I'm trying to get documents which are

((Field1=Keyword1 OR Field2=Keyword1) AND (Field1=Keyword2 OR Field2=Keyword2)) AND (Field3=Keyword3 ) AND (Field4!=Keyword4)).

The document which I refered is https://dzone.com/articles/23-useful-elasticsearch-example-queries and when trying to execute some of their queries in sense API extension in Chrome, it had syntax errors in it. Eg:

{
    "query": {
        "bool": {
            "must": {
                "bool" : { "should": [
                      { "match": { "title": "Elasticsearch" }},
                      { "match": { "title": "Solr" }} ] }
            },
            "must": { "match": { "authors": "clinton gormely" }},
            "must_not": { "match": {"authors": "radu gheorge" }}
        }
    }
}

It gives "Duplicate Must. Syntax error".

Could you help with forming my query?

Thanks.

Upvotes: 0

Views: 96

Answers (2)

Jai Sharma
Jai Sharma

Reputation: 733

As mentioned in the answer by Volodymyr, your query contains 2 must at the same level and hence the error. Look at this documentation to fulfil your requirement.

must: All of these clauses must match. The equivalent of AND.

must_not: All of these clauses must not match. The equivalent of NOT.

should At least one of these clauses must match. The equivalent of OR.

filter: Clauses that must match, but are run in non-scoring, filtering mode.

Upvotes: 0

Vova Bilyachat
Vova Bilyachat

Reputation: 19484

You have must twice in same level. Try this

   {
        "query": {
            "bool": {
                "must": [{
                    "bool" : { "should": [
                          { "match": { "title": "Elasticsearch" }},
                          { "match": { "title": "Solr" }} ] }
                }, { "match": { "authors": "clinton gormely" }}],

                "must_not": { "match": {"authors": "radu gheorge" }}
            }
        }
    }

Upvotes: 1

Related Questions