normalizer.asciidoc 2.9 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,js]
  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. "_doc": {
  27. "properties": {
  28. "foo": {
  29. "type": "keyword",
  30. "normalizer": "my_normalizer"
  31. }
  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. // CONSOLE
  67. The above queries match documents 1 and 2 since `BÀR` is converted to `bar` at
  68. both index and query time.
  69. [source,js]
  70. ----------------------------
  71. {
  72. "took": $body.took,
  73. "timed_out": false,
  74. "_shards": {
  75. "total": 5,
  76. "successful": 5,
  77. "skipped" : 0,
  78. "failed": 0
  79. },
  80. "hits": {
  81. "total": 2,
  82. "max_score": 0.2876821,
  83. "hits": [
  84. {
  85. "_index": "index",
  86. "_type": "_doc",
  87. "_id": "2",
  88. "_score": 0.2876821,
  89. "_source": {
  90. "foo": "bar"
  91. }
  92. },
  93. {
  94. "_index": "index",
  95. "_type": "_doc",
  96. "_id": "1",
  97. "_score": 0.2876821,
  98. "_source": {
  99. "foo": "BÀR"
  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,js]
  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. // CONSOLE
  124. // TEST[continued]
  125. returns
  126. [source,js]
  127. ----------------------------
  128. {
  129. "took": 43,
  130. "timed_out": false,
  131. "_shards": {
  132. "total": 5,
  133. "successful": 5,
  134. "skipped" : 0,
  135. "failed": 0
  136. },
  137. "hits": {
  138. "total": 3,
  139. "max_score": 0.0,
  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",/]