user1935449
user1935449

Reputation: 317

How to combine multiple bool queries in elasticsearch

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?

Upvotes: 14

Views: 23382

Answers (1)

scottjustin5000
scottjustin5000

Reputation: 1356

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

Upvotes: 33

Related Questions