explain.asciidoc 5.1 KB

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