Elasticsearch NumberFormatException when running two consecutive java tests

时间:2015-06-15 14:16:43

标签: java date elasticsearch numberformatexception spring-data-elasticsearch

I have two test in a class, each of them containing the following query:

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

In one of the tests, the number of the results elasticsearchTemplate.count(searchQuery, Article.class), in the other one the returned values are verified elasticsearchTemplate.queryForPage(searchQuery,Article.class)

If I run any of these two tests separately, the tests always pass, everything seems to work perfectly. If I run both of the two tests consequently, one after the other, the first one passes, the other one fails with a SearchPhaseExecutionException: Failed to execute phase ... nested: NumberFormatException[For input string: "2015-02-01T00:02:02.396Z"]...

It's even more strange, that this behavior appears just when a rangefilter for publishDate (having type: FieldType.Date) is applied. When other similar queries with boolFilter, termFilter etc are applied consequently, all the tests pass.

Also, if I run these two queries inside the same method: no exception is thrown.

I thought that an improper cache initialization/cleanup might cause the behavior... but still, why does not happen to the other queries as well?

Also, in the @After method of the class I delete all the indexes (elasticsearchTemplate.deleteIndex(Article.class)), and in the @Before method I do/redo bulk indexing and refresh.

Am I on the wrong path? What am I missing here?

The full stack trace:

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)

The mapping for the article index:

@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;
....
}

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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

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

添加显式映射:

elasticsearchTemplate.putMapping(Article.class)

type publishDate取自索引定义:

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

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

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