normalizer.asciidoc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. [[normalizer]]
  2. === `normalizer`
  3. The `normalizer` property of <<keyword,`keyword`>> fields is similar to
  4. <<analyzer,`analyzer`>> except that it guarantees that the analysis chain
  5. produces a single token.
  6. The `normalizer` is applied prior to indexing the keyword, as well as at
  7. search-time when the `keyword` field is searched via a query parser such as
  8. the <<query-dsl-match-query,`match`>> query or via a term-level query
  9. such as the <<query-dsl-term-query,`term`>> query.
  10. [source,console]
  11. --------------------------------
  12. PUT index
  13. {
  14. "settings": {
  15. "analysis": {
  16. "normalizer": {
  17. "my_normalizer": {
  18. "type": "custom",
  19. "char_filter": [],
  20. "filter": ["lowercase", "asciifolding"]
  21. }
  22. }
  23. }
  24. },
  25. "mappings": {
  26. "properties": {
  27. "foo": {
  28. "type": "keyword",
  29. "normalizer": "my_normalizer"
  30. }
  31. }
  32. }
  33. }
  34. PUT index/_doc/1
  35. {
  36. "foo": "BÀR"
  37. }
  38. PUT index/_doc/2
  39. {
  40. "foo": "bar"
  41. }
  42. PUT index/_doc/3
  43. {
  44. "foo": "baz"
  45. }
  46. POST index/_refresh
  47. GET index/_search
  48. {
  49. "query": {
  50. "term": {
  51. "foo": "BAR"
  52. }
  53. }
  54. }
  55. GET index/_search
  56. {
  57. "query": {
  58. "match": {
  59. "foo": "BAR"
  60. }
  61. }
  62. }
  63. --------------------------------
  64. The above queries match documents 1 and 2 since `BÀR` is converted to `bar` at
  65. both index and query time.
  66. [source,console-result]
  67. ----------------------------
  68. {
  69. "took": $body.took,
  70. "timed_out": false,
  71. "_shards": {
  72. "total": 1,
  73. "successful": 1,
  74. "skipped" : 0,
  75. "failed": 0
  76. },
  77. "hits": {
  78. "total" : {
  79. "value": 2,
  80. "relation": "eq"
  81. },
  82. "max_score": 0.4700036,
  83. "hits": [
  84. {
  85. "_index": "index",
  86. "_id": "1",
  87. "_score": 0.4700036,
  88. "_source": {
  89. "foo": "BÀR"
  90. }
  91. },
  92. {
  93. "_index": "index",
  94. "_id": "2",
  95. "_score": 0.4700036,
  96. "_source": {
  97. "foo": "bar"
  98. }
  99. }
  100. ]
  101. }
  102. }
  103. ----------------------------
  104. // TESTRESPONSE[s/"took".*/"took": "$body.took",/]
  105. Also, the fact that keywords are converted prior to indexing also means that
  106. aggregations return normalized values:
  107. [source,console]
  108. ----------------------------
  109. GET index/_search
  110. {
  111. "size": 0,
  112. "aggs": {
  113. "foo_terms": {
  114. "terms": {
  115. "field": "foo"
  116. }
  117. }
  118. }
  119. }
  120. ----------------------------
  121. // TEST[continued]
  122. returns
  123. [source,console-result]
  124. ----------------------------
  125. {
  126. "took": 43,
  127. "timed_out": false,
  128. "_shards": {
  129. "total": 1,
  130. "successful": 1,
  131. "skipped" : 0,
  132. "failed": 0
  133. },
  134. "hits": {
  135. "total" : {
  136. "value": 3,
  137. "relation": "eq"
  138. },
  139. "max_score": null,
  140. "hits": []
  141. },
  142. "aggregations": {
  143. "foo_terms": {
  144. "doc_count_error_upper_bound": 0,
  145. "sum_other_doc_count": 0,
  146. "buckets": [
  147. {
  148. "key": "bar",
  149. "doc_count": 2
  150. },
  151. {
  152. "key": "baz",
  153. "doc_count": 1
  154. }
  155. ]
  156. }
  157. }
  158. }
  159. ----------------------------
  160. // TESTRESPONSE[s/"took".*/"took": "$body.took",/]