QueryBuilders.termsQuery

使用案例:

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"
        }  
      }  
    }  
  }  
}  

  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值