【问题标题】:How to filter items in ElasticSearch based on country and item expiry date如何根据国家和项目到期日期过滤 ElasticSearch 中的项目
【发布时间】:2018-09-04 02:31:24
【问题描述】:

在我们的弹性搜索中,我们有来自不同国家/地区的产品,每个产品都与 inventoryDate 相关联(添加到 ES 的日期)。我的目标是根据国家及其库存日期过滤掉所有产品, 如果用户查询 es,过去 1 年,则根据产品国家/地区将被过滤掉,假设国家是 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"
}]

在这种情况下,查询将返回我,ProductId 01、03 和 05,

【问题讨论】:

  • 使用带有外部 should 子句和内部 must 子句以及日期范围查询的 bool 查询
  • 感谢@sramalingam24 的回复,你能帮我写这个查询吗

标签: elasticsearch elasticsearch-plugin elasticsearch-5


【解决方案1】:
{
   "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"
                           }
                        }
                     }
                  ]
               }
            }
         ]
      }
   }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-18
    • 1970-01-01
    • 2014-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多