0

我在一个类中有两个测试,每个测试都包含以下查询:

SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).withFilter(rangeFilter("publishDate").lt(date)).build();

在其中一项测试中,结果的数量elasticsearchTemplate.count(searchQuery, Article.class),在另一项中,验证返回值elasticsearchTemplate.queryForPage(searchQuery,Article.class)

如果我分别运行这两个测试中的任何一个,测试总是通过,一切似乎都很完美。 如果我因此同时运行这两个测试,一个接一个,第一个通过,另一个失败并出现SearchPhaseExecutionException:无法执行阶段...嵌套:NumberFormatException [对于输入字符串:“2015-02-01T00: 02:02.396Z"]...

更奇怪的是,这种行为仅在应用了 publishDate 的范围过滤器(具有类型:FieldType.Date)时才会出现。因此,当使用 boolFilter、termFilter 等其他类似查询时,所有测试都通过。

此外,如果我在同一个方法中运行这两个查询:不会引发异常。

我认为不正确的缓存初始化/清理可能会导致这种行为......但是,为什么其他查询也不会发生?

此外,在类的 @After 方法中,我删除了所有索引 (elasticsearchTemplate.deleteIndex(Article.class)),在 @Before 方法中,我执行/重做批量索引和刷新。

我走错路了吗?我在这里想念什么?

完整的堆栈跟踪:

org.elasticsearch.action.search.SearchPhaseExecutionException: Failed to execute phase [dfs], all shards failed; shardFailures {[jCBsPj2yR3qkX6HxN_xr4w][articles][0]: SearchParseException[[articles][0]: query[ConstantScore(*:*)],from[0],size[10]: Parse Failure [Failed to parse source [{"from":0,"size":10,"query":{"match_all":{}},"post_filter":{"range":{"publishDate":{"from":null,"to":"2015-02-01T00:02:02.676Z","include_lower":true,"include_upper":false}}}}]]]; nested: NumberFormatException[For input string: "2015-02-01T00:02:02.676Z"]; }{[jCBsPj2yR3qkX6HxN_xr4w][articles][1]: SearchParseException[[articles][1]: query[ConstantScore(*:*)],from[0],size[10]: Parse Failure [Failed to parse source [{"from":0,"size":10,"query":{"match_all":{}},"post_filter":{"range":{"publishDate":{"from":null,"to":"2015-02-01T00:02:02.676Z","include_lower":true,"include_upper":false}}}}]]]; nested: NumberFormatException[For input string: "2015-02-01T00:02:02.676Z"]; }{[jCBsPj2yR3qkX6HxN_xr4w][articles][2]: SearchParseException[[articles][2]: query[ConstantScore(*:*)],from[0],size[10]: Parse Failure [Failed to parse source [{"from":0,"size":10,"query":{"match_all":{}},"post_filter":{"range":{"publishDate":{"from":null,"to":"2015-02-01T00:02:02.676Z","include_lower":true,"include_upper":false}}}}]]]; nested: NumberFormatException[For input string: "2015-02-01T00:02:02.676Z"]; }{[jCBsPj2yR3qkX6HxN_xr4w][articles][3]: SearchParseException[[articles][3]: query[ConstantScore(*:*)],from[0],size[10]: Parse Failure [Failed to parse source [{"from":0,"size":10,"query":{"match_all":{}},"post_filter":{"range":{"publishDate":{"from":null,"to":"2015-02-01T00:02:02.676Z","include_lower":true,"include_upper":false}}}}]]]; nested: NumberFormatException[For input string: "2015-02-01T00:02:02.676Z"]; }{[jCBsPj2yR3qkX6HxN_xr4w][articles][4]: SearchParseException[[articles][4]: query[ConstantScore(*:*)],from[0],size[10]: Parse Failure [Failed to parse source [{"from":0,"size":10,"query":{"match_all":{}},"post_filter":{"range":{"publishDate":{"from":null,"to":"2015-02-01T00:02:02.676Z","include_lower":true,"include_upper":false}}}}]]]; nested: NumberFormatException[For input string: "2015-02-01T00:02:02.676Z"]; }
at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.onFirstPhaseResult(TransportSearchTypeAction.java:238)
at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction$1.onFailure(TransportSearchTypeAction.java:184)
at org.elasticsearch.search.action.SearchServiceTransportAction$23.run(SearchServiceTransportAction.java:565)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)

文章索引的映射:

@Document(indexName = "articles", type = "article", shards = 1, replicas = 0, refreshInterval = "-1", indexStoreType = "memory")
public class Article {
@Id
private Long id;
@Field(type = FieldType.String, store = true)
private String title;
@Field(type = FieldType.String, store = true)
private String text;
@Field(type = FieldType.String, store = true)
private String author;
@Field(type = FieldType.Date, store = true)
private Date publishDate;
@Field(type = FieldType.Date, store = true)
private Date lastModificationDate;
....
}
4

2 回答 2

0

由于异常抱怨 a NumberFormatException,您应该尝试将日期作为 long (而不是 Date 对象)发送,因为这是内部存储日期的方式。请参阅我date.getTime()在下面的代码中调用:

SearchQuery searchQuery = new NativeSearchQueryBuilder()
    .withQuery(matchAllQuery())
    .withFilter(rangeFilter("publishDate").lt(date.getTime())).build();
于 2015-06-16T07:49:58.680 回答
0

我也找到了另一个解决方案。

@Before方法中,我没有应用显式映射,这意味着不考虑索引中的@Field注释。Article实际上,使用了默认映射。

通过添加显式映射:

elasticsearchTemplate.putMapping(Article.class)

typeforpublishDate取自索引定义:

@Field(type = FieldType.Date, store = true)
private Date publishDate;

通过这种方式,所有测试都通过了,无论是单独运行它们还是按顺序运行它们。

此外,该rangeFilter参数也适用于初始参数date type。(当然,它也适用long type。)

于 2015-06-16T12:14:55.793 回答