运行 两次连续 java 测试时出现 Elasticsearch NumberFormatException
Elasticsearch NumberFormatException when running two consecutive java tests
我在 class 中有两个测试,每个测试都包含以下查询:
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 等查询时,所有测试都会通过。
此外,如果我 运行 这两个查询在同一个方法中:不会抛出异常。
我认为不正确的缓存 initialization/cleanup 可能会导致该行为...但是,为什么其他查询也不会发生这种情况?
此外,在class的@After方法中我删除了所有索引(elasticsearchTemplate.deleteIndex(Article.class)),在@Before方法中我do/redo 批量索引和刷新。
我走错路了吗?我在这里错过了什么?
完整堆栈跟踪:
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.onFailure(TransportSearchTypeAction.java:184)
at org.elasticsearch.search.action.SearchServiceTransportAction.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;
....
}
由于异常抱怨 NumberFormatException
,您应该尝试将日期发送为 long(而不是 Date 对象),因为这是内部存储日期的方式。请参阅我在下面的代码中调用 date.getTime()
:
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(matchAllQuery())
.withFilter(rangeFilter("publishDate").lt(date.getTime())).build();
我也找到了其他解决方案。
在 @Before
方法中,我没有应用显式映射,这意味着未考虑 Article
索引中的 @Field
注释。实际上,使用了默认映射。
通过添加显式映射:
elasticsearchTemplate.putMapping(Article.class)
publishDate
的 type
取自索引定义:
@Field(type = FieldType.Date, store = true)
private Date publishDate;
通过这种方式,所有测试都通过了,无论是 运行 它们是单独的还是全部的。
此外,rangeFilter
参数也适用于初始 date type
。 (当然,它也适用于 long type
。)
我在 class 中有两个测试,每个测试都包含以下查询:
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 等查询时,所有测试都会通过。
此外,如果我 运行 这两个查询在同一个方法中:不会抛出异常。
我认为不正确的缓存 initialization/cleanup 可能会导致该行为...但是,为什么其他查询也不会发生这种情况?
此外,在class的@After方法中我删除了所有索引(elasticsearchTemplate.deleteIndex(Article.class)),在@Before方法中我do/redo 批量索引和刷新。
我走错路了吗?我在这里错过了什么?
完整堆栈跟踪:
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.onFailure(TransportSearchTypeAction.java:184)
at org.elasticsearch.search.action.SearchServiceTransportAction.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;
....
}
由于异常抱怨 NumberFormatException
,您应该尝试将日期发送为 long(而不是 Date 对象),因为这是内部存储日期的方式。请参阅我在下面的代码中调用 date.getTime()
:
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(matchAllQuery())
.withFilter(rangeFilter("publishDate").lt(date.getTime())).build();
我也找到了其他解决方案。
在 @Before
方法中,我没有应用显式映射,这意味着未考虑 Article
索引中的 @Field
注释。实际上,使用了默认映射。
通过添加显式映射:
elasticsearchTemplate.putMapping(Article.class)
publishDate
的 type
取自索引定义:
@Field(type = FieldType.Date, store = true)
private Date publishDate;
通过这种方式,所有测试都通过了,无论是 运行 它们是单独的还是全部的。
此外,rangeFilter
参数也适用于初始 date type
。 (当然,它也适用于 long type
。)