explain.asciidoc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. [[search-explain]]
  2. === Explain API
  3. ++++
  4. <titleabbrev>Explain</titleabbrev>
  5. ++++
  6. Returns information about why a specific document matches (or doesn't match) a
  7. query.
  8. [source,console]
  9. --------------------------------------------------
  10. GET /my-index-000001/_explain/0
  11. {
  12. "query" : {
  13. "match" : { "message" : "elasticsearch" }
  14. }
  15. }
  16. --------------------------------------------------
  17. // TEST[setup:messages]
  18. [[search-explain-api-request]]
  19. ==== {api-request-title}
  20. `GET /<index>/_explain/<id>`
  21. `POST /<index>/_explain/<id>`
  22. [[search-explain-api-prereqs]]
  23. ==== {api-prereq-title}
  24. * If the {es} {security-features} are enabled, you must have the `read`
  25. <<privileges-list-indices,index privilege>> for the target index.
  26. [[search-explain-api-desc]]
  27. ==== {api-description-title}
  28. The explain API computes a score explanation for a query and a specific
  29. document. This can give useful feedback whether a document matches or
  30. didn't match a specific query.
  31. [[search-explain-api-path-params]]
  32. ==== {api-path-parms-title}
  33. `<id>`::
  34. (Required, integer) Defines the document ID.
  35. `<index>`::
  36. +
  37. --
  38. (Required, string)
  39. Index names used to limit the request.
  40. Only a single index name can be provided to this parameter.
  41. --
  42. [[search-explain-api-query-params]]
  43. ==== {api-query-parms-title}
  44. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=analyzer]
  45. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=analyze_wildcard]
  46. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=default_operator]
  47. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=df]
  48. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=lenient]
  49. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=preference]
  50. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=search-q]
  51. `stored_fields`::
  52. (Optional, string) A comma-separated list of stored fields to return in the
  53. response.
  54. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=routing]
  55. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=source]
  56. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=source_excludes]
  57. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=source_includes]
  58. [[search-explain-api-request-body]]
  59. ==== {api-request-body-title}
  60. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=query]
  61. [[search-explain-api-example]]
  62. ==== {api-examples-title}
  63. [source,console]
  64. --------------------------------------------------
  65. GET /my-index-000001/_explain/0
  66. {
  67. "query" : {
  68. "match" : { "message" : "elasticsearch" }
  69. }
  70. }
  71. --------------------------------------------------
  72. // TEST[setup:messages]
  73. The API returns the following response:
  74. [source,console-result]
  75. --------------------------------------------------
  76. {
  77. "_index":"my-index-000001",
  78. "_id":"0",
  79. "matched":true,
  80. "explanation":{
  81. "value":1.6943598,
  82. "description":"weight(message:elasticsearch in 0) [PerFieldSimilarity], result of:",
  83. "details":[
  84. {
  85. "value":1.6943598,
  86. "description":"score(freq=1.0), computed as boost * idf * tf from:",
  87. "details":[
  88. {
  89. "value":2.2,
  90. "description":"boost",
  91. "details":[]
  92. },
  93. {
  94. "value":1.3862944,
  95. "description":"idf, computed as log(1 + (N - n + 0.5) / (n + 0.5)) from:",
  96. "details":[
  97. {
  98. "value":1,
  99. "description":"n, number of documents containing term",
  100. "details":[]
  101. },
  102. {
  103. "value":5,
  104. "description":"N, total number of documents with field",
  105. "details":[]
  106. }
  107. ]
  108. },
  109. {
  110. "value":0.5555556,
  111. "description":"tf, computed as freq / (freq + k1 * (1 - b + b * dl / avgdl)) from:",
  112. "details":[
  113. {
  114. "value":1.0,
  115. "description":"freq, occurrences of term within document",
  116. "details":[]
  117. },
  118. {
  119. "value":1.2,
  120. "description":"k1, term saturation parameter",
  121. "details":[]
  122. },
  123. {
  124. "value":0.75,
  125. "description":"b, length normalization parameter",
  126. "details":[]
  127. },
  128. {
  129. "value":3.0,
  130. "description":"dl, length of field",
  131. "details":[]
  132. },
  133. {
  134. "value":5.4,
  135. "description":"avgdl, average length of field",
  136. "details":[]
  137. }
  138. ]
  139. }
  140. ]
  141. }
  142. ]
  143. }
  144. }
  145. --------------------------------------------------
  146. There is also a simpler way of specifying the query via the `q` parameter. The
  147. specified `q` parameter value is then parsed as if the `query_string` query was
  148. used. Example usage of the `q` parameter in the
  149. explain API:
  150. [source,console]
  151. --------------------------------------------------
  152. GET /my-index-000001/_explain/0?q=message:search
  153. --------------------------------------------------
  154. // TEST[setup:messages]
  155. The API returns the same result as the previous request.