Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 835
user1935449 Avatar asked Apr 03 '16 18:04

user1935449


People also ask

How do I merge two queries in Elasticsearch?

You can combine the queries using bool query. Based on your requirement you can use 'should' or 'must' inside the bool clauses.

How does bool query work in Elasticsearch?

Boolean, or a bool query in Elasticsearch, is a type of search that allows you to combine conditions using Boolean conditions. Elasticsearch will search the document in the specified index and return all the records matching the combination of Boolean clauses.

Can you join tables in Elasticsearch?

Elasticsearch does not support joining of indexes like in SQL. Instead elasticsearch offers two types of joins within a single index. The first is a nested query where a field value can be an array of objects, and the query can address the nested object fields.

What is BoolQueryBuilder?

public BoolQueryBuilder should(QueryBuilder queryBuilder) Adds a clause that should be matched by the returned documents. For a boolean query with no MUST clauses one or more SHOULD clauses must match a document for the BooleanQuery to match. No null value allowed.


1 Answers

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"
                                            }
                                        }
                                    ]
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}
like image 116
scottjustin5000 Avatar answered Oct 14 '22 06:10

scottjustin5000