normalizer.asciidoc 2.8 KB

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