默认分词器是Standard 标准分词器,是不区分大小写的。
在进行数据存储时, 大写的英文字符会转换成小写。
但keyword类型属于精准匹配,没法实现大小写区分。
normalizer
官方解释
第一:normalizer是 keyword的一个属性,类似 analyzer分词器的功能,不同的地方在于:可以对 keyword生成的单一 Term再做进一步的处理。
第二:normalizer 在 keyword 类型数据索引化之前被使用,同时在 match 或者 term 类型检索阶段也能被使用。
使用方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| PUT index { "settings": { "analysis": { "normalizer": { "my_normalizer": { "type": "custom", "char_filter": [], "filter": ["lowercase"] } } } }, "mappings": { "properties": { "foo": { "type": "keyword", "normalizer": "my_normalizer" } } } } ------------------------------------------------------------------------------------------------------------------------------ PUT index {"index":{"_id":1}} { "city": "New York"} {"index":{"_id":2}} { "city": "new York"} {"index":{"_id":3}} { "city": "New york"} {"index":{"_id":4}} { "city": "NEW YORK"} {"index":{"_id":5}} { "city": "Seattle"} ------------------------------------------------------------------------------------------------------------------------------ GET index/_search { "size": 0, "aggs": { "cities": { "terms": { "field": "city" } } } }
``` "aggregations" : { "cities" : { "doc_count_error_upper_bound" : 0, "sum_other_doc_count" : 0, "buckets" : [ { "key" : "new york", "doc_count" : 4 }, { "key" : "seattle", "doc_count" : 1 } ] } } ```
|
这样一来无论是精准检索,模糊检索还是聚合都是不区分大小写的