normalizer.asciidoc 3.1 KB

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