gpt4 book ai didi

elasticsearch - ElasticSearch中2个不同字段之间的日期范围

转载 作者:行者123 更新时间:2023-12-03 00:50:25 26 4
gpt4 key购买 nike

我想根据开始日期和结束日期在两个日期之间获取文档。

这是映射:

PUT data 
{
"mappings": {
"_doc": {
"properties": {
"product_code": {"type": "keyword"},
"color_code": {"type": "keyword"},
"warehouse_id": {"type": "short"},
"stock": {"type": "float"},
"inventory_start_date": {
"type": "date",
"format": "yyyy-MM-dd"
},
"inventory_end_date": {
"type": "date",
"format": "yyyy-MM-dd"
}
}
}
}
}

这是我的数据列表:
POST _bulk
{ "index" : { "_index" : "data", "_type" : "_doc" } }
{ "product_code" : "20001", "color_code" : "001", "warehouse_id" : 5, "stock" : 10,"inventory_start_date" : "2019-01-01","inventory_end_date" : "2019-01-04"}
{ "index" : { "_index" : "data", "_type" : "_doc" } }
{ "product_code" : "20001", "color_code" : "001", "warehouse_id" : 5, "stock" : 4, "inventory_start_date" : "2019-01-04","inventory_end_date" : "2019-01-07"}
{ "index" : { "_index" : "data", "_type" : "_doc" } }
{ "product_code" : "20001", "color_code" : "001", "warehouse_id" : 5, "stock" : 0, "inventory_start_date" : "2019-01-07","inventory_end_date" : "2019-01-07"}

库存开始日期和库存结束日期将库存量保持在2个日期之间。

问题是:我该如何在2个日期范围之间获取数据。例如;在2019-01-05和2019-01-06之间获取文档。或者,在2019-01-01至2019-01-03之间获取文档。

最佳答案

考虑到您提到的文档,如果我想要带有 list 的文档列表,例如在2019-01-012019-01-04之间,然后我将查询扩展为如下,并在包含在range queries子句中的两个不同字段上使用bool must实现它。

  • inventory_start_date-在2019-01-01和2019-01-04之间
  • inventory_end_date-在2019-01-01和2019-01-04之间

  • 询问
    POST myindex/_search
    {
    "query": {
    "bool": {
    "must": [
    {
    "range": {
    "inventory_start_date": {
    "gte": "2019-01-01",
    "lte": "2019-01-04"
    }
    }
    },
    {
    "range": {
    "inventory_end_date": {
    "gte": "2019-01-01",
    "lte": "2019-01-04"
    }
    }
    }
    ]
    }
    }
    }

    响应
    {
    "took" : 1,
    "timed_out" : false,
    "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
    },
    "hits" : {
    "total" : 1,
    "max_score" : 2.0,
    "hits" : [
    {
    "_index" : "myindex",
    "_type" : "_doc",
    "_id" : "1",
    "_score" : 2.0,
    "_source" : {
    "product_code" : "20001",
    "color_code" : "001",
    "warehouse_id" : 5,
    "stock" : 10,
    "inventory_start_date" : "2019-01-01",
    "inventory_end_date" : "2019-01-04"
    }
    }
    ]
    }
    }

    同样,对于任何其他不同的日期范围,您都需要相应地构造查询。

    让我知道是否有帮助。

    关于elasticsearch - ElasticSearch中2个不同字段之间的日期范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54572488/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com