|
1 | 1 | [[java-rest-high-search]] |
2 | 2 | === Search API |
3 | 3 |
|
4 | | -To be documented. |
| 4 | +[[java-rest-high-document-search-request]] |
| 5 | +==== Search Request |
| 6 | + |
| 7 | +The `SearchRequest` is used for any operation that has to do with searching |
| 8 | +documents, aggregations, suggestions and also offers ways of requesting |
| 9 | +highlighting on the resulting documents. |
| 10 | + |
| 11 | +In its most basic form, a query can be added to the request like this: |
| 12 | + |
| 13 | +["source","java",subs="attributes,callouts,macros"] |
| 14 | +-------------------------------------------------- |
| 15 | +include-tagged::{doc-tests}/SearchDocumentationIT.java[search-request-basic] |
| 16 | +-------------------------------------------------- |
| 17 | + |
| 18 | +<1> Creates the `SeachRequest`. Without arguments this runs against all indices. |
| 19 | +<2> Most parameters of the search can be added to the `SearchSourceBuilder` |
| 20 | +which contains everything that |
| 21 | +in the Rest API would be placed in the search request body. |
| 22 | +<3> Add a `match_all` query to the `SearchSourceBuilder`. |
| 23 | + |
| 24 | +==== Optional arguments |
| 25 | + |
| 26 | +Lets first look at some of the optional argument of a `SearchRequest`. |
| 27 | +First of all, the request can be restricted to one or more indices using the |
| 28 | +constructor or to on or more types using a setter: |
| 29 | + |
| 30 | +["source","java",subs="attributes,callouts,macros"] |
| 31 | +-------------------------------------------------- |
| 32 | +include-tagged::{doc-tests}/SearchDocumentationIT.java[search-request-indices-types] |
| 33 | +-------------------------------------------------- |
| 34 | + |
| 35 | +There are a couple of other interesting optional parameters: |
| 36 | + |
| 37 | +["source","java",subs="attributes,callouts,macros"] |
| 38 | +-------------------------------------------------- |
| 39 | +include-tagged::{doc-tests}/SearchDocumentationIT.java[search-request-routing] |
| 40 | +-------------------------------------------------- |
| 41 | +<1> Set a routing parameter |
| 42 | + |
| 43 | +["source","java",subs="attributes,callouts,macros"] |
| 44 | +-------------------------------------------------- |
| 45 | +include-tagged::{doc-tests}/SearchDocumentationIT.java[search-request-indicesOptions] |
| 46 | +-------------------------------------------------- |
| 47 | +<1> Setting `IndicesOptions` controls how unavailable indices are resolved and |
| 48 | +how wildcard expressions are expanded |
| 49 | + |
| 50 | +["source","java",subs="attributes,callouts,macros"] |
| 51 | +-------------------------------------------------- |
| 52 | +include-tagged::{doc-tests}/SearchDocumentationIT.java[search-request-preference] |
| 53 | +-------------------------------------------------- |
| 54 | +<1> Use the preference parameter e.g. to execute the search to prefer local |
| 55 | +shards. The The default is to randomize across shards. |
| 56 | + |
| 57 | +==== Using the SearchSourceBuilder |
| 58 | + |
| 59 | +Most options controlling the search behavior can be set on the |
| 60 | +`SearchSourceBuilder`, |
| 61 | +which contains more or less the equivalent of the options in the search request |
| 62 | +body of the Rest API. |
| 63 | + |
| 64 | +Here are a few examples of some common options: |
| 65 | + |
| 66 | +["source","java",subs="attributes,callouts,macros"] |
| 67 | +-------------------------------------------------- |
| 68 | +include-tagged::{doc-tests}/SearchDocumentationIT.java[search-source-basics] |
| 69 | +-------------------------------------------------- |
| 70 | +<1> Create a `SearchSourceBuilder` with default options. |
| 71 | +<2> Set the query. Can be any type of `QueryBuilder` |
| 72 | +<3> Set the `from` option that determines the result index to start searching |
| 73 | +from. Defaults to 0. |
| 74 | +<4> Set the `size` option that determines the number of search hits to return. |
| 75 | +Defaults to 10. |
| 76 | +<5> Set an optional timeout that controls how long the search is allowed to |
| 77 | +take. |
| 78 | + |
| 79 | +After this, the `SearchSourceBuilder` only needs to be added to the |
| 80 | +`SearchRequest`: |
| 81 | + |
| 82 | +["source","java",subs="attributes,callouts,macros"] |
| 83 | +-------------------------------------------------- |
| 84 | +include-tagged::{doc-tests}/SearchDocumentationIT.java[search-source-setter] |
| 85 | +-------------------------------------------------- |
| 86 | + |
| 87 | + |
| 88 | +[[java-rest-high-document-search-sync]] |
| 89 | +==== Synchronous Execution |
| 90 | + |
| 91 | +When executing a `SearchRequest` in the following manner, the client waits |
| 92 | +for the `SearchResponse` to be returned before continuing with code execution: |
| 93 | + |
| 94 | +["source","java",subs="attributes,callouts,macros"] |
| 95 | +-------------------------------------------------- |
| 96 | +include-tagged::{doc-tests}/SearchDocumentationIT.java[search-execute] |
| 97 | +-------------------------------------------------- |
| 98 | + |
| 99 | +[[java-rest-high-document-search-async]] |
| 100 | +==== Asynchronous Execution |
| 101 | + |
| 102 | + |
| 103 | +Executing a `SearchRequest` can also be done in an asynchronous fashion so that |
| 104 | +the client can return directly. Users need to specify how the response or |
| 105 | +potential failures will be handled by passing in appropriate listeners: |
| 106 | + |
| 107 | +["source","java",subs="attributes,callouts,macros"] |
| 108 | +-------------------------------------------------- |
| 109 | +include-tagged::{doc-tests}/SearchDocumentationIT.java[search-execute-async] |
| 110 | +-------------------------------------------------- |
| 111 | +<1> Called when the execution is successfully completed. |
| 112 | +<2> Called when the whole `SearchRequest` fails. |
| 113 | + |
| 114 | +==== SearchResponse |
| 115 | + |
| 116 | +The `SearchResponse` that is returned by executing the search provides details |
| 117 | +about the search execution itself as well as access to the documents returned. |
| 118 | +First, there is useful information about the request execution itself, like the |
| 119 | +HTTP status code, execution time or wether the request terminated early or timed |
| 120 | +out: |
| 121 | + |
| 122 | +["source","java",subs="attributes,callouts,macros"] |
| 123 | +-------------------------------------------------- |
| 124 | +include-tagged::{doc-tests}/SearchDocumentationIT.java[search-response-1] |
| 125 | +-------------------------------------------------- |
| 126 | + |
| 127 | +Second, the response also provides information about the execution on the |
| 128 | +shard level by offering statistics about the total number of shards that were |
| 129 | +affected by the search, and the successful vs. unsuccessful shards. Possible |
| 130 | +failures can also be handled by iterating over an array off |
| 131 | +`ShardSearchFailures` like in the following example: |
| 132 | + |
| 133 | +["source","java",subs="attributes,callouts,macros"] |
| 134 | +-------------------------------------------------- |
| 135 | +include-tagged::{doc-tests}/SearchDocumentationIT.java[search-response-2] |
| 136 | +-------------------------------------------------- |
| 137 | + |
| 138 | +To get access to the returned documents, we need to first get the `SearchHits` |
| 139 | +contained in the response: |
| 140 | + |
| 141 | +["source","java",subs="attributes,callouts,macros"] |
| 142 | +-------------------------------------------------- |
| 143 | +include-tagged::{doc-tests}/SearchDocumentationIT.java[search-hits-get] |
| 144 | +-------------------------------------------------- |
| 145 | + |
| 146 | +The `SearchHits` provides global information about all hits, like total number |
| 147 | +of hits or the maximum score: |
| 148 | + |
| 149 | +["source","java",subs="attributes,callouts,macros"] |
| 150 | +-------------------------------------------------- |
| 151 | +include-tagged::{doc-tests}/SearchDocumentationIT.java[search-hits-info] |
| 152 | +-------------------------------------------------- |
| 153 | + |
| 154 | +Nested inside the `SearchHits` are the individual search results that can |
| 155 | +be iterated over like this: |
| 156 | + |
| 157 | + |
| 158 | +["source","java",subs="attributes,callouts,macros"] |
| 159 | +-------------------------------------------------- |
| 160 | +include-tagged::{doc-tests}/SearchDocumentationIT.java[search-hits-singleHit] |
| 161 | +-------------------------------------------------- |
| 162 | + |
| 163 | +The `SearchHit` provides access to basic information like index, type, docId and |
| 164 | +score of each search hit: |
| 165 | + |
| 166 | +["source","java",subs="attributes,callouts,macros"] |
| 167 | +-------------------------------------------------- |
| 168 | +include-tagged::{doc-tests}/SearchDocumentationIT.java[search-hits-singleHit-properties] |
| 169 | +-------------------------------------------------- |
| 170 | + |
| 171 | +Furthermore, it lets you get back the document source, either as a simple |
| 172 | +JSON-String or as a map of key/value pairs. In this map, regular fields |
| 173 | +are keyed by the field name and contain the field value. Multi-valued fields are |
| 174 | +returned as lists of objects, nested objects as another key/value map. These |
| 175 | +cases need to be case accordingly: |
| 176 | + |
| 177 | +["source","java",subs="attributes,callouts,macros"] |
| 178 | +-------------------------------------------------- |
| 179 | +include-tagged::{doc-tests}/SearchDocumentationIT.java[search-hits-singleHit-source] |
| 180 | +-------------------------------------------------- |
0 commit comments