analyze.asciidoc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. [[java-rest-high-analyze]]
  2. === Analyze API
  3. [[java-rest-high-analyze-request]]
  4. ==== Analyze Request
  5. An `AnalyzeRequest` contains the text to analyze, and one of several options to
  6. specify how the analysis should be performed.
  7. The simplest version uses a built-in analyzer:
  8. ["source","java",subs="attributes,callouts,macros"]
  9. ---------------------------------------------------
  10. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[analyze-builtin-request]
  11. ---------------------------------------------------
  12. <1> The text to include. Multiple strings are treated as a multi-valued field
  13. <2> A built-in analyzer
  14. You can configure a custom analyzer:
  15. ["source","java",subs="attributes,callouts,macros"]
  16. ---------------------------------------------------
  17. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[analyze-custom-request]
  18. ---------------------------------------------------
  19. <1> Configure char filters
  20. <2> Configure the tokenizer
  21. <3> Add a built-in tokenfilter
  22. <4> Configuration for a custom tokenfilter
  23. <5> Add the custom tokenfilter
  24. You can also build a custom normalizer, by including only charfilters and
  25. tokenfilters:
  26. ["source","java",subs="attributes,callouts,macros"]
  27. ---------------------------------------------------
  28. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[analyze-custom-normalizer-request]
  29. ---------------------------------------------------
  30. You can analyze text using an analyzer defined in an existing index:
  31. ["source","java",subs="attributes,callouts,macros"]
  32. ---------------------------------------------------
  33. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[analyze-index-request]
  34. ---------------------------------------------------
  35. <1> The index containing the mappings
  36. <2> The analyzer defined on this index to use
  37. Or you can use a normalizer:
  38. ["source","java",subs="attributes,callouts,macros"]
  39. ---------------------------------------------------
  40. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[analyze-index-normalizer-request]
  41. ---------------------------------------------------
  42. <1> The index containing the mappings
  43. <2> The normalizer defined on this index to use
  44. You can analyze text using the mappings for a particular field in an index:
  45. ["source","java",subs="attributes,callouts,macros"]
  46. ---------------------------------------------------
  47. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[analyze-field-request]
  48. ---------------------------------------------------
  49. ==== Optional arguments
  50. The following arguments can also optionally be provided:
  51. ["source","java",subs="attributes,callouts,macros"]
  52. ---------------------------------------------------
  53. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[analyze-request-explain]
  54. ---------------------------------------------------
  55. <1> Setting `explain` to true will add further details to the response
  56. <2> Setting `attributes` allows you to return only token attributes that you are
  57. interested in
  58. [[java-rest-high-analyze-sync]]
  59. ==== Synchronous Execution
  60. ["source","java",subs="attributes,callouts,macros"]
  61. ---------------------------------------------------
  62. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[analyze-request-sync]
  63. ---------------------------------------------------
  64. [[java-rest-high-analyze-async]]
  65. ==== Asynchronous Execution
  66. The asynchronous execution of an analyze request requires both the `AnalyzeRequest`
  67. instance and an `ActionListener` instance to be passed to the asyncronous method:
  68. ["source","java",subs="attributes,callouts,macros"]
  69. ---------------------------------------------------
  70. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[analyze-request-async]
  71. ---------------------------------------------------
  72. The asynchronous method does not block and returns immediately. Once it is
  73. completed the `ActionListener` is called back using the `onResponse` method if the
  74. execution successfully completed or using the `onFailure` method if it failed.
  75. A typical listener for `AnalyzeResponse` looks like:
  76. ["source","java",subs="attributes,callouts,macros"]
  77. ---------------------------------------------------
  78. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[analyze-execute-listener]
  79. ---------------------------------------------------
  80. [[java-rest-high-analyze-response]]
  81. ==== Analyze Response
  82. The returned `AnalyzeResponse` allows you to retrieve details of the analysis as
  83. follows:
  84. ["source","java",subs="attributes,callouts,macros"]
  85. ---------------------------------------------------
  86. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[analyze-response-tokens]
  87. ---------------------------------------------------
  88. <1> `AnalyzeToken` holds information about the individual tokens produced by analysis
  89. If `explain` was set to `true`, then information is instead returned from the `detail()`
  90. method:
  91. ["source","java",subs="attributes,callouts,macros"]
  92. ---------------------------------------------------
  93. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[analyze-response-detail]
  94. ---------------------------------------------------
  95. <1> `DetailAnalyzeResponse` holds more detailed information about tokens produced by
  96. the various substeps in the analysis chain.