Elasticsearch exception [type=search_phase_execution_exception, reason=all shards failed]
elastic search查询数组、列表
·

遇到一个问题,根据dsl语句,改成用 ElasticsearchRestTemplate调用,结果传list就会报错,传单个值不会报错
{
"query" : {
"bool" : {
"filter": [
{
"terms":{
"topic_ids":[22,12]
}
}
]
}
},
"aggs":{
"countNum":{
"terms":{
"field":"topic_ids"
}
}
}
}
看了半天,没看明白,别人说这个报错是因为值类型不对导致的
然后改用自己查出所有结果,然后自己聚合
Map<Long, Long> collect = objects.stream().map(EsMicroHeadline::getTopicIds).flatMap(List::stream)
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
这样确实也能解决问题,就是麻烦了一点
然后,想到了可以打印dsl的,拿着打印的dsl,看看到底有什么问题
nativeSearchQueryBuilder.build().getQuery()

然后放在kibana执行报错:[terms] query does not support [value] within lookup element

瞬间我就反应过来了,我应该用terms,而不是term
改了代码,果然对了!
总结:
term支持这些value

terms支持这些value

很明显,terms就是支持数组查询,term不支持数组查询
所以,下回es遇到问题,还是要打印出来dsl,基本就能解决问题了
更多推荐



所有评论(0)