troubleshooting-searches.asciidoc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. [[troubleshooting-searches]]
  2. == Troubleshooting searches
  3. When you query your data, Elasticsearch may return an error, no search results,
  4. or results in an unexpected order. This guide describes how to troubleshoot
  5. searches.
  6. [discrete]
  7. [[troubleshooting-searches-exists]]
  8. === Ensure the data stream, index, or alias exists
  9. Elasticsearch returns an `index_not_found_exception` when the data stream, index
  10. or alias you try to query does not exist. This can happen when you misspell the
  11. name or when the data has been indexed to a different data stream or index.
  12. Use the <<indices-exists,exists API>> to check whether a data stream, index, or
  13. alias exists:
  14. [source,console]
  15. ----
  16. HEAD my-data-stream
  17. ----
  18. Use the <<data-stream-stats-api,data stream stats API>> to list all data
  19. streams:
  20. [source,console]
  21. ----
  22. GET /_data_stream/_stats?human=true
  23. ----
  24. Use the <<indices-get-index,get index API>> to list all indices and their
  25. aliases:
  26. [source,console]
  27. ----
  28. GET _all?filter_path=*.aliases
  29. ----
  30. Instead of an error, it is possible to retrieve partial search results if some
  31. of the indices you're querying are unavailable. Set `ignore_unavailable` to
  32. `true`:
  33. [source,console]
  34. ----
  35. GET /my-alias/_search?ignore_unavailable=true
  36. ----
  37. [discrete]
  38. [[troubleshooting-searches-data]]
  39. === Ensure the data stream or index contains data
  40. When a search request returns no hits, the data stream or index may contain no
  41. data. This can happen when there is a data ingestion issue. For example, the
  42. data may have been indexed to a data stream or index with another name.
  43. Use the <<search-count,count API>> to retrieve the number of documents in a data
  44. stream or index. Check that `count` in the response is not 0.
  45. ////
  46. [source,console]
  47. ----
  48. PUT my-index-000001
  49. {
  50. "mappings": {
  51. "properties": {
  52. "@timestamp": {
  53. "type": "date"
  54. },
  55. "my-field": {
  56. "type": "keyword"
  57. },
  58. "my-num-field": {
  59. "type": "integer"
  60. }
  61. }
  62. }
  63. }
  64. ----
  65. ////
  66. [source,console]
  67. ----
  68. GET /my-index-000001/_count
  69. ----
  70. //TEST[continued]
  71. NOTE: When getting no search results in {kib}, check that you have selected the
  72. correct data view and a valid time range. Also, ensure the data view has been
  73. configured with the correct time field.
  74. [discrete]
  75. [[troubleshooting-searches-field-exists-caps]]
  76. === Check that the field exists and its capabilities
  77. Querying a field that does not exist will not return any results. Use the
  78. <<search-field-caps,field capabilities API>> to check whether a field exists:
  79. [source,console]
  80. ----
  81. GET /my-index-000001/_field_caps?fields=my-field
  82. ----
  83. //TEST[continued]
  84. If the field does not exist, check the data ingestion process. The field may
  85. have a different name.
  86. If the field exists, the request will return the field's type and whether it is
  87. searchable and aggregatable.
  88. [source,console-response]
  89. ----
  90. {
  91. "indices": [
  92. "my-index-000001"
  93. ],
  94. "fields": {
  95. "my-field": {
  96. "keyword": {
  97. "type": "keyword", <1>
  98. "metadata_field": false,
  99. "searchable": true, <2>
  100. "aggregatable": true <3>
  101. }
  102. }
  103. }
  104. }
  105. ----
  106. <1> The field is of type `keyword` in this index.
  107. <2> The field is searchable in this index.
  108. <3> The field is aggregatable in this index.
  109. [discrete]
  110. [[troubleshooting-searches-mappings]]
  111. === Check the field's mappings
  112. A field's capabilities are determined by its <<mapping,mapping>>. To retrieve
  113. the mapping, use the <<indices-get-mapping,get mapping API>>:
  114. [source,console]
  115. ----
  116. GET /my-index-000001/_mappings
  117. ----
  118. //TEST[continued]
  119. If you query a `text` field, pay attention to the analyzer that may have been
  120. configured. You can use the <<indices-analyze,analyze API>> to check how a
  121. field's analyzer processes values and query terms:
  122. [source,console]
  123. ----
  124. GET /my-index-000001/_analyze
  125. {
  126. "field" : "my-field",
  127. "text" : "this is a test"
  128. }
  129. ----
  130. //TEST[continued]
  131. To change the mapping of an existing field, refer to
  132. <<updating-field-mappings,Changing the mapping of a field>>.
  133. [discrete]
  134. [[troubleshooting-check-field-values]]
  135. === Check the field's values
  136. Use the <<query-dsl-exists-query,`exists` query>> to check whether there are
  137. documents that return a value for a field. Check that `count` in the response is
  138. not 0.
  139. [source,console]
  140. ----
  141. GET /my-index-000001/_count
  142. {
  143. "query": {
  144. "exists": {
  145. "field": "my-field"
  146. }
  147. }
  148. }
  149. ----
  150. //TEST[continued]
  151. If the field is aggregatable, you can use <<search-aggregations,aggregations>>
  152. to check the field's values. For `keyword` fields, you can use a
  153. <<search-aggregations-bucket-terms-aggregation,terms aggregation>> to retrieve
  154. the field's most common values:
  155. [source,console]
  156. ----
  157. GET /my-index-000001/_search?filter_path=aggregations
  158. {
  159. "size": 0,
  160. "aggs": {
  161. "top_values": {
  162. "terms": {
  163. "field": "my-field",
  164. "size": 10
  165. }
  166. }
  167. }
  168. }
  169. ----
  170. //TEST[continued]
  171. For numeric fields, you can use the
  172. <<search-aggregations-metrics-stats-aggregation,stats aggregation>> to get an
  173. idea of the field's value distribution:
  174. [source,console]
  175. ----
  176. GET my-index-000001/_search?filter_path=aggregations
  177. {
  178. "aggs": {
  179. "my-num-field-stats": {
  180. "stats": {
  181. "field": "my-num-field"
  182. }
  183. }
  184. }
  185. }
  186. ----
  187. //TEST[continued]
  188. If the field does not return any values, check the data ingestion process. The
  189. field may have a different name.
  190. [discrete]
  191. [[troubleshooting-searches-latest-data]]
  192. === Check the latest value
  193. For time-series data, confirm there is non-filtered data within the attempted
  194. time range. For example, if you are trying to query the latest data for the
  195. `@timestamp` field, run the following to see if the max `@timestamp` falls
  196. within the attempted range:
  197. [source,console]
  198. ----
  199. GET my-index-000001/_search?sort=@timestamp:desc&size=1
  200. ----
  201. //TEST[continued]
  202. [discrete]
  203. [[troubleshooting-searches-validate-explain-profile]]
  204. === Validate, explain, and profile queries
  205. When a query returns unexpected results, Elasticsearch offers several tools to
  206. investigate why.
  207. The <<search-validate,validate API>> enables you to validate a query. Use the
  208. `rewrite` parameter to return the Lucene query an Elasticsearch query is
  209. rewritten into:
  210. [source,console]
  211. --------------------------------------------------
  212. GET /my-index-000001/_validate/query?rewrite=true
  213. {
  214. "query": {
  215. "match": {
  216. "user.id": {
  217. "query": "kimchy",
  218. "fuzziness": "auto"
  219. }
  220. }
  221. }
  222. }
  223. --------------------------------------------------
  224. //TEST[continued]
  225. Use the <<search-explain,explain API>> to find out why a specific document
  226. matches or doesn’t match a query:
  227. [source,console]
  228. --------------------------------------------------
  229. GET /my-index-000001/_explain/0
  230. {
  231. "query" : {
  232. "match" : { "message" : "elasticsearch" }
  233. }
  234. }
  235. --------------------------------------------------
  236. // TEST[setup:messages]
  237. The <<search-profile,profile API>> provides detailed timing information about a
  238. search request. For a visual representation of the results, use the
  239. {kibana-ref}/xpack-profiler.html[Search Profiler] in {kib}.
  240. NOTE: To troubleshoot queries in {kib}, select **Inspect** in the toolbar. Next,
  241. select **Request**. You can now copy the query {kib} sent to {es} for
  242. further analysis in Console.
  243. [discrete]
  244. [[troubleshooting-searches-settings]]
  245. === Check index settings
  246. <<index-modules-settings,Index settings>> can influence search results. For
  247. example, the `index.query.default_field` setting, which determines the field
  248. that is queried when a query specifies no explicit field. Use the
  249. <<indices-get-settings,get index settings API>> to retrieve the settings for an
  250. index:
  251. [source,console]
  252. ----
  253. GET /my-index-000001/_settings
  254. ----
  255. //TEST[continued]
  256. You can update dynamic index settings with the <<indices-update-settings,update
  257. index settings API>>. <<change-dynamic-index-setting-for-a-data-stream,Changing
  258. dynamic index settings for a data stream>> requires changing the index template
  259. used by the data stream.
  260. For static settings, you need to create a new index with the correct settings.
  261. Next, you can reindex the data into that index. For data streams, refer to
  262. <<change-static-index-setting-for-a-data-stream,Change a static index setting
  263. for a data stream>>.
  264. [discrete]
  265. [[troubleshooting-slow-searches]]
  266. === Find slow queries
  267. <<index-modules-slowlog,Slow logs>> can help pinpoint slow performing search
  268. requests. Enabling <<auditing-settings,audit logging>> on top can help determine
  269. query source. Add the following settings to the `elasticsearch.yml` configuration file
  270. to trace queries. The resulting logging is verbose, so disable these settings when not
  271. troubleshooting.
  272. [source,yaml]
  273. ----
  274. xpack.security.audit.enabled: true
  275. xpack.security.audit.logfile.events.include: _all
  276. xpack.security.audit.logfile.events.emit_request_body: true
  277. ----
  278. Refer to
  279. https://www.elastic.co/blog/advanced-tuning-finding-and-fixing-slow-elasticsearch-queries[Advanced
  280. tuning: finding and fixing slow Elasticsearch queries] for more information.