我想根据开始日期和结束日期在两个日期之间获取文档。
这是映射:
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"}
inventory_start_date和inventory_end_date将库存量保持在2个日期之间。
这里的问题是:如何在2个日期范围之间获取数据。例如;在2019-01-05和2019-01-06之间获取文档。或者,在2019-01-01至2019-01-03之间获取文档。
答案 0 :(得分:2)
考虑到您提到的文件,如果我想要带有清单的文件清单,例如在2019-01-01
和2019-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"
}
}
]
}
}
同样,对于任何其他不同的日期范围,您都需要相应地构造查询。
让我知道是否有帮助。