run-a-search.asciidoc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. [[run-a-search]]
  2. == Run a search
  3. You can use the <<search-search,search API>> to search data stored in
  4. {es} data streams or indices.
  5. The API can run two types of searches, depending on how you provide
  6. <<search-query,queries>>:
  7. <<run-uri-search,URI searches>>::
  8. Queries are provided through a query parameter. URI searches tend to be
  9. simpler and best suited for testing.
  10. <<run-request-body-search,Request body searches>>::
  11. Queries are provided through the JSON body of the API request. These queries
  12. are written in <<query-dsl,Query DSL>>. We recommend using request body
  13. searches in most production use cases.
  14. [WARNING]
  15. ====
  16. If you specify a query in both the URI and request body, the search API request
  17. runs only the URI query.
  18. ====
  19. [discrete]
  20. [[run-uri-search]]
  21. === Run a URI search
  22. You can use the search API's <<search-api-query-params-q,`q` query string
  23. parameter>> to run a search in the request's URI. The `q` parameter only accepts
  24. queries written in Lucene's <<query-string-syntax,query string syntax>>.
  25. .*Example*
  26. [%collapsible]
  27. ====
  28. To get started, ingest or add some data to an {es} index.
  29. The following <<docs-bulk,bulk API>> request adds some example user log data to
  30. the `user_logs_000001` index.
  31. [source,console]
  32. ----
  33. PUT /user_logs_000001/_bulk?refresh
  34. {"index":{"_index" : "user_logs_000001", "_id" : "1"}}
  35. { "@timestamp": "2020-12-06T11:04:05.000Z", "user": { "id": "vlb44hny" }, "message": "Login attempt failed" }
  36. {"index":{"_index" : "user_logs_000001", "_id" : "2"}}
  37. { "@timestamp": "2020-12-07T11:06:07.000Z", "user": { "id": "8a4f500d" }, "message": "Login successful" }
  38. {"index":{"_index" : "user_logs_000001", "_id" : "3"}}
  39. { "@timestamp": "2020-12-07T11:07:08.000Z", "user": { "id": "l7gk7f82" }, "message": "Logout successful" }
  40. ----
  41. You can now use the search API to run a URI search on this index.
  42. The following URI search matches documents with a `user.id` value of `l7gk7f82`.
  43. Note the query is specified using the `q` query string parameter.
  44. [source,console]
  45. ----
  46. GET /user_logs_000001/_search?q=user.id:8a4f500d
  47. ----
  48. // TEST[continued]
  49. The API returns the following response. Note the `hits.hits` property contains
  50. the document that matched the query.
  51. [source,console-result]
  52. ----
  53. {
  54. "took": 2,
  55. "timed_out": false,
  56. "_shards": {
  57. "total": 1,
  58. "successful": 1,
  59. "skipped": 0,
  60. "failed": 0
  61. },
  62. "hits": {
  63. "total": {
  64. "value": 1,
  65. "relation": "eq"
  66. },
  67. "max_score": 0.9808291,
  68. "hits": [
  69. {
  70. "_index": "user_logs_000001",
  71. "_id": "2",
  72. "_score": 0.9808291,
  73. "_source": {
  74. "@timestamp": "2020-12-07T11:06:07.000Z",
  75. "user": {
  76. "id": "8a4f500d"
  77. },
  78. "message": "Login successful"
  79. }
  80. }
  81. ]
  82. }
  83. }
  84. ----
  85. // TESTRESPONSE[s/"took": 2/"took": "$body.took"/]
  86. ====
  87. [discrete]
  88. [[run-request-body-search]]
  89. === Run a request body search
  90. You can use the search API's <<request-body-search-query,`query` request
  91. body parameter>> to provide a query as a JSON object, written in
  92. <<query-dsl,Query DSL>>.
  93. .*Example*
  94. [%collapsible]
  95. ====
  96. The following request body search uses the <<query-dsl-match-query,`match`>>
  97. query to match documents with a `message` value of `login successful`. Note the
  98. `match` query is specified as a JSON object in the `query` parameter.
  99. [source,console]
  100. ----
  101. GET /user_logs_000001/_search
  102. {
  103. "query": {
  104. "match": {
  105. "message": "login successful"
  106. }
  107. }
  108. }
  109. ----
  110. // TEST[continued]
  111. The API returns the following response.
  112. The `hits.hits` property contains matching documents. By default, the response
  113. sorts these matching documents by `_score`, a <<relevance-scores,relevance
  114. score>> that measures how well each document matches the query.
  115. [source,console-result]
  116. ----
  117. {
  118. "took": 1,
  119. "timed_out": false,
  120. "_shards": {
  121. "total": 1,
  122. "successful": 1,
  123. "skipped": 0,
  124. "failed": 0
  125. },
  126. "hits": {
  127. "total": {
  128. "value": 3,
  129. "relation": "eq"
  130. },
  131. "max_score": 0.9983525,
  132. "hits": [
  133. {
  134. "_index": "user_logs_000001",
  135. "_id": "2",
  136. "_score": 0.9983525,
  137. "_source": {
  138. "@timestamp": "2020-12-07T11:06:07.000Z",
  139. "user": {
  140. "id": "8a4f500d"
  141. },
  142. "message": "Login successful"
  143. }
  144. },
  145. {
  146. "_index": "user_logs_000001",
  147. "_id": "3",
  148. "_score": 0.49917626,
  149. "_source": {
  150. "@timestamp": "2020-12-07T11:07:08.000Z",
  151. "user": {
  152. "id": "l7gk7f82"
  153. },
  154. "message": "Logout successful"
  155. }
  156. },
  157. {
  158. "_index": "user_logs_000001",
  159. "_id": "1",
  160. "_score": 0.42081726,
  161. "_source": {
  162. "@timestamp": "2020-12-06T11:04:05.000Z",
  163. "user": {
  164. "id": "vlb44hny"
  165. },
  166. "message": "Login attempt failed"
  167. }
  168. }
  169. ]
  170. }
  171. }
  172. ----
  173. // TESTRESPONSE[s/"took": 1/"took": "$body.took"/]
  174. ====
  175. [discrete]
  176. [[search-multiple-indices]]
  177. === Search multiple data streams and indices
  178. To search multiple data streams and indices, add them as comma-separated values
  179. in the search API request path.
  180. .*Example*
  181. [%collapsible]
  182. ====
  183. The following request searches the `user_logs_000001` and `user_logs_000002`
  184. indices.
  185. [source,console]
  186. ----
  187. GET /user_logs_000001,user_logs_000002/_search
  188. {
  189. "query": {
  190. "match": {
  191. "message": "login successful"
  192. }
  193. }
  194. }
  195. ----
  196. // TEST[continued]
  197. // TEST[s/^/PUT user_logs_000002\n/]
  198. ====
  199. You can also search multiple data streams and indices using a wildcard (`*`)
  200. pattern.
  201. .*Example*
  202. [%collapsible]
  203. ====
  204. The following request targets the wildcard pattern `user_logs*`. The request
  205. searches any data streams or indices in the cluster that start with `user_logs`.
  206. [source,console]
  207. ----
  208. GET /user_logs*/_search
  209. {
  210. "query": {
  211. "match": {
  212. "message": "login successful"
  213. }
  214. }
  215. }
  216. ----
  217. // TEST[continued]
  218. ====
  219. To search all data streams and indices in a cluster, omit the target from the
  220. request path. Alternatively, you can use `_all` or `*`.
  221. .*Example*
  222. [%collapsible]
  223. ====
  224. The following requests are equivalent and search all data streams and indices in the cluster.
  225. [source,console]
  226. ----
  227. GET /_search
  228. {
  229. "query": {
  230. "match": {
  231. "message": "login successful"
  232. }
  233. }
  234. }
  235. GET /_all/_search
  236. {
  237. "query": {
  238. "match": {
  239. "message": "login successful"
  240. }
  241. }
  242. }
  243. GET /*/_search
  244. {
  245. "query" : {
  246. "match" : { "message" : "login" }
  247. }
  248. }
  249. ----
  250. // TEST[continued]
  251. ====
  252. include::request/from-size.asciidoc[]
  253. include::search-fields.asciidoc[]