analyze.asciidoc 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. [[indices-analyze]]
  2. === Analyze API
  3. ++++
  4. <titleabbrev>Analyze</titleabbrev>
  5. ++++
  6. Performs <<analysis,analysis>> on a text string
  7. and returns the resulting tokens.
  8. [source,console]
  9. --------------------------------------------------
  10. GET /_analyze
  11. {
  12. "analyzer" : "standard",
  13. "text" : "Quick Brown Foxes!"
  14. }
  15. --------------------------------------------------
  16. [[analyze-api-request]]
  17. ==== {api-request-title}
  18. `GET /_analyze`
  19. `POST /_analyze`
  20. `GET /<index>/_analyze`
  21. `POST /<index>/_analyze`
  22. [[analyze-api-path-params]]
  23. ==== {api-path-parms-title}
  24. `<index>`::
  25. +
  26. --
  27. (Optional, string)
  28. Index used to derive the analyzer.
  29. If specified,
  30. the `analyzer` or `<field>` parameter overrides this value.
  31. If no analyzer or field are specified,
  32. the analyze API uses the default analyzer for the index.
  33. If no index is specified
  34. or the index does not have a default analyzer,
  35. the analyze API uses the <<analysis-standard-analyzer,standard analyzer>>.
  36. --
  37. [[analyze-api-query-params]]
  38. ==== {api-query-parms-title}
  39. `analyzer`::
  40. +
  41. --
  42. (Optional, string or <<analysis-custom-analyzer,custom analyzer object>>)
  43. Analyzer used to analyze for the provided `text`.
  44. See <<analysis-analyzers>> for a list of built-in analyzers.
  45. You can also provide a <<analysis-custom-analyzer,custom analyzer>>.
  46. If this parameter is not specified,
  47. the analyze API uses the analyzer defined in the field's mapping.
  48. If no field is specified,
  49. the analyze API uses the default analyzer for the index.
  50. If no index is specified,
  51. or the index does not have a default analyzer,
  52. the analyze API uses the <<analysis-standard-analyzer,standard analyzer>>.
  53. --
  54. `attributes`::
  55. (Optional, array of strings)
  56. Array of token attributes used to filter the output of the `explain` parameter.
  57. `char_filter`::
  58. (Optional, array of strings)
  59. Array of character filters used to preprocess characters before the tokenizer.
  60. See <<analysis-charfilters>> for a list of character filters.
  61. `explain`::
  62. (Optional, boolean)
  63. If `true`, the response includes token attributes and additional details.
  64. Defaults to `false`.
  65. experimental:[The format of the additional detail information is labelled as experimental in Lucene and it may change in the future.]
  66. `field`::
  67. +
  68. --
  69. (Optional, string)
  70. Field used to derive the analyzer.
  71. To use this parameter,
  72. you must specify an index.
  73. If specified,
  74. the `analyzer` parameter overrides this value.
  75. If no field is specified,
  76. the analyze API uses the default analyzer for the index.
  77. If no index is specified
  78. or the index does not have a default analyzer,
  79. the analyze API uses the <<analysis-standard-analyzer,standard analyzer>>.
  80. --
  81. `filter`::
  82. (Optional, Array of strings)
  83. Array of token filters used to apply after the tokenizer.
  84. See <<analysis-tokenfilters>> for a list of token filters.
  85. `normalizer`::
  86. (Optional, string)
  87. Normalizer to use to convert text into a single token.
  88. See <<analysis-normalizers>> for a list of normalizers.
  89. `text`::
  90. (Required, string or array of strings)
  91. Text to analyze.
  92. If an array of strings is provided, it is analyzed as a multi-value field.
  93. `tokenizer`::
  94. (Optional, string)
  95. Tokenizer to use to convert text into tokens.
  96. See <<analysis-tokenizers>> for a list of tokenizers.
  97. [[analyze-api-example]]
  98. ==== {api-examples-title}
  99. [[analyze-api-no-index-ex]]
  100. ===== No index specified
  101. You can apply any of the built-in analyzers to the text string without
  102. specifying an index.
  103. [source,console]
  104. --------------------------------------------------
  105. GET /_analyze
  106. {
  107. "analyzer" : "standard",
  108. "text" : "this is a test"
  109. }
  110. --------------------------------------------------
  111. [[analyze-api-text-array-ex]]
  112. ===== Array of text strings
  113. If the `text` parameter is provided as array of strings, it is analyzed as a multi-value field.
  114. [source,console]
  115. --------------------------------------------------
  116. GET /_analyze
  117. {
  118. "analyzer" : "standard",
  119. "text" : ["this is a test", "the second text"]
  120. }
  121. --------------------------------------------------
  122. [[analyze-api-custom-analyzer-ex]]
  123. ===== Custom analyzer
  124. You can use the analyze API to test a custom transient analyzer built from
  125. tokenizers, token filters, and char filters. Token filters use the `filter`
  126. parameter:
  127. [source,console]
  128. --------------------------------------------------
  129. GET /_analyze
  130. {
  131. "tokenizer" : "keyword",
  132. "filter" : ["lowercase"],
  133. "text" : "this is a test"
  134. }
  135. --------------------------------------------------
  136. [source,console]
  137. --------------------------------------------------
  138. GET /_analyze
  139. {
  140. "tokenizer" : "keyword",
  141. "filter" : ["lowercase"],
  142. "char_filter" : ["html_strip"],
  143. "text" : "this is a <b>test</b>"
  144. }
  145. --------------------------------------------------
  146. deprecated[5.0.0, Use `filter`/`char_filter` instead of `filters`/`char_filters` and `token_filters` has been removed]
  147. Custom tokenizers, token filters, and character filters can be specified in the request body as follows:
  148. [source,console]
  149. --------------------------------------------------
  150. GET /_analyze
  151. {
  152. "tokenizer" : "whitespace",
  153. "filter" : ["lowercase", {"type": "stop", "stopwords": ["a", "is", "this"]}],
  154. "text" : "this is a test"
  155. }
  156. --------------------------------------------------
  157. [[analyze-api-specific-index-ex]]
  158. ===== Specific index
  159. You can also run the analyze API against a specific index:
  160. [source,console]
  161. --------------------------------------------------
  162. GET /analyze_sample/_analyze
  163. {
  164. "text" : "this is a test"
  165. }
  166. --------------------------------------------------
  167. // TEST[setup:analyze_sample]
  168. The above will run an analysis on the "this is a test" text, using the
  169. default index analyzer associated with the `analyze_sample` index. An `analyzer`
  170. can also be provided to use a different analyzer:
  171. [source,console]
  172. --------------------------------------------------
  173. GET /analyze_sample/_analyze
  174. {
  175. "analyzer" : "whitespace",
  176. "text" : "this is a test"
  177. }
  178. --------------------------------------------------
  179. // TEST[setup:analyze_sample]
  180. [[analyze-api-field-ex]]
  181. ===== Derive analyzer from a field mapping
  182. The analyzer can be derived based on a field mapping, for example:
  183. [source,console]
  184. --------------------------------------------------
  185. GET /analyze_sample/_analyze
  186. {
  187. "field" : "obj1.field1",
  188. "text" : "this is a test"
  189. }
  190. --------------------------------------------------
  191. // TEST[setup:analyze_sample]
  192. Will cause the analysis to happen based on the analyzer configured in the
  193. mapping for `obj1.field1` (and if not, the default index analyzer).
  194. [[analyze-api-normalizer-ex]]
  195. ===== Normalizer
  196. A `normalizer` can be provided for keyword field with normalizer associated with the `analyze_sample` index.
  197. [source,console]
  198. --------------------------------------------------
  199. GET /analyze_sample/_analyze
  200. {
  201. "normalizer" : "my_normalizer",
  202. "text" : "BaR"
  203. }
  204. --------------------------------------------------
  205. // TEST[setup:analyze_sample]
  206. Or by building a custom transient normalizer out of token filters and char filters.
  207. [source,console]
  208. --------------------------------------------------
  209. GET /_analyze
  210. {
  211. "filter" : ["lowercase"],
  212. "text" : "BaR"
  213. }
  214. --------------------------------------------------
  215. [[explain-analyze-api]]
  216. ===== Explain analyze
  217. If you want to get more advanced details, set `explain` to `true` (defaults to `false`). It will output all token attributes for each token.
  218. You can filter token attributes you want to output by setting `attributes` option.
  219. NOTE: The format of the additional detail information is labelled as experimental in Lucene and it may change in the future.
  220. [source,console]
  221. --------------------------------------------------
  222. GET /_analyze
  223. {
  224. "tokenizer" : "standard",
  225. "filter" : ["snowball"],
  226. "text" : "detailed output",
  227. "explain" : true,
  228. "attributes" : ["keyword"] <1>
  229. }
  230. --------------------------------------------------
  231. <1> Set "keyword" to output "keyword" attribute only
  232. The request returns the following result:
  233. [source,console-result]
  234. --------------------------------------------------
  235. {
  236. "detail" : {
  237. "custom_analyzer" : true,
  238. "charfilters" : [ ],
  239. "tokenizer" : {
  240. "name" : "standard",
  241. "tokens" : [ {
  242. "token" : "detailed",
  243. "start_offset" : 0,
  244. "end_offset" : 8,
  245. "type" : "<ALPHANUM>",
  246. "position" : 0
  247. }, {
  248. "token" : "output",
  249. "start_offset" : 9,
  250. "end_offset" : 15,
  251. "type" : "<ALPHANUM>",
  252. "position" : 1
  253. } ]
  254. },
  255. "tokenfilters" : [ {
  256. "name" : "snowball",
  257. "tokens" : [ {
  258. "token" : "detail",
  259. "start_offset" : 0,
  260. "end_offset" : 8,
  261. "type" : "<ALPHANUM>",
  262. "position" : 0,
  263. "keyword" : false <1>
  264. }, {
  265. "token" : "output",
  266. "start_offset" : 9,
  267. "end_offset" : 15,
  268. "type" : "<ALPHANUM>",
  269. "position" : 1,
  270. "keyword" : false <1>
  271. } ]
  272. } ]
  273. }
  274. }
  275. --------------------------------------------------
  276. <1> Output only "keyword" attribute, since specify "attributes" in the request.
  277. [[tokens-limit-settings]]
  278. ===== Setting a token limit
  279. Generating excessive amount of tokens may cause a node to run out of memory.
  280. The following setting allows to limit the number of tokens that can be produced:
  281. `index.analyze.max_token_count`::
  282. The maximum number of tokens that can be produced using `_analyze` API.
  283. The default value is `10000`. If more than this limit of tokens gets
  284. generated, an error will be thrown. The `_analyze` endpoint without a specified
  285. index will always use `10000` value as a limit. This setting allows you to control
  286. the limit for a specific index:
  287. [source,console]
  288. --------------------------------------------------
  289. PUT /analyze_sample
  290. {
  291. "settings" : {
  292. "index.analyze.max_token_count" : 20000
  293. }
  294. }
  295. --------------------------------------------------
  296. [source,console]
  297. --------------------------------------------------
  298. GET /analyze_sample/_analyze
  299. {
  300. "text" : "this is a test"
  301. }
  302. --------------------------------------------------
  303. // TEST[setup:analyze_sample]