14

I want to create the equivalent of the following query -

(city = 'New York' AND state = 'NY') AND ((businessName='Java' and businessName='Shop') OR (category='Java' and category = 'Shop'))

I tried different combinations of bool queries using must and should but nothing seems to be working. Can this be done?

2
  • what is not working - can you elaborate? Commented Apr 3, 2016 at 18:41
  • I am not able to come up with the right syntax. Always get some parsing exception. Commented Apr 3, 2016 at 19:10

1 Answer 1

33

How about something like this:

{
    "query": {
        "match_all": {}
    },
    "filter": {
        "bool": {
            "must": [
                {
                    "term": {
                        "city": "New york"
                    }
                },
                {
                    "term": {
                        "state": "NY"
                    }
                },
                {
                    "bool": {
                        "should": [
                            {
                                "bool": {
                                    "must": [
                                        {
                                            "term": {
                                                "businessName": "Java"
                                            }
                                        },
                                        {
                                            "term": {
                                                "businessName": "Shop"
                                            }
                                        }
                                    ]
                                }
                            },
                            {
                                "bool": {
                                    "must": [
                                        {
                                            "term": {
                                                "category": "Java"
                                            }
                                        },
                                        {
                                            "term": {
                                                "category": "Shop"
                                            }
                                        }
                                    ]
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I think you might add minimum_should_match for should query
Still working in v7.5 - don't forget to remove/check the placement of minimum_should_match for queries as you could end up getting no results at all if you'r having a must query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.