Search

Introduced 1.0

The Search API operation lets you execute a search request to search your cluster for data.

Example

  1. GET /movies/_search
  2. {
  3. "query": {
  4. "match": {
  5. "text_entry": "I am the night"
  6. }
  7. }
  8. }

copy

Path and HTTP Methods

  1. GET /<target-index>/_search
  2. GET /_search
  3. POST /<target-index>/_search
  4. POST /_search

URL Parameters

All URL parameters are optional.

ParameterTypeDescription
allow_no_indicesBooleanWhether to ignore wildcards that don’t match any indexes. Default is true.
allow_partial_search_resultsBooleanWhether to return partial results if the request runs into an error or times out. Default is true.
analyzerStringAnalyzer to use in the query string.
analyze_wildcardBooleanWhether the update operation should include wildcard and prefix queries in the analysis. Default is false.
batched_reduce_sizeIntegerHow many shard results to reduce on a node. Default is 512.
cancel_after_time_intervalTimeThe time after which the search request will be canceled. Request-level parameter takes precedence over cancel_after_time_interval cluster setting. Default is -1.
ccs_minimize_roundtripsBooleanWhether to minimize roundtrips between a node and remote clusters. Default is true.
default_operatorStringIndicates whether the default operator for a string query should be AND or OR. Default is OR.
dfStringThe default field in case a field prefix is not provided in the query string.
docvalue_fieldsStringThe fields that OpenSearch should return using their docvalue forms.
expand_wildcardsStringSpecifies the type of index that wildcard expressions can match. Supports comma-separated values. Valid values are all (match any index), open (match open, non-hidden indexes), closed (match closed, non-hidden indexes), hidden (match hidden indexes), and none (deny wildcard expressions). Default is open.
explainBooleanWhether to return details about how OpenSearch computed the document’s score. Default is false.
fromIntegerThe starting index to search from. Default is 0.
ignore_throttledBooleanWhether to ignore concrete, expanded, or indexes with aliases if indexes are frozen. Default is true.
ignore_unavailableBooleanSpecifies whether to include missing or closed indexes in the response. Default is false.
lenientBooleanSpecifies whether OpenSearch should accept requests if queries have format errors (for example, querying a text field for an integer). Default is false.
max_concurrent_shard_requestsIntegerHow many concurrent shard requests this request should execute on each node. Default is 5.
pre_filter_shard_sizeIntegerA prefilter size threshold that triggers a prefilter operation if the request exceeds the threshold. Default is 128 shards.
preferenceStringSpecifies the shards or nodes on which OpenSearch should perform the search. For valid values, see The preference query parameter.
qStringLucene query string’s query.
request_cacheBooleanSpecifies whether OpenSearch should use the request cache. Default is whether it’s enabled in the index’s settings.
rest_total_hits_as_intBooleanWhether to return hits.total as an integer. Returns an object otherwise. Default is false.
routingStringValue used to route the update by query operation to a specific shard.
scrollTimeHow long to keep the search context open.
search_typeStringWhether OpenSearch should use global term and document frequencies when calculating relevance scores. Valid choices are query_then_fetch and dfs_query_then_fetch. query_then_fetch scores documents using local term and document frequencies for the shard. It’s usually faster but less accurate. dfs_query_then_fetch scores documents using global term and document frequencies across all shards. It’s usually slower but more accurate. Default is query_then_fetch.
seq_no_primary_termBooleanWhether to return sequence number and primary term of the last operation of each document hit.
sizeIntegerHow many results to include in the response.
sortListA comma-separated list of <field> : <direction> pairs to sort by.
_sourceStringWhether to include the _source field in the response.
_source_excludesListA comma-separated list of source fields to exclude from the response.
_source_includesListA comma-separated list of source fields to include in the response.
statsStringValue to associate with the request for additional logging.
stored_fieldsBooleanWhether the get operation should retrieve fields stored in the index. Default is false.
suggest_fieldStringFields OpenSearch can use to look for similar terms.
suggest_modeStringThe mode to use when searching. Available options are always (use suggestions based on the provided terms), popular (use suggestions that have more occurrences), and missing (use suggestions for terms not in the index).
suggest_sizeIntegerHow many suggestions to return.
suggest_textStringThe source that suggestions should be based off of.
terminate_afterIntegerThe maximum number of documents OpenSearch should process before terminating the request. Default is 0.
timeoutTimeHow long the operation should wait for a response from active shards. Default is 1m.
track_scoresBooleanWhether to return document scores. Default is false.
track_total_hitsBoolean or IntegerWhether to return how many documents matched the query.
typed_keysBooleanWhether returned aggregations and suggested terms should include their types in the response. Default is true.
versionBooleanWhether to include the document version as a match.

