Method: Elasticsearch::API::Actions#search
- Defined in:
- lib/elasticsearch/api/actions/search.rb
#search(arguments = {}) ⇒ Object
Run a search. Get search hits that match the query defined in the request. You can provide search queries using the q query string parameter or the request body. If both are specified, only the query parameter is used. If the Elasticsearch security features are enabled, you must have the read index privilege for the target data stream, index, or alias. For cross-cluster search, refer to the documentation about configuring CCS privileges. To search a point in time (PIT) for an alias, you must have the read index privilege for the alias’s data streams or indices. **Search slicing** When paging through a large number of documents, it can be helpful to split the search into multiple slices to consume them independently with the slice and pit properties. By default the splitting is done first on the shards, then locally on each shard. The local splitting partitions the shard into contiguous ranges based on Lucene document IDs. For instance if the number of shards is equal to 2 and you request 4 slices, the slices 0 and 2 are assigned to the first shard and the slices 1 and 3 are assigned to the second shard. IMPORTANT: The same point-in-time ID should be used for all slices. If different PIT IDs are used, slices can overlap and miss documents. This situation can occur because the splitting criterion is based on Lucene document IDs, which are not stable across changes to the index.
Parameters:
-
arguments
(Hash)
(defaults to: {})
—
a customizable set of options
Options Hash (arguments):
-
:index
(String, Array)
—
A comma-separated list of data streams, indices, and aliases to search. It supports wildcards (‘*`). To search all data streams and indices, omit this parameter or use `*` or
_all. -
:allow_no_indices
(Boolean)
—
If
false, the request returns an error if any wildcard expression, index alias, or_allvalue targets only missing or closed indices. This behavior applies even if the request targets other open indices. For example, a request targeting ‘foo*,bar*` returns an error if an index starts withfoobut no index starts withbar. Server default: true. -
:allow_partial_search_results
(Boolean)
—
If
trueand there are shard request timeouts or shard failures, the request returns partial results. Iffalse, it returns an error with no partial results.To override the default behavior, you can set thesearch.default_allow_partial_resultscluster setting tofalse. Server default: true. -
:analyzer
(String)
—
The analyzer to use for the query string. This parameter can be used only when the
qquery string parameter is specified. -
:analyze_wildcard
(Boolean)
—
If
true, wildcard and prefix queries are analyzed. This parameter can be used only when theqquery string parameter is specified. -
:batched_reduce_size
(Integer)
—
The number of shard results that should be reduced at once on the coordinating node. If the potential number of shards in the request can be large, this value should be used as a protection mechanism to reduce the memory overhead per search request. Server default: 512.
-
:ccs_minimize_roundtrips
(Boolean)
—
If
true, network round-trips between the coordinating node and the remote clusters are minimized when running cross-cluster search (CCS) requests. Server default: true. -
:default_operator
(String)
—
The default operator for the query string query:
andoror. This parameter can be used only when theqquery string parameter is specified. Server default: or. -
:df
(String)
—
The field to use as a default when no field prefix is given in the query string. This parameter can be used only when the
qquery string parameter is specified. -
:docvalue_fields
(String, Array<String>)
—
A comma-separated list of fields to return as the docvalue representation of a field for each hit.
-
:expand_wildcards
(String, Array<String>)
—
The type of index that wildcard patterns can match. If the request can target data streams, this argument determines whether wildcard expressions match hidden data streams. It supports comma-separated values such as ‘open,hidden`. Server default: open.
-
:explain
(Boolean)
—
If
true, the request returns detailed information about score computation as part of a hit. -
:ignore_throttled
(Boolean)
—
If
true, concrete, expanded or aliased indices will be ignored when frozen. Server default: true. -
:ignore_unavailable
(Boolean)
—
If
false, the request returns an error if it targets a missing or closed index. -
:include_named_queries_score
(Boolean)
—
If
true, the response includes the score contribution from any named queries.This functionality reruns each named query on every hit in a search response. Typically, this adds a small overhead to a request. However, using computationally expensive named queries on a large number of hits may add significant overhead. -
:lenient
(Boolean)
—
If
true, format-based query failures (such as providing text to a numeric field) in the query string will be ignored. This parameter can be used only when theqquery string parameter is specified. -
:max_concurrent_shard_requests
(Integer)
—
The number of concurrent shard requests per node that the search runs concurrently. This value should be used to limit the impact of the search on the cluster in order to limit the number of concurrent shard requests. Server default: 5.
-
:preference
(String)
—
The nodes and shards used for the search. By default, Elasticsearch selects from eligible nodes and shards using adaptive replica selection, accounting for allocation awareness. Valid values are:
-
_only_localto run the search only on shards on the local node. -
_localto, if possible, run the search on shards on the local node, or if not, select shards using the default method. -
‘_only_nodes:<node-id>,<node-id>` to run the search on only the specified nodes IDs. If suitable shards exist on more than one selected node, use shards on those nodes using the default method. If none of the specified nodes are available, select shards from any available node using the default method.
-
‘_prefer_nodes:<node-id>,<node-id>` to if possible, run the search on the specified nodes IDs. If not, select shards using the default method.
-
‘_shards:<shard>,<shard>` to run the search only on the specified shards. You can combine this value with other
preferencevalues. However, the_shardsvalue must come first. For example: `_shards:2,3|_local`. -
‘<custom-string>` (any string that does not start with
_) to route searches with the same `<custom-string>` to the same shards in the same order.
-
-
:pre_filter_shard_size
(Integer)
—
A threshold that enforces a pre-filter roundtrip to prefilter search shards based on query rewriting if the number of shards the search request expands to exceeds the threshold. This filter roundtrip can limit the number of shards significantly if for instance a shard can not match any documents based on its rewrite method (if date filters are mandatory to match but the shard bounds and the query are disjoint). When unspecified, the pre-filter phase is executed if any of these conditions is met:
-
The request targets more than 128 shards.
-
The request targets one or more read-only index.
-
The primary sort of the query targets an indexed field.
-
-
:request_cache
(Boolean)
—
If
true, the caching of search results is enabled for requests wheresizeis0. It defaults to index level settings. -
:routing
(String, Array<String>)
—
A custom value that is used to route operations to a specific shard.
-
:scroll
(Time)
—
The period to retain the search context for scrolling. By default, this value cannot exceed
1d(24 hours). You can change this limit by using thesearch.max_keep_alivecluster-level setting. -
:search_type
(String)
—
Indicates how distributed term frequencies are calculated for relevance scoring.
-
:stats
(Array<String>)
—
Specific
tagof the request for logging and statistical purposes. -
:stored_fields
(String, Array<String>)
—
A comma-separated list of stored fields to return as part of a hit. If no fields are specified, no stored fields are included in the response. If this field is specified, the
_sourceparameter defaults tofalse. You can pass ‘_source: true` to return both source fields and stored fields in the search response. -
:suggest_field
(String)
—
The field to use for suggestions.
-
:suggest_mode
(String)
—
The suggest mode. This parameter can be used only when the
suggest_fieldandsuggest_textquery string parameters are specified. Server default: missing. -
:suggest_size
(Integer)
—
The number of suggestions to return. This parameter can be used only when the
suggest_fieldandsuggest_textquery string parameters are specified. -
:suggest_text
(String)
—
The source text for which the suggestions should be returned. This parameter can be used only when the
suggest_fieldandsuggest_textquery string parameters are specified. -
:terminate_after
(Integer)
—
The maximum number of documents to collect for each shard. If a query reaches this limit, Elasticsearch terminates the query early. Elasticsearch collects documents before sorting.IMPORTANT: Use with caution. Elasticsearch applies this parameter to each shard handling the request. When possible, let Elasticsearch perform early termination automatically. Avoid specifying this parameter for requests that target data streams with backing indices across multiple data tiers. If set to
0(default), the query does not terminate early. Server default: 0. -
:timeout
(Time)
—
The period of time to wait for a response from each shard. If no response is received before the timeout expires, the request fails and returns an error. It defaults to no timeout.
-
:track_total_hits
(Boolean, Integer)
—
The number of hits matching the query to count accurately. If
true, the exact number of hits is returned at the cost of some performance. Iffalse, the response does not include the total number of hits matching the query. Server default: 10000. -
:track_scores
(Boolean)
—
If
true, the request calculates and returns document scores, even if the scores are not used for sorting. -
:typed_keys
(Boolean)
—
If
true, aggregation and suggester names are be prefixed by their respective types in the response. -
:rest_total_hits_as_int
(Boolean)
—
Indicates whether
hits.totalshould be rendered as an integer or an object in the rest search response. -
:version
(Boolean)
—
If
true, the request returns the document version as part of a hit. -
:_source
(Boolean, String, Array<String>)
—
The source fields that are returned for matching documents. These fields are returned in the
hits._sourceproperty of the search response. Valid values are:-
trueto return the entire document source. -
falseto not return the document source. -
‘<string>` to return the source fields that are specified as a comma-separated list that supports wildcard (`*`) patterns. Server default: true.
-
-
:_source_excludes
(String, Array<String>)
—
A comma-separated list of source fields to exclude from the response. You can also use this parameter to exclude fields from the subset specified in
_source_includesquery parameter. If the_sourceparameter isfalse, this parameter is ignored. -
:_source_exclude_vectors
(Boolean)
—
Whether vectors should be excluded from _source
-
:_source_includes
(String, Array<String>)
—
A comma-separated list of source fields to include in the response. If this parameter is specified, only these source fields are returned. You can exclude fields from this subset using the
_source_excludesquery parameter. If the_sourceparameter isfalse, this parameter is ignored. -
:seq_no_primary_term
(Boolean)
—
If
true, the request returns the sequence number and primary term of the last modification of each hit. -
:q
(String)
—
A query in the Lucene query string syntax. Query parameter searches do not support the full Elasticsearch Query DSL but are handy for testing.IMPORTANT: This parameter overrides the query parameter in the request body. If both parameters are specified, documents matching the query request body parameter are not returned.
-
:size
(Integer)
—
The number of hits to return. By default, you cannot page through more than 10,000 hits using the
fromandsizeparameters. To page through more hits, use thesearch_afterparameter. Server default: 10. -
:from
(Integer)
—
The starting document offset, which must be non-negative. By default, you cannot page through more than 10,000 hits using the
fromandsizeparameters. To page through more hits, use thesearch_afterparameter. Server default: 0. -
:sort
(String, Array<String>)
—
A comma-separated list of ‘<field>:<direction>` pairs.
-
:force_synthetic_source
(Boolean)
—
Should this request force synthetic _source? Use this to test if the mapping supports synthetic _source and to get a sense of the worst case performance. Fetches with this enabled will be slower the enabling synthetic source natively in the index.
-
:error_trace
(Boolean)
—
When set to
trueElasticsearch will include the full stack trace of errors when they occur. -
:filter_path
(String, Array<String>)
—
Comma-separated list of filters in dot notation which reduce the response returned by Elasticsearch.
-
:human
(Boolean)
—
When set to
truewill return statistics in a format suitable for humans. For example ‘“exists_time”: “1h”` for humans and `“exists_time_in_millis”: 3600000` for computers. When disabled the human readable values will be omitted. This makes sense for responses being consumed only by machines. -
:pretty
(Boolean)
—
If set to
truethe returned JSON will be “pretty-formatted”. Only use this option for debugging only. -
:headers
(Hash)
—
Custom HTTP headers
-
:body
(Hash)
—
request body
See Also:
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/elasticsearch/api/actions/search.rb', line 167 def search(arguments = {}) request_opts = { endpoint: arguments[:endpoint] || 'search' } defined_params = [:index].each_with_object({}) do |variable, set_variables| set_variables[variable] = arguments[variable] if arguments.key?(variable) end request_opts[:defined_params] = defined_params unless defined_params.empty? arguments = arguments.clone headers = arguments.delete(:headers) || {} body = arguments.delete(:body) _index = arguments.delete(:index) method = if body Elasticsearch::API::HTTP_POST else Elasticsearch::API::HTTP_GET end path = if _index "#{Utils.listify(_index)}/_search" else '_search' end params = Utils.process_params(arguments) Elasticsearch::API::Response.new( perform_request(method, path, params, body, headers, request_opts) ) end |