Java 弹性搜索范围日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20705211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Elastic search range dates
提问by user3083022
I have created an Elastic search index from a Mongo database. The documents in Mongo have the following structure:
我从 Mongo 数据库创建了一个弹性搜索索引。Mongo 中的文档具有以下结构:
{
"_id" : ObjectId("525facace4b0c1f5e78753ea"),
"time" : ISODate("2013-10-17T09:23:56.131Z"),
"type" : "A",
"url" : "www.google.com",
"name" : "peter",
}
The index was created (apparently) without any problems. Now, I am trying to use Elastic Search to retrieve the documents in the index between two dates. I have read that I have to use range queries, but I have tried many times things like
创建索引(显然)没有任何问题。现在,我正在尝试使用弹性搜索来检索两个日期之间索引中的文档。我读过我必须使用范围查询,但我已经尝试了很多次,比如
MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("name", "peter").type(Type.PHRASE).minimumShouldMatch("99%");
LocalDateTime toLocal = new LocalDateTime(2013,12,18, 0, 0);
Date to = toLocal.toDate();
LocalDateTime fromLocal = new LocalDateTime(2013,12,17, 0, 0);
Date from = fromLocal.toDate();
RangeQueryBuilder queryDate = QueryBuilders.rangeQuery("time").to(to).from(from);
FilterBuilder filterDate = FilterBuilders.queryFilter(queryDate);
srb = esH.client.prepareSearch("my_index");
srb.setQuery(queryBuilder);
srb.setFilter(filterDate);
sr = srb.execute().actionGet();
and I get 0 hits although there should be many results. I have tried to enter strings instead of dates, but same results.
虽然应该有很多结果,但我得到了 0 次点击。我尝试输入字符串而不是日期,但结果相同。
When I perform a basic query without filters such as:
当我执行没有过滤器的基本查询时,例如:
MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("name", "peter").type(Type.PHRASE).minimumShouldMatch("99%");
SearchRequestBuilder srb = esH.client.prepareSearch("my_index");
rb.setQuery(queryBuilder);
SearchResponse sr = srb.execute().actionGet();
I get hits with that look like this:
我得到了这样的点击:
{
"_index" : "my_index",
"_type" : "type",
"_id" : "5280d3c2e4b05e95aa703e34",
"_score" : 1.375688, "_source" : {"type":["A"],"time":["Mon Nov 11 13:55:30 CET 2013"],"name":["peter"]}
}
Where the field time
does not have the format ISODate("2013-10-17T09:23:56.131Z")
anymore.
字段time
不再具有格式的地方ISODate("2013-10-17T09:23:56.131Z")
。
To sum up, what would be the Java code (and types) for querying between two dates (and times), taking into account the format?
总而言之,考虑到格式,用于在两个日期(和时间)之间进行查询的 Java 代码(和类型)是什么?
回答by Thomas
You are probably passing the wrong field name to the range query at this line:
您可能在这一行将错误的字段名称传递给范围查询:
RangeQueryBuilder queryDate = QueryBuilders.rangeQuery("time").to(to).from(from);
RangeQueryBuilder queryDate = QueryBuilders.rangeQuery("time").to(to).from(from);
It should probably be @timestamp
(or the field you're using to store your timestamp) instead of time
. Additionally it seems that there is no time
field in Elasticsearch for the example document you included. This also points to the issue that the time
field wasn't converted correctly from Mongo to Elasticsearch.
它可能应该是@timestamp
(或您用来存储时间戳的字段)而不是time
. 此外,time
您所包含的示例文档在 Elasticsearch 中似乎没有字段。这也指出了该time
字段未从 Mongo 正确转换为 Elasticsearch 的问题。
回答by Nikhil Kumar K
Can you try
你能试一下吗
FilterBuilders.rangeFilter("@timestamp").from("from time").to("toTime")
回答by Prateek Paranjpe
This will work -
这将工作 -
You can pass in Long timestamps to the gte and lte params.
您可以将 Long 时间戳传递给 gte 和 lte 参数。
QueryBuilders.rangeQuery("time").gte(startTime).lte(endTime);
QueryBuilders.rangeQuery("time").gte(startTime).lte(endTime);
Make sure to add an "L" at the end of the startTime and endTime, so that it knows its a long and not an int.
确保在 startTime 和 endTime 的末尾添加一个“L”,以便它知道它是 long 而不是 int。