The preference query parameter

The preference query parameter specifies the shards or nodes on which OpenSearch should perform the search. The following are valid values:

  • _primary: Perform the search only on primary shards.
  • _replica: Perform the search only on replica shards.
  • _primary_first: Perform the search on primary shards but fail over to other available shards if primary shards are not available.
  • _replica_first: Perform the search on replica shards but fail over to other available shards if replica shards are not available.
  • _local: If possible, perform the search on the local node’s shards.
  • _prefer_nodes:<node-id-1>,<node-id-2>: If possible, perform the search on the specified nodes. Use a comma-separated list to specify multiple nodes.
  • _shards:<shard-id-1>,<shard-id-2>: Perform the search only on the specified shards. Use a comma-separated list to specify multiple shards. When combined with other preferences, the _shards preference must be listed first. For example, _shards:1,2|_replica.
  • _only_nodes:<node-id-1>,<node-id-2>: Perform the search only on the specified nodes. Use a comma-separated list to specify multiple nodes.
  • <string>: Specifies a custom string to use for the search. The string cannot start with an underscore character (_). Searches with the same custom string are routed to the same shards.

Request body

All fields are optional.

FieldTypeDescription
docvalue_fieldsArray of objectsThe fields that OpenSearch should return using their docvalue forms. Specify a format to return results in a certain format, such as date and time.
fieldsArrayThe fields to search for in the request. Specify a format to return results in a certain format, such as date and time.
explainStringWhether to return details about how OpenSearch computed the document’s score. Default is false.
fromIntegerThe starting index to search from. Default is 0.
indices_boostArray of objectsValues used to boost the score of specified indexes. Specify in the format of <index> : <boost-multiplier>
min_scoreIntegerSpecify a score threshold to return only documents above the threshold.
queryObjectThe DSL query to use in the request.
seq_no_primary_termBooleanWhether to return sequence number and primary term of the last operation of each document hit.
sizeIntegerHow many results to return. Default is 10.
_source Whether to include the _source field in the response.
statsStringValue to associate with the request for additional logging.
terminate_afterIntegerThe maximum number of documents OpenSearch should process before terminating the request. Default is 0.
timeoutTimeHow long to wait for a response. Default is no timeout.
versionBooleanWhether to include the document version in the response.

Response body

  1. {
  2. "took": 3,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 1,
  13. "relation": "eq"
  14. },
  15. "max_score": 1.0,
  16. "hits": [
  17. {
  18. "_index": "superheroes",
  19. "_id": "1",
  20. "_score": 1.0,
  21. "_source": {
  22. "superheroes": [
  23. {
  24. "Hero name": "Superman",
  25. "Real identity": "Clark Kent",
  26. "Age": 28
  27. },
  28. {
  29. "Hero name": "Batman",
  30. "Real identity": "Bruce Wayne",
  31. "Age": 26
  32. },
  33. {
  34. "Hero name": "Flash",
  35. "Real identity": "Barry Allen",
  36. "Age": 28
  37. },
  38. {
  39. "Hero name": "Robin",
  40. "Real identity": "Dick Grayson",
  41. "Age": 15
  42. }
  43. ]
  44. }
  45. }
  46. ]
  47. }
  48. }

The ext object

Starting with OpenSearch 2.10, plugin authors can add an ext object to the search response. The purpose of the ext object is to contain plugin-specific response fields. For example, in conversational search, the result of Retrieval Augmented Generation (RAG) is a single “hit” (answer). Plugin authors can include this answer in the search response as part of the ext object so that it is separate from the search hits. In the following example response, the RAG result is in the ext.retrieval_augmented_generation.answer field:

  1. {
  2. "took": 3,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 3,
  6. "successful": 3,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 110,
  13. "relation": "eq"
  14. },
  15. "max_score": 0.55129033,
  16. "hits": [
  17. {
  18. "_index": "...",
  19. "_id": "...",
  20. "_score": 0.55129033,
  21. "_source": {
  22. "text": "...",
  23. "title": "..."
  24. }
  25. },
  26. {
  27. ...
  28. }
  29. ...
  30. {
  31. ...
  32. }
  33. ],
  34. }, // end of hits
  35. "ext": {
  36. "retrieval_augmented_generation": { // a search response processor
  37. "answer": "RAG answer"
  38. }
  39. }
  40. }