1,问题:
当我们使用QueryBuilders.termQuery方法对指定的字段进行精准查询时,如果查询的条件为英文字母时,是没有问题的
当查询条件为中文时,则可能会查询不出数据,如下:当algorithmTag为中文时,即使es中有对应的记录,也是查询不出来的
@Override
public List<ESXTopicIndex> listByAlgorithmTag(String algorithmTag, int from, int size, int status) {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
.must(QueryBuilders.termQuery(Constant.XTopicIndexFields.FIELD_ALGORITHM_TAG, algorithmTag));
boolQueryBuilder.must(QueryBuilders.termQuery(XTopicConstant.FIELD_STATUS, status));
searchSourceBuilder.sort(XTopicIndexFields.FIELD_TOPIC_PUBLISH_TIME, SortOrder.DESC);
searchSourceBuilder.from(from).size(size).postFilter(boolQueryBuilder);
return query(searchSourceBuilder.toString());
}
附:依赖的es版本:
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.3.0</version>
</dependency>
2,解决方法: 在查询条件的关键词后拼接".keyword" 变为如下即可实现精准匹配:
@Override
public List<ESXTopicIndex> listByAlgorithmTag(String algorithmTag, int from, int size, int status) {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
.must(QueryBuilders.termQuery(XTopicIndexFields.FIELD_ALGORITHM_TAG + ".keyword", algorithmTag));
boolQueryBuilder.must(QueryBuilders.termQuery(XTopicConstant.FIELD_STATUS, status));
searchSourceBuilder.sort(XTopicIndexFields.FIELD_TOPIC_PUBLISH_TIME, SortOrder.DESC);
searchSourceBuilder.from(from).size(size).postFilter(boolQueryBuilder);
return query(searchSourceBuilder.toString());
}
3,注意:文章来源:https://www.toymoban.com/news/detail-509082.html
网上有说使用matchPhraseQuery代替termQuery使用可以查询出来,问题是:虽然使用matchPhraseQuery可以查询出来,但是查询出来的内容会比精准查询的数据要多,因为它并不是严格的精准查询的,而是将查询条件作为一个整体(不进行分词)然后进行模糊查询的文章来源地址https://www.toymoban.com/news/detail-509082.html
到了这里,关于spring boot es 支持中文精准查询的方法QueryBuilders.termQuery的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!