gpt4 book ai didi

java - Spring Data Elasticsearch 查询日期格式

转载 作者:行者123 更新时间:2023-11-30 08:41:19 25 4
gpt4 key购买 nike

我们正在使用 spring data elasticsearch 库来查询我们的 elasticsearch 服务器。我们目前正在使用 rest 调用来获取结果并且已经成功,但是我们想使用该库。

我们发送的工作休息查询类似于

{
"query": {
"bool": {
"must": [
{ "range" : { "startDateTime" : { "from" : "2016-01-31T00:00:00", "to" : "2016-02-01T00:00:00" }}},
{ "match_phrase" : { "keyword" : "task" }}
]
}
}
}

使用spring data elasicsearch查询推导工具我们创建了方法

findMessagesByKeywordAndStartDateTimeBetween(String keyword, String start, String end);

派生到查询

{
"from": 0,
"query": {
"bool": {
"must": [
{"query_string":{"query":"\"tasks\"","fields":["keyword"]}},
{"range":{"startDateTime":{"from":"2016-01-31T00:00:00","to":"2016-02-01T00:00:00","include_lower":true,"include_upper":true}}}
]
}
}
}

我可以在休息客户端中运行此查询并接收数据,但是,当库尝试查询数据库时,我收到错误

{
"timestamp": 1454360466934,
"status": 500,
"error": "Internal Server Error",
"exception": "org.elasticsearch.action.search.SearchPhaseExecutionException",
"message": "Failed to execute phase [query_fetch], all shards failed; shardFailures {
[██████████████████████][████████████][0]: RemoteTransportException[
[██████-██-███-███████][inet[/██.███.███.███:9300]]
[indices:data/read/search[phase/query+fetch]]]; nested: SearchParseException[
[████████████][0]: from[0],size[10]: Parse Failure [
Failed to parse source [
{\"from\":0,\"size\":10,\"query\":{\"bool\":{\"must\":[{\"query_string\":{\"query\":\"\"/tasks\"\",\"fields\":[\"method\"]}},{\"range\":{\"startDateTime\":{\"from\":\"2016-01-31T00:00:00.000Z\",\"to\":\"2016-02-01T00:00:00.000Z\",\"include_lower\":true,\"include_upper\":true}}}]}}}
]
]
];
nested: NumberFormatException[For input string: \"2016-01-31T00:00:00.000Z\"];
}",
"path": "/report/tasks"
}

这让我们相信我们要求的日期格式不正确,无法引用数据库项目,但样本结果看起来像

{
"_index": "████████████",
"_type": "████████████",
"_id": "████████████",
"_score": 0.000,
"_source": {
"keyword": "tasks",
"endDateTime": "2016-01-15T00:57:31.427Z",
"startDateTime": "2016-01-15T00:57:30.201Z",
"@timestamp": "2016-01-15T00:57:31+00:00",
"responseBody": "{...stuff goes here...}"
}
},...

因此您会认为您可以使用该格式进行查询。


我们决定尝试使用新查询获取所有带有 tasks 关键字的结果

findMessagesByKeyword(String keyword);

派生到

{
"from": 0,
"query": {
"bool": {
"must": [
{"query_string":{"query":"\"tasks\"","fields":["keyword"]}}
]
}
}
}

这将返回页面中的所有结果,并在将映射对象的 startDateTime 和 responseBody 字段打印到控制台之后

10: [
[Thu Oct 15 18:55:53 EDT 2015, {...stuff goes here...}]
[Thu Oct 15 18:56:38 EDT 2015, {...stuff goes here...}]
[Thu Oct 15 18:56:49 EDT 2015, {...stuff goes here...}]
[Thu Oct 15 18:58:59 EDT 2015, {...stuff goes here...}]
[Thu Oct 15 18:59:16 EDT 2015, {...stuff goes here...}]
[Thu Oct 15 18:59:33 EDT 2015, {...stuff goes here...}]
[Thu Oct 15 18:59:54 EDT 2015, {...stuff goes here...}]
[Thu Oct 15 19:00:02 EDT 2015, {...stuff goes here...}]
[Thu Oct 15 19:00:02 EDT 2015, {...stuff goes here...}]
[Thu Oct 15 19:00:11 EDT 2015, {...stuff goes here...}]
] //These are paged results, there are results as recently as last week, just not in this page

我注意到日期时间字段现在采用不同的格式,所以我使用的格式是

public String DATETIME_FORMAT = "EE MMM dd HH:mm:ss zz yyyy";

代替

public String DATETIME_FORMAT = "yyyy-MM-dd'T'00:00:00.000'Z'";

得到错误

NumberFormatException[For input string: \"Sun Jan 31 00:00:00 EST 2016\"]

  • 如果有帮助,该字段的映射是

    “开始日期时间”:{ “类型”:“日期”, “格式”:“日期可选时间” },

  • 我们尝试了很多格式和数据类型。当我们改变格式时到

    yyyMMddHHmmss

    我们不再收到错误,但没有得到任何结果。


在这一点上,我们知道我们一定做错了什么,但不确定去哪里。非常感谢所有帮助。


2016 年 2 月 2 日上午 10:15

感谢@Richa:将日期转换为长(以毫秒为单位)后,查询似乎可以运行,但没有返回任何结果。

这是在从昨天到今天的默认时间范围内运行的,并且在大约 10 天的手动时间范围内运行,我知道有大约 300 条记录。

我还能够使用当前的 rest 实现验证是否存在数据,并且我能够使用 rest 客户端进行三重检查,但没有用于 spring 数据实现的数据。

想法?

最佳答案

我在我的项目中也遇到了这个错误。使用 Long 数据类型解决了这个问题。您正在使用的 spring Data 提供的 dynamic finder 正在使用 String 中的 date 参数。将日期转换为毫秒并以 Long 形式传递日期。使用方法为:

findMessagesByKeywordAndStartDateTimeBetween(String keyword, Long start, Long end);

希望这对您有所帮助。

关于java - Spring Data Elasticsearch 查询日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35141047/

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