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?
You can combine the queries using bool query. Based on your requirement you can use 'should' or 'must' inside the bool clauses.
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.
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.
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.
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"
}
}
]
}
}
]
}
}
]
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With