suja
suja

Reputation: 1288

Elastic Search - Filter with condition for many OR condition

Hi am very new to Elastic Search and find it a bit difficult to understand how it works. Say suppose I have 20 products in elastic search server. Each product object looks something like this

 {
 "took" : 0,
 "timed_out" : false,
 "_shards" : {
  "total" : 1,
 "successful" : 1,
 "skipped" : 0,
 "failed" : 0
 },
"hits" : {
  "total" : {
  "value" : 27,
  "relation" : "eq"
},
"hits" : [
  {
    "_index" : "ddddd",
    "_type" : "_doc",
    "_id" : "1062985",
    "_score" : 1.0,
    "_source" : {
      "productId" : "1234567890",
      "name" : "Cakes",
      "slug" : "cakes",
      "adId" : 1062985,
      "shortDescription" : "My teddy Bear",
      "longDescription" : "",
      "totalRating" : 0,
      "totalReviewCount" : 0,
      "attributeSet" : "609e11c66b66fe030d94f400",
"mapDetails" : {
        "addressComponents" : {
          "country" : {
            "shortName" : "IN",
            "longName" : "India"
          },
          "administrativeAreaLevel1" : {
            "shortName" : "KL",
            "longName" : "Kerala"
          },
          "administrativeAreaLevel2" : {
            "shortName" : "TVM",
            "longName" : "Thiruvananthapuram"
          },
          "locatlity" : {
            "shortName" : "",
            "longName" : ""
          },
          "sublocalityLevel1" : {
            "shortName" : "Ulloor",
            "longName" : "Ulloor"
          },
          "sublocalityLevel2" : {
            "shortName" : "",
            "longName" : ""
          },
          "route" : {
            "shortName" : "SH 1",
            "longName" : "State Highway 1"
          },
          "postalCode" : {
            "shortName" : "695015",
            "longName" : "695015"
          }
        },
        "location" : [
          8.5241391,
          76.9366376
        ],
        "locationString" : "206, SH 1, Ulloor, Thiruvananthapuram, Kerala 695015, India"
      },
      "category" : [
        {
          "catId" : "60b47e0cf511114e4a9107d8",
          "isCatgegory" : true
        },
        {
          "catId" : "root",
          "isParentCatgegory" : true
        }
      ]
 }
]
}
}

now what i need to achieve is sort them according to date and then from that filter out the featured ones using featured tag in object. Then from that filter using multiple OR conditions

What I tried is

     {
       "from": 0,
      "size": 10,
      "query": {
    "bool": {
      "must": [
        {
          "term": {
            "featured": {
              "value": "true"
            }
          }
        },
        {
          "bool": {
            "filter": [
              {
                "terms": {
                  "category.catId": [
                    "60c2c8c1031ee362a67e1316",
                    "609e1a546b66fe030d94f402",
                    "609e1ad46b66fe030d94f405",
                    "60b47e0cf511114e4a9107d8"
                  ]
                }
              }
            ]
          }
        },
        {
          "bool": {
            "filter": [
              {
                "nested": {
                  "path": "mapDetails.addressComponents.country",
                  "query": {
                    "term": {
                      "mapDetails.addressComponents.country.shortName": {
                        "value": "IN"
                      }
                    }
                  }
                }
              }
            ]
          }
        }
      ]
    }
  },
  "sort": [
    {
      "featured": {
        "order": "desc"
      }
    },
    {
      "updatedAt": {
        "order": "desc"
      }
    }
  ]
}

But this gives result for featured items that match the category AND mapDetails.addressComponents.country

What i want is featured AND (category OR mapDetails.addressComponents.country)

TRIED 1

    {
  "from": 0,
  "size": 10,
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "featured": {
              "value": "true"
            }
          }
        }
      ],
      "should": [
        {
          "bool": {
            "filter": [
              {
                "nested": {
                  "path": "mapDetails.addressComponents.country",
                  "query": {
                    "term": {
                      "mapDetails.addressComponents.country.shortName": {
                        "value": "IN"
                      }
                    }
                  }
                }
              }
            ]
          }
        },
        {
          "bool": {
            "filter": [
              {
                "terms": {
                  "category.catId": [
                    "60c2c8c1031ee362a67e1316"
                    
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  },
  "sort": [
    {
      "featured": {
        "order": "desc"
      }
    },
    {
      "updatedAt": {
        "order": "desc"
      }
    }
  ]
}

Result :: it gives result which doesn't fall under the category or location

Can someone please point me in correct direction?

Thanks in Advance Any help is appreciated.

Upvotes: 1

Views: 754

Answers (1)

jaspreet chahal
jaspreet chahal

Reputation: 9109

You should use a should clause. Should works as an OR

{
   "from":0,
   "size":10,
   "query":{
      "bool":{
         "must":[
            {
               "term":{
                  "featured":{
                     "value":"true"
                  }
               }
            },
            {
               "bool":{
                  "should":[
                     {
                        "terms":{
                           "category.catId":[
                              "60c2c8c1031ee362a67e1316",
                              "609e1a546b66fe030d94f402",
                              "609e1ad46b66fe030d94f405",
                              "60b47e0cf511114e4a9107d8"
                           ]
                        }
                     },
                     {
                        "nested":{
                           "path":"mapDetails.addressComponents.country",
                           "query":{
                              "term":{
                                 "mapDetails.addressComponents.country.shortName":{
                                    "value":"IN"
                                 }
                              }
                           }
                        }
                     },
                     "minimum_should_match":1
                  ]
               }
            }
         ]
      }
   },
   "sort":[
      {
         "featured":{
            "order":"desc"
         }
      },
      {
         "updatedAt":{
            "order":"desc"
         }
      }
   ]
}

Upvotes: 1

Related Questions