analyze.asciidoc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 'filters'
  26. parameter name:
  27. [source,js]
  28. --------------------------------------------------
  29. curl -XGET 'localhost:9200/_analyze' -d '
  30. {
  31. "tokenizer" : "keyword",
  32. "filters" : ["lowercase"],
  33. "text" : "this is a test"
  34. }'
  35. curl -XGET 'localhost:9200/_analyze' -d '
  36. {
  37. "tokenizer" : "keyword",
  38. "token_filters" : ["lowercase"],
  39. "char_filters" : ["html_strip"],
  40. "text" : "this is a <b>test</b>"
  41. }'
  42. --------------------------------------------------
  43. It can also run against a specific index:
  44. [source,js]
  45. --------------------------------------------------
  46. curl -XGET 'localhost:9200/test/_analyze' -d '
  47. {
  48. "text" : "this is a test"
  49. }'
  50. --------------------------------------------------
  51. The above will run an analysis on the "this is a test" text, using the
  52. default index analyzer associated with the `test` index. An `analyzer`
  53. can also be provided to use a different analyzer:
  54. [source,js]
  55. --------------------------------------------------
  56. curl -XGET 'localhost:9200/test/_analyze' -d '
  57. {
  58. "analyzer" : "whitespace",
  59. "text : "this is a test"
  60. }'
  61. --------------------------------------------------
  62. Also, the analyzer can be derived based on a field mapping, for example:
  63. [source,js]
  64. --------------------------------------------------
  65. curl -XGET 'localhost:9200/test/_analyze' -d '
  66. {
  67. "field" : "obj1.field1",
  68. "text" : "this is a test"
  69. }'
  70. --------------------------------------------------
  71. Will cause the analysis to happen based on the analyzer configured in the
  72. mapping for `obj1.field1` (and if not, the default index analyzer).
  73. All parameters can also supplied as request parameters. For example:
  74. [source,js]
  75. --------------------------------------------------
  76. curl -XGET 'localhost:9200/_analyze?tokenizer=keyword&filters=lowercase&text=this+is+a+test'
  77. --------------------------------------------------
  78. For backwards compatibility, we also accept the text parameter as the body of the request,
  79. provided it doesn't start with `{` :
  80. [source,js]
  81. --------------------------------------------------
  82. curl -XGET 'localhost:9200/_analyze?tokenizer=keyword&token_filters=lowercase&char_filters=html_strip' -d 'this is a <b>test</b>'
  83. --------------------------------------------------
  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 test/_analyze
  91. {
  92. "tokenizer" : "standard",
  93. "token_filters" : ["snowball"],
  94. "text" : "detailed output",
  95. "explain" : true,
  96. "attributes" : ["keyword"] <1>
  97. }
  98. --------------------------------------------------
  99. // AUTOSENSE
  100. <1> Set "keyword" to output "keyword" attribute only
  101. coming[2.0.0, body based parameters were added in 2.0.0]
  102. The request returns the following result:
  103. [source,js]
  104. --------------------------------------------------
  105. {
  106. "detail" : {
  107. "custom_analyzer" : true,
  108. "charfilters" : [ ],
  109. "tokenizer" : {
  110. "name" : "standard",
  111. "tokens" : [ {
  112. "token" : "detailed",
  113. "start_offset" : 0,
  114. "end_offset" : 8,
  115. "type" : "<ALPHANUM>",
  116. "position" : 0
  117. }, {
  118. "token" : "output",
  119. "start_offset" : 9,
  120. "end_offset" : 15,
  121. "type" : "<ALPHANUM>",
  122. "position" : 1
  123. } ]
  124. },
  125. "tokenfilters" : [ {
  126. "name" : "snowball",
  127. "tokens" : [ {
  128. "token" : "detail",
  129. "start_offset" : 0,
  130. "end_offset" : 8,
  131. "type" : "<ALPHANUM>",
  132. "position" : 0,
  133. "keyword" : false <1>
  134. }, {
  135. "token" : "output",
  136. "start_offset" : 9,
  137. "end_offset" : 15,
  138. "type" : "<ALPHANUM>",
  139. "position" : 1,
  140. "keyword" : false <1>
  141. } ]
  142. } ]
  143. }
  144. }
  145. --------------------------------------------------
  146. <1> Output only "keyword" attribute, since specify "attributes" in the request.