elasticsearch 分词处理
elasticsearch字符串在默认情况下,存储时会被analyze(默认是标准分析器),这个过程会将字符串进行分词动作,这对于数据分析需求来说是不必须要的,对于性能来说也是有损耗的,为此,我们需要将对应索引中的所有的字符串值设置成not_analyzed,这可以通过动态模板来完成:
1.创建 不分词的string字段
curl -XPUT localhost:9200/索引名称/类型名称/_mapping?pretty -d '{"类型名称":{"properties":{"字段名称":{"type":"字段类型","store":"是否存储","index":"索引方式、是否分析"}}}}'
# 例子
curl -XPUT localhost:9200/logstash/logstash/_mapping?pretty -d '{"message":{"properties":{"str1":{"type":"string","index":"not_analyzed"}}}}'
2,在 analyzed字符串中进行查询
curl -XPOST localhost:9200/logstash/_search?pretty -d '{"query":{"term":{"str2":"world"}}}'
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 0.5945348,
"hits" : [ {
"_index" : "abc",
"_type" : "abc",
"_id" : "AVM2vbbqJmh5lL1r79nw",
"_score" : 0.5945348,
"_source":{"str1":"hello","str2":"world"}
}, {
"_index" : "abc",
"_type" : "abc",
"_id" : "AVM2vRQgJmh5lL1r79nv",
"_score" : 0.37158427,
"_source":{"str1":"hello, world!","str2":"goodbye! world"}
} ]
}
}
3.不分析的String如何查询
如果字段是不分词的,而查询的是这个字段里面的一个词,那么使用term时无法查询到目标文档的。