使用案例:
private List 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"
}
}
}
}
}