analyze.asciidoc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. [[indices-analyze]]
  2. == Analyze
  3. Performs the analysis process on a text and return the tokens breakdown
  4. of the text.
  5. Can be used without specifying an index against one of the many built in
  6. analyzers:
  7. [source,js]
  8. --------------------------------------------------
  9. curl -XGET 'localhost:9200/_analyze' -d '
  10. {
  11. "analyzer" : "standard",
  12. "text" : "this is a test"
  13. }'
  14. --------------------------------------------------
  15. If text parameter is provided as array of strings, it is analyzed as a multi-valued field.
  16. [source,js]
  17. --------------------------------------------------
  18. curl -XGET 'localhost:9200/_analyze' -d '
  19. {
  20. "analyzer" : "standard",
  21. "text" : ["this is a test", "the second text"]
  22. }'
  23. --------------------------------------------------
  24. Or by building a custom transient analyzer out of tokenizers,
  25. token filters and char filters. Token filters can use the shorter 'filter'
  26. parameter name:
  27. [source,js]
  28. --------------------------------------------------
  29. curl -XGET 'localhost:9200/_analyze' -d '
  30. {
  31. "tokenizer" : "keyword",
  32. "filter" : ["lowercase"],
  33. "text" : "this is a test"
  34. }'
  35. curl -XGET 'localhost:9200/_analyze' -d '
  36. {
  37. "tokenizer" : "keyword",
  38. "filter" : ["lowercase"],
  39. "char_filter" : ["html_strip"],
  40. "text" : "this is a <b>test</b>"
  41. }'
  42. --------------------------------------------------
  43. deprecated[5.0.0, Use `filter`/`char_filter` instead of `filters`/`char_filters` and `token_filters` has been removed]
  44. Custom tokenizers, token filters, and character filters can be specified in the request body as follows:
  45. [source,js]
  46. --------------------------------------------------
  47. curl -XGET 'localhost:9200/_analyze' -d '
  48. {
  49. "tokenizer" : "whitespace",
  50. "filter" : ["lowercase", {"type": "stop", "stopwords": ["a", "is", "this"]}],
  51. "text" : "this is a test"
  52. }'
  53. --------------------------------------------------
  54. It can also run against a specific index:
  55. [source,js]
  56. --------------------------------------------------
  57. curl -XGET 'localhost:9200/test/_analyze' -d '
  58. {
  59. "text" : "this is a test"
  60. }'
  61. --------------------------------------------------
  62. The above will run an analysis on the "this is a test" text, using the
  63. default index analyzer associated with the `test` index. An `analyzer`
  64. can also be provided to use a different analyzer:
  65. [source,js]
  66. --------------------------------------------------
  67. curl -XGET 'localhost:9200/test/_analyze' -d '
  68. {
  69. "analyzer" : "whitespace",
  70. "text" : "this is a test"
  71. }'
  72. --------------------------------------------------
  73. Also, the analyzer can be derived based on a field mapping, for example:
  74. [source,js]
  75. --------------------------------------------------
  76. curl -XGET 'localhost:9200/test/_analyze' -d '
  77. {
  78. "field" : "obj1.field1",
  79. "text" : "this is a test"
  80. }'
  81. --------------------------------------------------
  82. Will cause the analysis to happen based on the analyzer configured in the
  83. mapping for `obj1.field1` (and if not, the default index analyzer).
  84. === Explain Analyze
  85. If you want to get more advanced details, set `explain` to `true` (defaults to `false`). It will output all token attributes for each token.
  86. You can filter token attributes you want to output by setting `attributes` option.
  87. experimental[The format of the additional detail information is experimental and can change at any time]
  88. [source,js]
  89. --------------------------------------------------
  90. GET _analyze
  91. {
  92. "tokenizer" : "standard",
  93. "filter" : ["snowball"],
  94. "text" : "detailed output",
  95. "explain" : true,
  96. "attributes" : ["keyword"] <1>
  97. }
  98. --------------------------------------------------
  99. // CONSOLE
  100. <1> Set "keyword" to output "keyword" attribute only
  101. The request returns the following result:
  102. [source,js]
  103. --------------------------------------------------
  104. {
  105. "detail" : {
  106. "custom_analyzer" : true,
  107. "charfilters" : [ ],
  108. "tokenizer" : {
  109. "name" : "standard",
  110. "tokens" : [ {
  111. "token" : "detailed",
  112. "start_offset" : 0,
  113. "end_offset" : 8,
  114. "type" : "<ALPHANUM>",
  115. "position" : 0
  116. }, {
  117. "token" : "output",
  118. "start_offset" : 9,
  119. "end_offset" : 15,
  120. "type" : "<ALPHANUM>",
  121. "position" : 1
  122. } ]
  123. },
  124. "tokenfilters" : [ {
  125. "name" : "snowball",
  126. "tokens" : [ {
  127. "token" : "detail",
  128. "start_offset" : 0,
  129. "end_offset" : 8,
  130. "type" : "<ALPHANUM>",
  131. "position" : 0,
  132. "keyword" : false <1>
  133. }, {
  134. "token" : "output",
  135. "start_offset" : 9,
  136. "end_offset" : 15,
  137. "type" : "<ALPHANUM>",
  138. "position" : 1,
  139. "keyword" : false <1>
  140. } ]
  141. } ]
  142. }
  143. }
  144. --------------------------------------------------
  145. // TESTRESPONSE
  146. <1> Output only "keyword" attribute, since specify "attributes" in the request.