带有日期范围查询的Elasticsearch查询无法正常工作。
FROM
答案 0 :(得分:0)
您使用了错误的日期格式,而使用yyyy-mm-dd
时正确的日期格式是dd-mm-yyyy
。有关更多信息,请参考date data type和date range query ES文档。
正确的示例
{
"mappings": {
"my_type": {
"properties": {
"sales_date": {
"type": "date"
}
}
}
}
}
{
"sales_date" : "01-02-2020"
}
{
"sales_date" : "2020-02-05"
}
{
"sales_date" : "2020-03-08"
}
{
"sales_date" : "2020-03-15"
}
date range query
{
"query": {
"range" : {
"sales_date" : {
"gte" : "2020-02-01", --> NOTE DIFFERENCE
"lte" : "2020-03-01"
}
}
}
}
"hits": [
{
"_index": "so-60584496",
"_type": "my_type",
"_id": "1",
"_score": 1.0,
"_source": {
"sales_date": "2020-02-01"
}
},
{
"_index": "so-60584496",
"_type": "my_type",
"_id": "2",
"_score": 1.0,
"_source": {
"sales_date": "2020-02-05"
}
}
]
答案 1 :(得分:0)
对我来说,范围查询适用于具有映射的非嵌套日期:
"mappings": {
"_default_": {
"properties": {
"date": {
"type": "date",
"format": "MM-dd-yyyy"
}
}
}}
工作查询:
{
"from": 0,
"size": 10,
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "100",
"fields": [
"nodeId"
],
"default_operator": "and"
}
},
{
"query_string": {
"query": "DAILY",
"fields": [
"aggLevel"
],
"default_operator": "and"
}
},
{
"query_string": {
"query": "23",
"fields": [
"replId"
],
"default_operator": "and"
}
},
{
"range": {
"date": {
"gte": "01-02-2020",
"lte": "08-03-2020"
}
}
}
]
}
}
}