日期范围查询不适用于弹性 2.4 集群
Date range query not working on elastic 2.4 cluster
带日期范围查询的 Elasticsearch 查询不工作。
{
"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": {
"sales.date": {
"gte": "01-02-2020",
"lte": "08-03-2020"
}
}
}
]
}
}
}
您使用了错误的日期格式,正确的格式是 yyyy-mm-dd
,而您使用的是 dd-mm-yyyy
。请参阅 date data type and 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"
}
}
]
对我来说,范围查询适用于具有映射的非嵌套日期:
"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"
}
}
}
]
}
}
}
带日期范围查询的 Elasticsearch 查询不工作。
{
"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": {
"sales.date": {
"gte": "01-02-2020",
"lte": "08-03-2020"
}
}
}
]
}
}
}
您使用了错误的日期格式,正确的格式是 yyyy-mm-dd
,而您使用的是 dd-mm-yyyy
。请参阅 date data type and 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"
}
}
]
对我来说,范围查询适用于具有映射的非嵌套日期:
"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"
}
}
}
]
}
}
}