2016-12-30 03:36:10 -05:00
|
|
|
[[normalizer]]
|
|
|
|
=== `normalizer`
|
|
|
|
|
|
|
|
The `normalizer` property of <<keyword,`keyword`>> fields is similar to
|
|
|
|
<<analyzer,`analyzer`>> except that it guarantees that the analysis chain
|
|
|
|
produces a single token.
|
|
|
|
|
|
|
|
The `normalizer` is applied prior to indexing the keyword, as well as at
|
|
|
|
search-time when the `keyword` field is searched via a query parser such as
|
2019-06-21 11:53:01 -04:00
|
|
|
the <<query-dsl-match-query,`match`>> query or via a term-level query
|
2018-05-03 04:23:14 -04:00
|
|
|
such as the <<query-dsl-term-query,`term`>> query.
|
2016-12-30 03:36:10 -05:00
|
|
|
|
2019-09-06 11:31:13 -04:00
|
|
|
[source,console]
|
2016-12-30 03:36:10 -05:00
|
|
|
--------------------------------
|
2019-01-22 09:13:52 -05:00
|
|
|
PUT index
|
2016-12-30 03:36:10 -05:00
|
|
|
{
|
|
|
|
"settings": {
|
|
|
|
"analysis": {
|
|
|
|
"normalizer": {
|
|
|
|
"my_normalizer": {
|
|
|
|
"type": "custom",
|
|
|
|
"char_filter": [],
|
|
|
|
"filter": ["lowercase", "asciifolding"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"mappings": {
|
2019-01-22 09:13:52 -05:00
|
|
|
"properties": {
|
|
|
|
"foo": {
|
|
|
|
"type": "keyword",
|
|
|
|
"normalizer": "my_normalizer"
|
2016-12-30 03:36:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 11:47:53 -05:00
|
|
|
PUT index/_doc/1
|
2016-12-30 03:36:10 -05:00
|
|
|
{
|
|
|
|
"foo": "BÀR"
|
|
|
|
}
|
|
|
|
|
2017-12-14 11:47:53 -05:00
|
|
|
PUT index/_doc/2
|
2016-12-30 03:36:10 -05:00
|
|
|
{
|
|
|
|
"foo": "bar"
|
|
|
|
}
|
|
|
|
|
2017-12-14 11:47:53 -05:00
|
|
|
PUT index/_doc/3
|
2016-12-30 03:36:10 -05:00
|
|
|
{
|
|
|
|
"foo": "baz"
|
|
|
|
}
|
|
|
|
|
|
|
|
POST index/_refresh
|
|
|
|
|
2018-05-03 04:23:14 -04:00
|
|
|
GET index/_search
|
|
|
|
{
|
|
|
|
"query": {
|
|
|
|
"term": {
|
|
|
|
"foo": "BAR"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-30 03:36:10 -05:00
|
|
|
GET index/_search
|
|
|
|
{
|
|
|
|
"query": {
|
|
|
|
"match": {
|
|
|
|
"foo": "BAR"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------
|
|
|
|
|
2018-05-03 04:23:14 -04:00
|
|
|
The above queries match documents 1 and 2 since `BÀR` is converted to `bar` at
|
2016-12-30 03:36:10 -05:00
|
|
|
both index and query time.
|
|
|
|
|
2019-09-06 16:09:09 -04:00
|
|
|
[source,console-result]
|
2016-12-30 03:36:10 -05:00
|
|
|
----------------------------
|
|
|
|
{
|
|
|
|
"took": $body.took,
|
|
|
|
"timed_out": false,
|
|
|
|
"_shards": {
|
2018-05-14 12:22:35 -04:00
|
|
|
"total": 1,
|
|
|
|
"successful": 1,
|
2017-07-12 16:19:20 -04:00
|
|
|
"skipped" : 0,
|
2016-12-30 03:36:10 -05:00
|
|
|
"failed": 0
|
|
|
|
},
|
|
|
|
"hits": {
|
2018-12-05 13:49:06 -05:00
|
|
|
"total" : {
|
|
|
|
"value": 2,
|
|
|
|
"relation": "eq"
|
|
|
|
},
|
2018-05-14 12:22:35 -04:00
|
|
|
"max_score": 0.47000363,
|
2016-12-30 03:36:10 -05:00
|
|
|
"hits": [
|
|
|
|
{
|
|
|
|
"_index": "index",
|
2017-12-14 11:47:53 -05:00
|
|
|
"_type": "_doc",
|
2018-05-14 12:22:35 -04:00
|
|
|
"_id": "1",
|
|
|
|
"_score": 0.47000363,
|
2016-12-30 03:36:10 -05:00
|
|
|
"_source": {
|
2018-05-14 12:22:35 -04:00
|
|
|
"foo": "BÀR"
|
2016-12-30 03:36:10 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"_index": "index",
|
2017-12-14 11:47:53 -05:00
|
|
|
"_type": "_doc",
|
2018-05-14 12:22:35 -04:00
|
|
|
"_id": "2",
|
|
|
|
"_score": 0.47000363,
|
2016-12-30 03:36:10 -05:00
|
|
|
"_source": {
|
2018-05-14 12:22:35 -04:00
|
|
|
"foo": "bar"
|
2016-12-30 03:36:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----------------------------
|
|
|
|
// TESTRESPONSE[s/"took".*/"took": "$body.took",/]
|
|
|
|
|
|
|
|
Also, the fact that keywords are converted prior to indexing also means that
|
|
|
|
aggregations return normalized values:
|
|
|
|
|
2019-09-06 11:31:13 -04:00
|
|
|
[source,console]
|
2016-12-30 03:36:10 -05:00
|
|
|
----------------------------
|
|
|
|
GET index/_search
|
|
|
|
{
|
|
|
|
"size": 0,
|
|
|
|
"aggs": {
|
|
|
|
"foo_terms": {
|
|
|
|
"terms": {
|
|
|
|
"field": "foo"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-09 03:50:43 -04:00
|
|
|
----------------------------
|
2016-12-30 03:36:10 -05:00
|
|
|
// TEST[continued]
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
2019-09-06 16:09:09 -04:00
|
|
|
[source,console-result]
|
2016-12-30 03:36:10 -05:00
|
|
|
----------------------------
|
|
|
|
{
|
|
|
|
"took": 43,
|
|
|
|
"timed_out": false,
|
|
|
|
"_shards": {
|
2018-05-14 12:22:35 -04:00
|
|
|
"total": 1,
|
|
|
|
"successful": 1,
|
2017-07-12 16:19:20 -04:00
|
|
|
"skipped" : 0,
|
2016-12-30 03:36:10 -05:00
|
|
|
"failed": 0
|
|
|
|
},
|
|
|
|
"hits": {
|
2018-12-05 13:49:06 -05:00
|
|
|
"total" : {
|
|
|
|
"value": 3,
|
|
|
|
"relation": "eq"
|
|
|
|
},
|
2018-08-22 11:23:54 -04:00
|
|
|
"max_score": null,
|
2016-12-30 03:36:10 -05:00
|
|
|
"hits": []
|
|
|
|
},
|
|
|
|
"aggregations": {
|
|
|
|
"foo_terms": {
|
|
|
|
"doc_count_error_upper_bound": 0,
|
|
|
|
"sum_other_doc_count": 0,
|
|
|
|
"buckets": [
|
|
|
|
{
|
|
|
|
"key": "bar",
|
|
|
|
"doc_count": 2
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"key": "baz",
|
|
|
|
"doc_count": 1
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----------------------------
|
|
|
|
// TESTRESPONSE[s/"took".*/"took": "$body.took",/]
|