troubleshooting-searches.asciidoc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. "my-field": {
  53. "type": "keyword"
  54. },
  55. "my-num-field": {
  56. "type": "integer"
  57. }
  58. }
  59. }
  60. }
  61. ----
  62. ////
  63. [source,console]
  64. ----
  65. GET /my-index-000001/_count
  66. ----
  67. //TEST[continued]
  68. NOTE: When getting no search results in {kib}, check that you have selected the
  69. correct data view and a valid time range. Also, ensure the data view has been
  70. configured with the correct time field.
  71. [discrete]
  72. [[troubleshooting-searches-field-exists-caps]]
  73. === Check that the field exists and its capabilities
  74. Querying a field that does not exist will not return any results. Use the
  75. <<search-field-caps,field capabilities API>> to check whether a field exists:
  76. [source,console]
  77. ----
  78. GET /my-index-000001/_field_caps?fields=my-field
  79. ----
  80. //TEST[continued]
  81. If the field does not exist, check the data ingestion process. The field may
  82. have a different name.
  83. If the field exists, the request will return the field's type and whether it is
  84. searchable and aggregatable.
  85. [source,console-response]
  86. ----
  87. {
  88. "indices": [
  89. "my-index-000001"
  90. ],
  91. "fields": {
  92. "my-field": {
  93. "keyword": {
  94. "type": "keyword", <1>
  95. "metadata_field": false,
  96. "searchable": true, <2>
  97. "aggregatable": true <3>
  98. }
  99. }
  100. }
  101. }
  102. ----
  103. <1> The field is of type `keyword` in this index.
  104. <2> The field is searchable in this index.
  105. <3> The field is aggregatable in this index.
  106. [discrete]
  107. [[troubleshooting-searches-mappings]]
  108. === Check the field's mappings
  109. A field's capabilities are determined by its <<mapping,mapping>>. To retrieve
  110. the mapping, use the <<indices-get-mapping,get mapping API>>:
  111. [source,console]
  112. ----
  113. GET /my-index-000001/_mappings
  114. ----
  115. //TEST[continued]
  116. If you query a `text` field, pay attention to the analyzer that may have been
  117. configured. You can use the <<indices-analyze,analyze API>> to check how a
  118. field's analyzer processes values and query terms:
  119. [source,console]
  120. ----
  121. GET /my-index-000001/_analyze
  122. {
  123. "field" : "my-field",
  124. "text" : "this is a test"
  125. }
  126. ----
  127. //TEST[continued]
  128. To change the mapping of an existing field, refer to
  129. <<updating-field-mappings,Changing the mapping of a field>>.
  130. [discrete]
  131. [[troubleshooting-check-field-values]]
  132. === Check the field's values
  133. Use the <<query-dsl-exists-query,`exists` query>> to check whether there are
  134. documents that return a value for a field. Check that `count` in the response is
  135. not 0.
  136. [source,console]
  137. ----
  138. GET /my-index-000001/_count
  139. {
  140. "query": {
  141. "exists": {
  142. "field": "my-field"
  143. }
  144. }
  145. }
  146. ----
  147. //TEST[continued]
  148. If the field is aggregatable, you can use <<search-aggregations,aggregations>>
  149. to check the field's values. For `keyword` fields, you can use a
  150. <<search-aggregations-bucket-terms-aggregation,terms aggregation>> to retrieve
  151. the field's most common values:
  152. [source,console]
  153. ----
  154. GET /my-index-000001/_search?filter_path=aggregations
  155. {
  156. "size": 0,
  157. "aggs": {
  158. "top_values": {
  159. "terms": {
  160. "field": "my-field",
  161. "size": 10
  162. }
  163. }
  164. }
  165. }
  166. ----
  167. //TEST[continued]
  168. For numeric fields, you can use the
  169. <<search-aggregations-metrics-stats-aggregation,stats aggregation>> to get an
  170. idea of the field's value distribution:
  171. [source,console]
  172. ----
  173. GET my-index-000001/_search?filter_path=aggregations
  174. {
  175. "aggs": {
  176. "my-num-field-stats": {
  177. "stats": {
  178. "field": "my-num-field"
  179. }
  180. }
  181. }
  182. }
  183. ----
  184. //TEST[continued]
  185. If the field does not return any values, check the data ingestion process. The
  186. field may have a different name.
  187. [discrete]
  188. [[troubleshooting-searches-validate-explain-profile]]
  189. === Validate, explain, and profile queries
  190. When a query returns unexpected results, Elasticsearch offers several tools to
  191. investigate why.
  192. The <<search-validate,validate API>> enables you to validate a query. Use the
  193. `rewrite` parameter to return the Lucene query an Elasticsearch query is
  194. rewritten into:
  195. [source,console]
  196. --------------------------------------------------
  197. GET /my-index-000001/_validate/query?rewrite=true
  198. {
  199. "query": {
  200. "match": {
  201. "user.id": {
  202. "query": "kimchy",
  203. "fuzziness": "auto"
  204. }
  205. }
  206. }
  207. }
  208. --------------------------------------------------
  209. //TEST[continued]
  210. Use the <<search-explain,explain API>> to find out why a specific document
  211. matches or doesn’t match a query:
  212. [source,console]
  213. --------------------------------------------------
  214. GET /my-index-000001/_explain/0
  215. {
  216. "query" : {
  217. "match" : { "message" : "elasticsearch" }
  218. }
  219. }
  220. --------------------------------------------------
  221. // TEST[setup:messages]
  222. The <<search-profile,profile API>> provides detailed timing information about a
  223. search request. For a visual representation of the results, use the
  224. {kibana-ref}/xpack-profiler.html[Search Profiler] in {kib}.
  225. NOTE: To troubleshoot queries in {kib}, select **Inspect** in the toolbar. Next,
  226. select **Request**. You can now copy the query {kib} sent to {es} for
  227. further analysis in Console.
  228. [discrete]
  229. [[troubleshooting-searches-settings]]
  230. === Check index settings
  231. <<index-modules-settings,Index settings>> can influence search results. For
  232. example, the `index.query.default_field` setting, which determines the field
  233. that is queried when a query specifies no explicit field. Use the
  234. <<indices-get-settings,get index settings API>> to retrieve the settings for an
  235. index:
  236. [source,console]
  237. ----
  238. GET /my-index-000001/_settings
  239. ----
  240. //TEST[continued]
  241. You can update dynamic index settings with the <<indices-update-settings,update
  242. index settings API>>. <<change-dynamic-index-setting-for-a-data-stream,Changing
  243. dynamic index settings for a data stream>> requires changing the index template
  244. used by the data stream.
  245. For static settings, you need to create a new index with the correct settings.
  246. Next, you can reindex the data into that index. For data streams, refer to
  247. <<change-static-index-setting-for-a-data-stream,Change a static index setting
  248. for a data stream>>.