Lijo Abraham
Lijo Abraham

Reputation: 881

multiple sub query inside one query elasticsearch

I have index named dictionary , where contains field like keyword,mapped keyword and category filter.

Keyword   Mapped Keyowrd       Category
-------   --------------       --------
apple     apple iphone         smartphones
apple     apple watch          smart watches
apple     apple ipad           tablets

So if user searches for apple, internally the query will search mapped keywords with respective categories as below query.

SELECT * FROM products where (title= "*apple*" AND title="*iphone*" and category="smartphones") OR (title= "*apple*" AND title="*ipad*" and category="tablets") OR (title= "*apple*" AND title="*watch*" and category="smart watches")

Below is the corresponding elastic search query,I have written.

{
   "query": {
      "bool": {
         "should": [
            {
               "bool": {
                  "must": [
                     {
                         "match" : {
                            "title" : {
                                "query" : "apple iphone",
                                "operator" : "and"
                            }
                        }
                     },
                     {
                        "term": {
                           "category.raw": "smartphones"
                        }
                     }
                  ]
               }
            },
            {
               "bool": {
                  "must": [
                     {
                        "match" : {
                            "title" : {
                                "query" : "apple watch",
                                "operator" : "and"
                            }
                        }
                     },
                     {
                        "term": {
                           "category.raw": "smartwatch"
                        }
                     }
                  ]
               }
            },
            {
               "bool": {
                  "must": [
                     {
                       "match" : {
                            "title" : {
                                "query" : "apple ipad",
                                "operator" : "and"
                            }
                        }
                     },
                     {
                        "term": {
                           "category.raw": "tablets"
                        }
                     }
                  ]
               }
            }
         ],
         "minimum_should_match": 1
      }
   }
}

Upvotes: 0

Views: 928

Answers (1)

femtoRgon
femtoRgon

Reputation: 33351

Yes, your query looks fine as far as I can tell. "minimum_should_match": 1 isn't really necessary, that's the default behavior.

You might be able to impose that sort of logic using a function_score query (maybe with a script_score), but I think the better way to do that would be to just execute three different queries, and get the results for each. If you want to execute those multiple queries in one request, you can do that using the Multi Search API.

Upvotes: 1

Related Questions