search-your-data.asciidoc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. [[search-your-data]]
  2. = Search your data
  3. [[search-query]]
  4. A _search query_, or _query_, is a request for information about data in
  5. {es} data streams or indices.
  6. You can think of a query as a question, written in a way {es} understands.
  7. Depending on your data, you can use a query to get answers to questions like:
  8. * What processes on my server take longer than 500 milliseconds to respond?
  9. * What users on my network ran `regsvr32.exe` within the last week?
  10. * What pages on my website contain a specific word or phrase?
  11. A _search_ consists of one or more queries that are combined and sent to {es}.
  12. Documents that match a search's queries are returned in the _hits_, or
  13. _search results_, of the response.
  14. A search may also contain additional information used to better process its
  15. queries. For example, a search may be limited to a specific index or only return
  16. a specific number of results.
  17. [discrete]
  18. [[run-an-es-search]]
  19. == Run a search
  20. You can use the <<search-search,search API>> to search and
  21. <<search-aggregations,aggregate>> data stored in {es} data streams or indices.
  22. The API's `query` request body parameter accepts queries written in
  23. <<query-dsl,Query DSL>>.
  24. The following request searches `my-index-000001` using a
  25. <<query-dsl-match-query,`match`>> query. This query matches documents with a
  26. `user.id` value of `kimchy`.
  27. [source,console]
  28. ----
  29. GET /my-index-000001/_search
  30. {
  31. "query": {
  32. "match": {
  33. "user.id": "kimchy"
  34. }
  35. }
  36. }
  37. ----
  38. // TEST[setup:my_index]
  39. The API response returns the top 10 documents matching the query in the
  40. `hits.hits` property.
  41. [source,console-result]
  42. ----
  43. {
  44. "took": 5,
  45. "timed_out": false,
  46. "_shards": {
  47. "total": 1,
  48. "successful": 1,
  49. "skipped": 0,
  50. "failed": 0
  51. },
  52. "hits": {
  53. "total": {
  54. "value": 1,
  55. "relation": "eq"
  56. },
  57. "max_score": 1.3862942,
  58. "hits": [
  59. {
  60. "_index": "my-index-000001",
  61. "_id": "kxWFcnMByiguvud1Z8vC",
  62. "_score": 1.3862942,
  63. "_source": {
  64. "@timestamp": "2099-11-15T14:12:12",
  65. "http": {
  66. "request": {
  67. "method": "get"
  68. },
  69. "response": {
  70. "bytes": 1070000,
  71. "status_code": 200
  72. },
  73. "version": "1.1"
  74. },
  75. "message": "GET /search HTTP/1.1 200 1070000",
  76. "source": {
  77. "ip": "127.0.0.1"
  78. },
  79. "user": {
  80. "id": "kimchy"
  81. }
  82. }
  83. }
  84. ]
  85. }
  86. }
  87. ----
  88. // TESTRESPONSE[s/"took": 5/"took": "$body.took"/]
  89. // TESTRESPONSE[s/"_id": "kxWFcnMByiguvud1Z8vC"/"_id": "$body.hits.hits.0._id"/]
  90. [discrete]
  91. [[common-search-options]]
  92. === Common search options
  93. You can use the following options to customize your searches.
  94. *Query DSL* +
  95. <<query-dsl,Query DSL>> supports a variety of query types you can mix and match
  96. to get the results you want. Query types include:
  97. * <<query-dsl-bool-query,Boolean>> and other <<compound-queries,compound
  98. queries>>, which let you combine queries and match results based on multiple
  99. criteria
  100. * <<term-level-queries,Term-level queries>> for filtering and finding exact matches
  101. * <<full-text-queries,Full text queries>>, which are commonly used in search
  102. engines
  103. * <<geo-queries,Geo>> and <<shape-queries,spatial queries>>
  104. *Aggregations* +
  105. You can use <<search-aggregations,search aggregations>> to get statistics and
  106. other analytics for your search results. Aggregations help you answer questions
  107. like:
  108. * What's the average response time for my servers?
  109. * What are the top IP addresses hit by users on my network?
  110. * What is the total transaction revenue by customer?
  111. *Search multiple data streams and indices* +
  112. You can use comma-separated values and grep-like index patterns to search
  113. several data streams and indices in the same request. You can even boost search
  114. results from specific indices. See <<search-multiple-indices>>.
  115. *Paginate search results* +
  116. By default, searches return only the top 10 matching hits. To retrieve
  117. more or fewer documents, see <<paginate-search-results>>.
  118. *Retrieve selected fields* +
  119. The search response's `hit.hits` property includes the full document
  120. <<mapping-source-field,`_source`>> for each hit. To retrieve only a subset of
  121. the `_source` or other fields, see <<search-fields>>.
  122. *Sort search results* +
  123. By default, search hits are sorted by `_score`, a <<relevance-scores,relevance
  124. score>> that measures how well each document matches the query. To customize the
  125. calculation of these scores, use the
  126. <<query-dsl-script-score-query,`script_score`>> query. To sort search hits by
  127. other field values, see <<sort-search-results>>.
  128. *Run an async search* +
  129. {es} searches are designed to run on large volumes of data quickly, often
  130. returning results in milliseconds. For this reason, searches are
  131. _synchronous_ by default. The search request waits for complete results before
  132. returning a response.
  133. However, complete results can take longer for searches across
  134. <<frozen-indices,frozen indices>> or <<modules-cross-cluster-search,multiple
  135. clusters>>.
  136. To avoid long waits, you can use run an _asynchronous_, or _async_, search
  137. instead. An <<async-search-intro,async search>> lets you retrieve partial
  138. results for a long-running search now and get complete results later.
  139. [discrete]
  140. [[search-timeout]]
  141. === Search timeout
  142. By default, search requests don't time out. The request waits for complete
  143. results before returning a response.
  144. While <<async-search-intro,async search>> is designed for long-running
  145. searches, you can also use the `timeout` parameter to specify a duration you'd
  146. like to wait for a search to complete. If no response is received before this
  147. period ends, the request fails and returns an error.
  148. [source,console]
  149. ----
  150. GET /my-index-000001/_search
  151. {
  152. "timeout": "2s",
  153. "query": {
  154. "match": {
  155. "user.id": "kimchy"
  156. }
  157. }
  158. }
  159. ----
  160. // TEST[setup:my_index]
  161. To set a cluster-wide default timeout for all search requests, configure
  162. `search.default_search_timeout` using the <<cluster-update-settings,cluster
  163. settings API>>. This global timeout duration is used if no `timeout` argument is
  164. passed in the request. If the global search timeout expires before the search
  165. request finishes, the request is cancelled using <<task-cancellation,task
  166. cancellation>>. The `search.default_search_timeout` setting defaults to `-1` (no
  167. timeout).
  168. [discrete]
  169. [[global-search-cancellation]]
  170. === Search cancellation
  171. You can cancel a search request using the <<task-cancellation,task management
  172. API>>. {es} also automatically cancels a search request when your client's HTTP
  173. connection closes. We recommend you set up your client to close HTTP connections
  174. when a search request is aborted or times out.
  175. include::request/track-total-hits.asciidoc[]
  176. include::quickly-check-for-matching-docs.asciidoc[]
  177. include::request/collapse.asciidoc[]
  178. include::filter-search-results.asciidoc[]
  179. include::request/highlighting.asciidoc[]
  180. include::{es-repo-dir}/async-search.asciidoc[]
  181. include::{es-repo-dir}/search/near-real-time.asciidoc[]
  182. include::paginate-search-results.asciidoc[]
  183. include::request/inner-hits.asciidoc[]
  184. include::search-fields.asciidoc[]
  185. include::{es-repo-dir}/modules/cross-cluster-search.asciidoc[]
  186. include::search-multiple-indices.asciidoc[]
  187. include::search-shard-routing.asciidoc[]
  188. include::request/sort.asciidoc[]