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