specify-analyzer.asciidoc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. [[specify-analyzer]]
  2. === Specify an analyzer
  3. {es} offers a variety of ways to specify built-in or custom analyzers:
  4. * By `text` field, index, or query
  5. * For <<analysis-index-search-time,index or search time>>
  6. [TIP]
  7. .Keep it simple
  8. ====
  9. The flexibility to specify analyzers at different levels and for different times
  10. is great... _but only when it's needed_.
  11. In most cases, a simple approach works best: Specify an analyzer for each
  12. `text` field, as outlined in <<specify-index-field-analyzer>>.
  13. This approach works well with {es}'s default behavior, letting you use the same
  14. analyzer for indexing and search. It also lets you quickly see which analyzer
  15. applies to which field using the <<indices-get-mapping,get mapping API>>.
  16. If you don't typically create mappings for your indices, you can use
  17. <<indices-templates,index templates>> to achieve a similar effect.
  18. ====
  19. [[specify-index-time-analyzer]]
  20. ==== How {es} determines the index analyzer
  21. {es} determines which index analyzer to use by checking the following parameters
  22. in order:
  23. . The <<analyzer,`analyzer`>> mapping parameter for the field.
  24. See <<specify-index-field-analyzer>>.
  25. . The `analysis.analyzer.default` index setting.
  26. See <<specify-index-time-default-analyzer>>.
  27. If none of these parameters are specified, the
  28. <<analysis-standard-analyzer,`standard` analyzer>> is used.
  29. [[specify-index-field-analyzer]]
  30. ==== Specify the analyzer for a field
  31. When mapping an index, you can use the <<analyzer,`analyzer`>> mapping parameter
  32. to specify an analyzer for each `text` field.
  33. The following <<indices-create-index,create index API>> request sets the
  34. `whitespace` analyzer as the analyzer for the `title` field.
  35. [source,console]
  36. ----
  37. PUT my_index
  38. {
  39. "mappings": {
  40. "properties": {
  41. "title": {
  42. "type": "text",
  43. "analyzer": "whitespace"
  44. }
  45. }
  46. }
  47. }
  48. ----
  49. [[specify-index-time-default-analyzer]]
  50. ==== Specify the default analyzer for an index
  51. In addition to a field-level analyzer, you can set a fallback analyzer for
  52. using the `analysis.analyzer.default` setting.
  53. The following <<indices-create-index,create index API>> request sets the
  54. `simple` analyzer as the fallback analyzer for `my_index`.
  55. [source,console]
  56. ----
  57. PUT my_index
  58. {
  59. "settings": {
  60. "analysis": {
  61. "analyzer": {
  62. "default": {
  63. "type": "simple"
  64. }
  65. }
  66. }
  67. }
  68. }
  69. ----
  70. [[specify-search-analyzer]]
  71. ==== How {es} determines the search analyzer
  72. // tag::search-analyzer-warning[]
  73. [WARNING]
  74. ====
  75. In most cases, specifying a different search analyzer is unnecessary. Doing so
  76. could negatively impact relevancy and result in unexpected search results.
  77. If you choose to specify a separate search analyzer, we recommend you thoroughly
  78. <<test-analyzer,test your analysis configuration>> before deploying in
  79. production.
  80. ====
  81. // end::search-analyzer-warning[]
  82. At search time, {es} determines which analyzer to use by checking the following
  83. parameters in order:
  84. . The <<analyzer,`analyzer`>> parameter in the search query.
  85. See <<specify-search-query-analyzer>>.
  86. . The <<search-analyzer,`search_analyzer`>> mapping parameter for the field.
  87. See <<specify-search-field-analyzer>>.
  88. . The `analysis.analyzer.default_search` index setting.
  89. See <<specify-search-default-analyzer>>.
  90. . The <<analyzer,`analyzer`>> mapping parameter for the field.
  91. See <<specify-index-field-analyzer>>.
  92. If none of these parameters are specified, the
  93. <<analysis-standard-analyzer,`standard` analyzer>> is used.
  94. [[specify-search-query-analyzer]]
  95. ==== Specify the search analyzer for a query
  96. When writing a <<full-text-queries,full-text query>>, you can use the `analyzer`
  97. parameter to specify a search analyzer. If provided, this overrides any other
  98. search analyzers.
  99. The following <<search-search,search API>> request sets the `stop` analyzer as
  100. the search analyzer for a <<query-dsl-match-query,`match`>> query.
  101. [source,console]
  102. ----
  103. GET my_index/_search
  104. {
  105. "query": {
  106. "match": {
  107. "message": {
  108. "query": "Quick foxes",
  109. "analyzer": "stop"
  110. }
  111. }
  112. }
  113. }
  114. ----
  115. // TEST[s/^/PUT my_index\n/]
  116. [[specify-search-field-analyzer]]
  117. ==== Specify the search analyzer for a field
  118. When mapping an index, you can use the <<analyzer,`search_analyzer`>> mapping
  119. parameter to specify a search analyzer for each `text` field.
  120. If a search analyzer is provided, the index analyzer must also be specified
  121. using the `analyzer` parameter.
  122. The following <<indices-create-index,create index API>> request sets the
  123. `simple` analyzer as the search analyzer for the `title` field.
  124. [source,console]
  125. ----
  126. PUT my_index
  127. {
  128. "mappings": {
  129. "properties": {
  130. "title": {
  131. "type": "text",
  132. "analyzer": "whitespace",
  133. "search_analyzer": "simple"
  134. }
  135. }
  136. }
  137. }
  138. ----
  139. [[specify-search-default-analyzer]]
  140. ==== Specify the default search analyzer for an index
  141. When <<indices-create-index,creating an index>>, you can set a default search
  142. analyzer using the `analysis.analyzer.default_search` setting.
  143. If a search analyzer is provided, a default index analyzer must also be
  144. specified using the `analysis.analyzer.default` setting.
  145. The following <<indices-create-index,create index API>> request sets the
  146. `whitespace` analyzer as the default search analyzer for the `my_index` index.
  147. [source,console]
  148. ----
  149. PUT my_index
  150. {
  151. "settings": {
  152. "analysis": {
  153. "analyzer": {
  154. "default": {
  155. "type": "simple"
  156. },
  157. "default_search": {
  158. "type": "whitespace"
  159. }
  160. }
  161. }
  162. }
  163. }
  164. ----