Elastic Search AND and OR condition is not returning correct data

65 Views Asked by At

I have data from two sources of document one which have status purchased and another does not have status purchased. In order to identify the status purchased i am using a nested object called purchase which have purchase.location and purchase.purchased_on. Thus I am able to identify the item is purchased in second case.

I am building a filter and want to filter the purchased item which should return me data with documents have status purchased or should have purchase.location as one of the value and purchase.purchased_on should be greater than 1.

Elastic Search query built by me is

{
 "query": {
   "bool": {
     "should": [
       {
         "terms": {
           "status": [
             "purchased"
           ]
         }
       },
       {
         "bool": {
           "should": [
             {
               "terms": {
                 "purchase.location": [
                   "flipkart",
                   "amazon",
                   "bestbuy"
                 ]
               }
             },
             {
               "range": {
                 "purchase.purchased_on": {
                   "gte": 1
                 }
               }
             }
           ]
         }
       }
     ]
   }
 }
}

It only returns data whose status is purchased and does not return the data of other condition.

1

There are 1 best solutions below

0
Mathew On

use nested query,

{
    "query": {
        "bool": {
            "should": [
                {
                    "terms": {
                        "status": [
                            "purchased"
                        ]
                    }
                },
                {
                    "nested": {
                        "path": "purchase",
                        "query": {
                            "bool": {
                                "should": [
                                    {
                                        "terms": {
                                            "purchase.location": [
                                                "flipkart",
                                                "amazon",
                                                "bestbuy"
                                            ]
                                        }
                                    },
                                    {
                                        "range": {
                                            "purchase.purchased_on": {
                                                "gte": 1
                                            }
                                        }
                                    }
                                ],
                                "minimum_should_match": 1
                            }
                        }
                    }
                }
            ],
            "minimum_should_match": 1,
            "boost": 1.0
        }
    }
}

and the result is,

{
    "took": 47,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 1.287682,
        "hits": [
            {
                "_index": "purchase",
                "_type": "_doc",
                "_id": "yb5za4cB6Rdc8HbD9stk",
                "_score": 1.287682,
                "_source": {
                    "purchase": [
                        {
                            "location": "flipkart",
                            "purchased_on": "2"
                        }
                    ]
                }
            },
            {
                "_index": "purchase",
                "_type": "_doc",
                "_id": "yr50a4cB6Rdc8HbD_cto",
                "_score": 1.0,
                "_source": {
                    "status": "purchased"
                }
            },
            {
                "_index": "purchase",
                "_type": "_doc",
                "_id": "y753a4cB6Rdc8HbDCcuM",
                "_score": 1.0,
                "_source": {
                    "purchase": [
                        {
                            "purchased_on": "4"
                        }
                    ]
                }
            }
        ]
    }
}