[英]Elasticsearch range query date format for java not returning all the results
I have an elasticsearch index that contains fields with a timestamp in the format " dd/MM/yyyy HH:mm:ss " and a customer name.我有一个 elasticsearch 索引,其中包含格式为“ dd/MM/yyyy HH:mm:ss ”的时间戳字段和客户名称。 I need to delete records added to the index before a certain timestamp.
我需要删除某个时间戳之前添加到索引中的记录。 Using deletebyquery api for java I had the following code:
使用deletebyquery api 为 java 我有以下代码:
DeleteByQueryRequest request =
new DeleteByQueryRequest(index);
//request.setQuery(new TermQueryBuilder(customerKeywordField, customerName));
BoolQueryBuilder query = QueryBuilders.boolQuery()
.filter(QueryBuilders.termsQuery(customerKeywordField, customerName))
.filter(QueryBuilders.rangeQuery("createdDate.keyword").lte(timestamp));
request.setQuery(query);
try {
BulkByScrollResponse bulkResponse =
restHighLevelClient.deleteByQuery(request, RequestOptions.DEFAULT);
}
} catch (Exception e) {
//exception handling
}
and this was working as intended, however now that the dates are " 01/10/2021 ", it no longer returns records for " 29/09/2021 " or the like as part of results, so I assume it is taking date format as " MM/dd/yyyy " instead这是按预期工作的,但是现在日期是“ 01/10/2021 ”,它不再返回“ 29/09/2021 ”等的记录作为结果的一部分,所以我假设它采用日期格式改为“ MM/dd/yyyy ”
I tried setting format as我尝试将格式设置为
QueryBuilders.rangeQuery("createdDate.keyword").lte(timestamp).format("*dd/MM/yyyy HH:mm:ss*")
but that did not work either.但这也不起作用。 I have verified that the timestamp being passed is in the appropriate format and the timestamp on the record is in the correct format too, so I am at a loss.
我已经验证传递的时间戳格式正确,记录上的时间戳格式也正确,所以我不知所措。 If someone can help, I would appreciate it.
如果有人可以提供帮助,我将不胜感激。
EDIT: Mapping编辑:映射
{
"Index_x" : {
"mappings" : {
"properties" : {
//other fields
"createdDate" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"customer" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
Sample document:样本文件:
{
"_index": "Index_x",
"_type": "_doc",
"_id": "1632381612786",
"_score": 1,
"_source": {
"customer": "customer1",
"createdDate": "23/09/2021 12:49:44",
//other fields
},
"fields": {
"customer.keyword": [
"customer1"
"createdDate": [
"23/09/2021 12:49:44"
],
"createdDate.keyword": [
"23/09/2021 12:49:44"
]
"customer": [
"customer1"
]//other fields
}
}
The field createdDate.keyword
is a keyword
, not date
as shown in your mapping (see mapping types doc ):字段
createdDate.keyword
是一个keyword
,而不是映射中显示的date
(请参阅映射类型文档):
"createdDate" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
You should change the field type to date
.您应该将字段类型更改为
date
。
If you are using dynamic mapping , during indexing, the string field createdDate
is checked to see whether its contents match any of the date patterns in order to add it as a new date
field.如果您使用动态映射,在索引期间,将检查字符串字段
createdDate
以查看其内容是否与任何日期模式匹配,以便将其添加为新的date
字段。 It seems that your format is not supported by default.默认情况下似乎不支持您的格式。
To solve this, you could customize the dynamic_date_formats to support your own date format.要解决这个问题,您可以自定义 dynamic_date_formats以支持您自己的日期格式。
Note that range queries on keyword
fields are considered expensive and will not be executed by default, see docs .请注意,
keyword
字段的范围查询被认为是昂贵的,默认情况下不会执行,请参阅文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.