-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Starting search request documentation for high level client #25651
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,180 @@ | ||
| [[java-rest-high-search]] | ||
| === Search API | ||
|
|
||
| To be documented. | ||
| [[java-rest-high-document-search-request]] | ||
| ==== Search Request | ||
|
|
||
| The `SearchRequest` is used for any operation that has to do with searching | ||
| documents, aggregations, suggestions and also offers ways of requesting | ||
| highlighting on the resulting documents. | ||
|
|
||
| In its most basic form, a query can be added to the request like this: | ||
|
|
||
| ["source","java",subs="attributes,callouts,macros"] | ||
| -------------------------------------------------- | ||
| include-tagged::{doc-tests}/SearchDocumentationIT.java[search-request-basic] | ||
| -------------------------------------------------- | ||
|
|
||
| <1> Creates the `SeachRequest`. Without arguments this runs against all indices. | ||
| <2> Most parameters of the search can be added to the `SearchSourceBuilder` | ||
| which contains everything that | ||
| in the Rest API would be placed in the search request body. | ||
| <3> Add a `match_all` query to the `SearchSourceBuilder`. | ||
|
|
||
| ==== Optional arguments | ||
|
|
||
| Lets first look at some of the optional argument of a `SearchRequest`. | ||
| First of all, the request can be restricted to one or more indices using the | ||
| constructor or to on or more types using a setter: | ||
|
|
||
| ["source","java",subs="attributes,callouts,macros"] | ||
| -------------------------------------------------- | ||
| include-tagged::{doc-tests}/SearchDocumentationIT.java[search-request-indices-types] | ||
| -------------------------------------------------- | ||
|
|
||
| There are a couple of other interesting optional parameters: | ||
|
|
||
| ["source","java",subs="attributes,callouts,macros"] | ||
| -------------------------------------------------- | ||
| include-tagged::{doc-tests}/SearchDocumentationIT.java[search-request-routing] | ||
| -------------------------------------------------- | ||
| <1> Set a routing parameter | ||
|
|
||
| ["source","java",subs="attributes,callouts,macros"] | ||
| -------------------------------------------------- | ||
| include-tagged::{doc-tests}/SearchDocumentationIT.java[search-request-indicesOptions] | ||
| -------------------------------------------------- | ||
| <1> Setting `IndicesOptions` controls how unavailable indices are resolved and | ||
| how wildcard expressions are expanded | ||
|
|
||
| ["source","java",subs="attributes,callouts,macros"] | ||
| -------------------------------------------------- | ||
| include-tagged::{doc-tests}/SearchDocumentationIT.java[search-request-preference] | ||
| -------------------------------------------------- | ||
| <1> Use the preference parameter e.g. to execute the search to prefer local | ||
| shards. The The default is to randomize across shards. | ||
|
|
||
| ==== Using the SearchSourceBuilder | ||
|
|
||
| Most options controlling the search behavior can be set on the | ||
| `SearchSourceBuilder`, | ||
| which contains more or less the equivalent of the options in the search request | ||
| body of the Rest API. | ||
|
|
||
| Here are a few examples of some common options: | ||
|
|
||
| ["source","java",subs="attributes,callouts,macros"] | ||
| -------------------------------------------------- | ||
| include-tagged::{doc-tests}/SearchDocumentationIT.java[search-source-basics] | ||
| -------------------------------------------------- | ||
| <1> Create a `SearchSourceBuilder` with default options. | ||
| <2> Set the query. Can be any type of `QueryBuilder` | ||
| <3> Set the `from` option that determines the result index to start searching | ||
| from. Defaults to 0. | ||
| <4> Set the `size` option that determines the number of search hits to return. | ||
| Defaults to 10. | ||
| <5> Set an optional timeout that controls how long the search is allowed to | ||
| take. | ||
|
|
||
| After this, the `SearchSourceBuilder` only needs to be added to the | ||
| `SearchRequest`: | ||
|
|
||
| ["source","java",subs="attributes,callouts,macros"] | ||
| -------------------------------------------------- | ||
| include-tagged::{doc-tests}/SearchDocumentationIT.java[search-source-setter] | ||
| -------------------------------------------------- | ||
|
|
||
|
|
||
| [[java-rest-high-document-search-sync]] | ||
| ==== Synchronous Execution | ||
|
|
||
| When executing a `SearchRequest` in the following manner, the client waits | ||
| for the `SearchResponse` to be returned before continuing with code execution: | ||
|
|
||
| ["source","java",subs="attributes,callouts,macros"] | ||
| -------------------------------------------------- | ||
| include-tagged::{doc-tests}/SearchDocumentationIT.java[search-execute] | ||
| -------------------------------------------------- | ||
|
|
||
| [[java-rest-high-document-search-async]] | ||
| ==== Asynchronous Execution | ||
|
|
||
|
|
||
| Executing a `SearchRequest` can also be done in an asynchronous fashion so that | ||
| the client can return directly. Users need to specify how the response or | ||
| potential failures will be handled by passing in appropriate listeners: | ||
|
|
||
| ["source","java",subs="attributes,callouts,macros"] | ||
| -------------------------------------------------- | ||
| include-tagged::{doc-tests}/SearchDocumentationIT.java[search-execute-async] | ||
| -------------------------------------------------- | ||
| <1> Called when the execution is successfully completed. | ||
| <2> Called when the whole `SearchRequest` fails. | ||
|
|
||
| ==== SearchResponse | ||
|
|
||
| The `SearchResponse` that is returned by executing the search provides details | ||
| about the search execution itself as well as access to the documents returned. | ||
| First, there is useful information about the request execution itself, like the | ||
| HTTP status code, execution time or wether the request terminated early or timed | ||
| out: | ||
|
|
||
| ["source","java",subs="attributes,callouts,macros"] | ||
| -------------------------------------------------- | ||
| include-tagged::{doc-tests}/SearchDocumentationIT.java[search-response-1] | ||
| -------------------------------------------------- | ||
|
|
||
| Second, the response also provides information about the execution on the | ||
| shard level by offering statistics about the total number of shards that were | ||
| affected by the search, and the successful vs. unsuccessful shards. Possible | ||
| failures can also be handled by iterating over an array off | ||
| `ShardSearchFailures` like in the following example: | ||
|
|
||
| ["source","java",subs="attributes,callouts,macros"] | ||
| -------------------------------------------------- | ||
| include-tagged::{doc-tests}/SearchDocumentationIT.java[search-response-2] | ||
| -------------------------------------------------- | ||
|
|
||
| To get access to the returned documents, we need to first get the `SearchHits` | ||
| contained in the response: | ||
|
|
||
| ["source","java",subs="attributes,callouts,macros"] | ||
| -------------------------------------------------- | ||
| include-tagged::{doc-tests}/SearchDocumentationIT.java[search-hits-get] | ||
| -------------------------------------------------- | ||
|
|
||
| The `SearchHits` provides global information about all hits, like total number | ||
| of hits or the maximum score: | ||
|
|
||
| ["source","java",subs="attributes,callouts,macros"] | ||
| -------------------------------------------------- | ||
| include-tagged::{doc-tests}/SearchDocumentationIT.java[search-hits-info] | ||
| -------------------------------------------------- | ||
|
|
||
| Nested inside the `SearchHits` are the individual search results that can | ||
| be iterated over like this: | ||
|
|
||
|
|
||
| ["source","java",subs="attributes,callouts,macros"] | ||
| -------------------------------------------------- | ||
| include-tagged::{doc-tests}/SearchDocumentationIT.java[search-hits-singleHit] | ||
| -------------------------------------------------- | ||
|
|
||
| The `SearchHit` provides access to basic information like index, type, docId and | ||
| score of each search hit: | ||
|
|
||
| ["source","java",subs="attributes,callouts,macros"] | ||
| -------------------------------------------------- | ||
| include-tagged::{doc-tests}/SearchDocumentationIT.java[search-hits-singleHit-properties] | ||
| -------------------------------------------------- | ||
|
|
||
| Furthermore, it lets you get back the document source, either as a simple | ||
| JSON-String or as a map of key/value pairs. In this map, regular fields | ||
| are keyed by the field name and contain the field value. Multi-valued fields are | ||
| returned as lists of objects, nested objects as another key/value map. These | ||
| cases need to be case accordingly: | ||
|
|
||
| ["source","java",subs="attributes,callouts,macros"] | ||
| -------------------------------------------------- | ||
| include-tagged::{doc-tests}/SearchDocumentationIT.java[search-hits-singleHit-source] | ||
| -------------------------------------------------- | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rather than having a big snippet with 5 or more notes, in other docs we did one snippet per option, it is a bit more work but maybe better when visualized and more readable?