首页 > 解决方案 > 弹性搜索 - 过滤多个 OR 条件的条件

问题描述

您好,我是 Elastic Search 的新手,很难理解它是如何工作的。假设我在弹性搜索服务器中有 20 种产品。每个产品对象看起来像这样

 {
 "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
        }
      ]
 }
]
}
}

现在我需要实现的是根据日期对它们进行排序,然后使用对象中的特征标记从中过滤掉特征。然后使用多个 OR 条件从该过滤器中

我尝试的是

     {
       "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"
      }
    }
  ]
}

但这会为匹配类别和 mapDetails.addressComponents.country 的特色项目提供结果

我想要的是特色和(类别或 mapDetails.addressComponents.country)

试过 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"
      }
    }
  ]
}

结果 :: 它给出的结果不属于类别或位置

有人可以指出我正确的方向吗?

提前致谢 任何帮助表示赞赏。

标签: elasticsearch

解决方案


您应该使用 should 子句。应该作为 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"
         }
      }
   ]
}

推荐阅读