2013-08-28 19:24:34 -04:00
|
|
|
[[search-request-body]]
|
2019-07-19 14:35:36 -04:00
|
|
|
=== Request Body Search
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-09-04 04:52:17 -04:00
|
|
|
Specifies search criteria as request body parameters.
|
|
|
|
|
2019-09-09 12:35:50 -04:00
|
|
|
[source,console]
|
2019-09-04 04:52:17 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
GET /twitter/_search
|
|
|
|
{
|
|
|
|
"query" : {
|
|
|
|
"term" : { "user" : "kimchy" }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
// TEST[setup:twitter]
|
|
|
|
|
|
|
|
|
|
|
|
[[search-request-body-api-request]]
|
|
|
|
==== {api-request-title}
|
|
|
|
|
|
|
|
|
|
|
|
`GET /<index>/_search
|
|
|
|
{
|
|
|
|
"query": {<parameters>}
|
|
|
|
}`
|
|
|
|
|
|
|
|
|
|
|
|
[[search-request-body-api-desc]]
|
|
|
|
==== {api-description-title}
|
|
|
|
|
2013-08-28 19:24:34 -04:00
|
|
|
The search request can be executed with a search DSL, which includes the
|
2019-09-04 04:52:17 -04:00
|
|
|
<<query-dsl,Query DSL>>, within its body.
|
|
|
|
|
|
|
|
|
|
|
|
[[search-request-body-api-path-params]]
|
|
|
|
==== {api-path-parms-title}
|
|
|
|
|
|
|
|
include::{docdir}/rest-api/common-parms.asciidoc[tag=index]
|
|
|
|
|
|
|
|
|
|
|
|
[[search-request-body-api-request-body]]
|
|
|
|
==== {api-request-body-title}
|
|
|
|
|
|
|
|
`allow_partial_search_results`::
|
|
|
|
(Optional, boolean) Set to `false` to fail the request if only partial results
|
|
|
|
are available. Defaults to `true`, which returns partial results in the event
|
|
|
|
of timeouts or partial failures You can override the default behavior for all
|
|
|
|
requests by setting `search.default_allow_partial_results` to `false` in the
|
|
|
|
cluster settings.
|
|
|
|
|
|
|
|
`batched_reduce_size`::
|
|
|
|
(Optional, integer) The number of shard results that should be reduced at once
|
|
|
|
on the coordinating node. This value should be used as a protection mechanism
|
|
|
|
to reduce the memory overhead per search request if the potential number of
|
|
|
|
shards in the request can be large.
|
|
|
|
|
|
|
|
[[ccs-minimize-roundtrips]]
|
|
|
|
`ccs_minimize_roundtrips`::
|
|
|
|
(Optional, boolean) If `true`, the network round-trips between the
|
|
|
|
coordinating node and the remote clusters ewill be minimized when executing
|
2019-12-26 14:20:51 -05:00
|
|
|
{ccs} requests. See <<ccs-network-delays>>. Defaults to `true`.
|
2019-09-04 04:52:17 -04:00
|
|
|
|
|
|
|
include::{docdir}/rest-api/common-parms.asciidoc[tag=from]
|
|
|
|
|
|
|
|
`request_cache`::
|
|
|
|
(Optional, boolean) If `true`, the caching of search results is enabled for
|
|
|
|
requests where `size` is `0`. See <<shard-request-cache>>.
|
|
|
|
|
|
|
|
include::{docdir}/rest-api/common-parms.asciidoc[tag=search_type]
|
|
|
|
|
|
|
|
`size`::
|
|
|
|
(Optional, integer) The number of hits to return. Defaults to `10`.
|
|
|
|
|
|
|
|
include::{docdir}/rest-api/common-parms.asciidoc[tag=terminate_after]
|
|
|
|
|
2019-10-10 04:35:09 -04:00
|
|
|
include::{docdir}/rest-api/common-parms.asciidoc[tag=search_timeout]
|
2019-09-04 04:52:17 -04:00
|
|
|
|
|
|
|
|
|
|
|
Out of the above, the `search_type`, `request_cache` and the
|
|
|
|
`allow_partial_search_results` settings must be passed as query-string
|
|
|
|
parameters. The rest of the search request should be passed within the body
|
|
|
|
itself. The body content can also be passed as a REST parameter named `source`.
|
|
|
|
|
|
|
|
Both HTTP GET and HTTP POST can be used to execute search with body. Since not
|
|
|
|
all clients support GET with body, POST is allowed as well.
|
|
|
|
|
|
|
|
|
|
|
|
[[search-request-body-api-example]]
|
|
|
|
==== {api-examples-title}
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-09-09 12:35:50 -04:00
|
|
|
[source,console]
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
2017-12-14 11:47:53 -05:00
|
|
|
GET /twitter/_search
|
2016-09-14 11:23:25 -04:00
|
|
|
{
|
2013-08-28 19:24:34 -04:00
|
|
|
"query" : {
|
|
|
|
"term" : { "user" : "kimchy" }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-09-14 11:23:25 -04:00
|
|
|
// TEST[setup:twitter]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-09-04 04:52:17 -04:00
|
|
|
|
|
|
|
The API returns the following response:
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-09-06 16:09:09 -04:00
|
|
|
[source,console-result]
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
{
|
2016-09-14 11:23:25 -04:00
|
|
|
"took": 1,
|
|
|
|
"timed_out": false,
|
2016-08-01 08:09:54 -04:00
|
|
|
"_shards":{
|
2016-09-14 11:23:25 -04:00
|
|
|
"total" : 1,
|
|
|
|
"successful" : 1,
|
2017-07-12 16:19:20 -04:00
|
|
|
"skipped" : 0,
|
2016-08-01 08:09:54 -04:00
|
|
|
"failed" : 0
|
|
|
|
},
|
|
|
|
"hits":{
|
2018-12-05 13:49:06 -05:00
|
|
|
"total" : {
|
|
|
|
"value": 1,
|
|
|
|
"relation": "eq"
|
|
|
|
},
|
2019-12-10 12:04:58 -05:00
|
|
|
"max_score": 1.3862942,
|
2016-08-01 08:09:54 -04:00
|
|
|
"hits" : [
|
|
|
|
{
|
|
|
|
"_index" : "twitter",
|
2017-12-14 11:47:53 -05:00
|
|
|
"_type" : "_doc",
|
2016-09-14 11:23:25 -04:00
|
|
|
"_id" : "0",
|
2019-12-10 12:04:58 -05:00
|
|
|
"_score": 1.3862942,
|
2016-08-01 08:09:54 -04:00
|
|
|
"_source" : {
|
|
|
|
"user" : "kimchy",
|
2016-09-14 11:23:25 -04:00
|
|
|
"message": "trying out Elasticsearch",
|
|
|
|
"date" : "2009-11-15T14:12:12",
|
|
|
|
"likes" : 0
|
2016-08-01 08:09:54 -04:00
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
2016-08-01 08:09:54 -04:00
|
|
|
]
|
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-09-14 11:23:25 -04:00
|
|
|
// TESTRESPONSE[s/"took": 1/"took": $body.took/]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2014-08-06 05:54:51 -04:00
|
|
|
|
2019-07-19 14:35:36 -04:00
|
|
|
==== Fast check for any matching docs
|
2015-10-30 12:26:18 -04:00
|
|
|
|
2018-02-12 07:36:33 -05:00
|
|
|
NOTE: `terminate_after` is always applied **after** the `post_filter` and stops
|
|
|
|
the query as well as the aggregation executions when enough hits have been
|
|
|
|
collected on the shard. Though the doc count on aggregations may not reflect
|
|
|
|
the `hits.total` in the response since aggregations are applied **before** the
|
|
|
|
post filtering.
|
|
|
|
|
2015-10-30 12:26:18 -04:00
|
|
|
In case we only want to know if there are any documents matching a
|
|
|
|
specific query, we can set the `size` to `0` to indicate that we are not
|
|
|
|
interested in the search results. Also we can set `terminate_after` to `1`
|
|
|
|
to indicate that the query execution can be terminated whenever the first
|
|
|
|
matching document was found (per shard).
|
|
|
|
|
2019-09-09 12:35:50 -04:00
|
|
|
[source,console]
|
2015-10-30 12:26:18 -04:00
|
|
|
--------------------------------------------------
|
2018-02-12 07:36:33 -05:00
|
|
|
GET /_search?q=message:number&size=0&terminate_after=1
|
2015-10-30 12:26:18 -04:00
|
|
|
--------------------------------------------------
|
2016-09-14 11:23:25 -04:00
|
|
|
// TEST[setup:twitter]
|
2015-10-30 12:26:18 -04:00
|
|
|
|
2019-09-04 04:52:17 -04:00
|
|
|
|
2015-10-30 12:26:18 -04:00
|
|
|
The response will not contain any hits as the `size` was set to `0`. The
|
|
|
|
`hits.total` will be either equal to `0`, indicating that there were no
|
|
|
|
matching documents, or greater than `0` meaning that there were at least
|
|
|
|
as many documents matching the query when it was early terminated.
|
|
|
|
Also if the query was terminated early, the `terminated_early` flag will
|
|
|
|
be set to `true` in the response.
|
|
|
|
|
2019-09-06 16:09:09 -04:00
|
|
|
[source,console-result]
|
2015-10-30 12:26:18 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
{
|
2016-08-01 08:09:54 -04:00
|
|
|
"took": 3,
|
2015-10-30 12:26:18 -04:00
|
|
|
"timed_out": false,
|
|
|
|
"terminated_early": true,
|
|
|
|
"_shards": {
|
2016-08-01 08:09:54 -04:00
|
|
|
"total": 1,
|
|
|
|
"successful": 1,
|
2017-07-12 16:19:20 -04:00
|
|
|
"skipped" : 0,
|
2015-10-30 12:26:18 -04:00
|
|
|
"failed": 0
|
|
|
|
},
|
|
|
|
"hits": {
|
2018-12-05 13:49:06 -05:00
|
|
|
"total" : {
|
|
|
|
"value": 1,
|
|
|
|
"relation": "eq"
|
|
|
|
},
|
2018-08-22 11:23:54 -04:00
|
|
|
"max_score": null,
|
2015-10-30 12:26:18 -04:00
|
|
|
"hits": []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-09-14 11:23:25 -04:00
|
|
|
// TESTRESPONSE[s/"took": 3/"took": $body.took/]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-09-04 04:52:17 -04:00
|
|
|
|
2017-08-15 02:43:26 -04:00
|
|
|
The `took` time in the response contains the milliseconds that this request
|
|
|
|
took for processing, beginning quickly after the node received the query, up
|
|
|
|
until all search related work is done and before the above JSON is returned
|
|
|
|
to the client. This means it includes the time spent waiting in thread pools,
|
|
|
|
executing a distributed search across the whole cluster and gathering all the
|
|
|
|
results.
|
|
|
|
|
2019-09-04 04:52:17 -04:00
|
|
|
|
2019-04-09 10:35:25 -04:00
|
|
|
include::request/docvalue-fields.asciidoc[]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-04-09 10:35:25 -04:00
|
|
|
include::request/explain.asciidoc[]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-04-09 10:35:25 -04:00
|
|
|
include::request/collapse.asciidoc[]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-04-09 10:35:25 -04:00
|
|
|
include::request/from-size.asciidoc[]
|
2019-01-04 14:36:49 -05:00
|
|
|
|
2019-04-09 10:35:25 -04:00
|
|
|
include::request/highlighting.asciidoc[]
|
2013-11-13 10:53:10 -05:00
|
|
|
|
2019-04-09 10:35:25 -04:00
|
|
|
include::request/index-boost.asciidoc[]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-04-09 10:35:25 -04:00
|
|
|
include::request/inner-hits.asciidoc[]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-04-09 10:35:25 -04:00
|
|
|
include::request/min-score.asciidoc[]
|
|
|
|
|
|
|
|
include::request/named-queries-and-filters.asciidoc[]
|
2014-01-09 17:20:06 -05:00
|
|
|
|
2013-12-16 06:11:06 -05:00
|
|
|
include::request/post-filter.asciidoc[]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-04-09 10:35:25 -04:00
|
|
|
include::request/preference.asciidoc[]
|
|
|
|
|
|
|
|
include::request/query.asciidoc[]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
include::request/rescore.asciidoc[]
|
|
|
|
|
2019-04-09 10:35:25 -04:00
|
|
|
include::request/script-fields.asciidoc[]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
include::request/scroll.asciidoc[]
|
|
|
|
|
2019-04-09 10:35:25 -04:00
|
|
|
include::request/search-after.asciidoc[]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-04-09 10:35:25 -04:00
|
|
|
include::request/search-type.asciidoc[]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-04-09 10:35:25 -04:00
|
|
|
include::request/seq-no.asciidoc[]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-04-09 10:35:25 -04:00
|
|
|
include::request/sort.asciidoc[]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-04-09 10:35:25 -04:00
|
|
|
include::request/source-filtering.asciidoc[]
|
2014-10-13 03:41:55 -04:00
|
|
|
|
2019-04-09 10:35:25 -04:00
|
|
|
include::request/stored-fields.asciidoc[]
|
2016-01-12 11:40:34 -05:00
|
|
|
|
2019-04-09 10:35:25 -04:00
|
|
|
include::request/track-total-hits.asciidoc[]
|
2017-01-30 05:47:05 -05:00
|
|
|
|
2019-04-09 10:35:25 -04:00
|
|
|
include::request/version.asciidoc[]
|