如何根据国家和项目到期日期过滤 ElasticSearch 中的项目

How to filter items in ElasticSearch based on country and item expiry date

在我们的弹性搜索中,我们有来自不同国家的产品,每个产品都与 inventoryDate(添加到 ES 的日期)相关联。我的目标是根据国家及其库存日期过滤掉所有产品, 如果用户将查询过去 1 年的 es,那么将根据产品国家/地区过滤掉,假设国家/地区是 India 然后获取所有不早于 1 年,如果产品国家是新加坡 则获取所有不早于 6 个月 的产品

[{
 "productId": "01",
 "productName": "product 01",
 "productCountry": "India",
 "inventoryDate": "2017-08-17T02:29:03.617Z"
},{
 "productId": "02",
 "productName": "product 02",
 "productCountry": "India",
 "inventoryDate": "2017-02-20T02:29:03.617Z"
},{
 "productId": "03",
 "productName": "product 03",
 "productCountry": "Singapore",
 "inventoryDate": "2018-01-17T02:29:03.617Z"
},{
 "productId": "04",
 "productName": "product 04",
 "productCountry": "Singapore",
 "inventoryDate": "2017-08-17T02:29:03.617Z"
},{
 "productId": "05",
 "productName": "product 05",
 "productCountry": "China",
 "inventoryDate": "2018-01-01T02:29:03.617Z"
},{
 "productId": "06",
 "productName": "product 06",
 "productCountry": "China",
 "inventoryDate": "2017-12-10T02:29:03.617Z"
}]

在这种情况下,查询将 return me, ProductId 01, 03 and 05,

{
   "query": {
      "bool": {
         "should": [
            {
               "bool": {
                  "must": [
                     {
                        "term": {
                           "productCountry": "India"
                        }
                     },
                     {
                        "range": {
                           "inventoryDate": {
                              "gte": "now-1y/d"
                           }
                        }
                     }
                  ]
               }
            },
            {
               "bool": {
                  "must": [
                     {
                        "term": {
                           "productCountry": "Singapore"
                        }
                     },
                     {
                        "range": {
                           "inventoryDate": {
                              "gte": "now-6M/d"
                           }
                        }
                     }
                  ]
               }
            }
         ]
      }
   }

}