QueryBuilders.termsQuery 蔚落 2022-10-12 15:58 170阅读 0赞 使用案例: private List<Integer> doctorIds; // id匹配多个值 tempQueryBuilder.must(QueryBuilders.termsQuery("_id", doctorIds)); 最近在做es6.x 查询,有一个业务涉及到 类似sql in 查询功能。日志格式是自定义格式,按照某一个字段使用termsQuery 查询时,结果为空 日志格式 \{"host\_name":"VM-TR73PO26-DB","time":"2018-12-09", ...\} BoolQueryBuilder boolQuery = QueryBuilders.boolQuery(); boolQuery.must(QueryBuilders.termsQuery("host_name", hostList)); //hostList 是List 集合。 SearchResponse searchResponse = searchRequestBuilder.execute().actionGet(); 返回结果为空。定位原因发现是应该 termsQuery进行了分词导致的 处理方法1: 在出现分词查询,key 添加**keyword**,**只适用于es6,加上keyword就不会进行分词了** boolQuery.must(QueryBuilders.termsQuery("**host\_name.keyword**", hostList)); 处理方法2: 可以修改 es 或者logstash 分词规则。比较好方式修改 es 映射规则 没有权限修改es 。 只能修改logstash 映射模板。配置如下: output \{ elasticsearch \{ hosts => "localhost:9200" index => "my\_index" template => "/data1/cloud/logstash-5.5.1/filebeat-template.json" //模板映射文件 template\_name => "my\_index" template\_overwrite => true //覆盖原有模板 \} \} 至此已完成模板替换。 filebeat-template.json 格式如下: \{ "template" : "索引", "order":1 //设置 > 0 值, 执行从大到小映射模板 "settings" : \{ "index.number\_of\_shards": 1, "number\_of\_replicas": 1 \}, "mappings" : \{ "\_default\_" : \{ "\_all" : \{"enabled" : true, "omit\_norms" : true\}, "dynamic\_templates" : \[ \{ "message\_field" : \{ "match" : "message", "match\_mapping\_type" : "string", "mapping" : \{ "type" : "string", "index" : "not\_analyzed", "omit\_norms" : true, "fielddata" : \{ "format" : "disabled" \} \} \} \}, \{ "string\_fields" : \{ "match" : "\*", "match\_mapping\_type" : "string", "mapping" : \{ "type" : "string", "index" : "not\_analyzed", "doc\_values" : true \} \} \} \], "properties" : \{ "@timestamp" : \{ "type" : "string" \}, "health\_time" : \{ "type" : "string" \}, "host\_name" : \{ "type" : "string", "index": "not\_analyzed" \}, "tags" : \{ "type" : "string" \}, "type" : \{ "type" : "string" \} \} \} \} \}
还没有评论,来说两句吧...