This version of the OpenSearch documentation is no longer maintained. For the latest version, see the current documentation. For information about OpenSearch version maintenance, see Release Schedule and Maintenance Policy.

Explain

Introduced 1.0

Wondering why a specific document ranks higher (or lower) for a query? You can use the explain API for an explanation of how the relevance score (_score) is calculated for every result.

OpenSearch uses a probabilistic ranking framework called Okapi BM25 to calculate relevance scores. Okapi BM25 is based on the original TF/IDF framework used by Apache Lucene.

The explain API is an expensive operation in terms of both resources and time. On production clusters, we recommend using it sparingly for the purpose of troubleshooting.

Example

To see the explain output for all results, set the explain flag to true either in the URL or in the body of the request:

  1. POST opensearch_dashboards_sample_data_ecommerce/_search?explain=true
  2. {
  3. "query": {
  4. "match": {
  5. "customer_first_name": "Mary"
  6. }
  7. }
  8. }

copy

More often, you want the output for a single document. In that case, specify the document ID in the URL:

  1. POST opensearch_dashboards_sample_data_ecommerce/_explain/EVz1Q3sBgg5eWQP6RSte
  2. {
  3. "query": {
  4. "match": {
  5. "customer_first_name": "Mary"
  6. }
  7. }
  8. }

copy

Path and HTTP methods

  1. GET <target>/_explain/<id>
  2. POST <target>/_explain/<id>

URL parameters

You must specify the index and document ID. All other URL parameters are optional.

ParameterTypeDescriptionRequired
<index>StringName of the index. You can only specify a single index.Yes
<_id>StringA unique identifier to attach to the document.Yes
analyzerStringThe analyzer to use in the query string.No
analyze_wildcardBooleanSpecifies whether to analyze wildcard and prefix queries. Default is false.No
default_operatorStringIndicates whether the default operator for a string query should be AND or OR. Default is OR.No
dfStringThe default field in case a field prefix is not provided in the query string.No
lenientBooleanSpecifies whether OpenSearch should ignore format-based query failures (for example, querying a text field for an integer). Default is false.No
preferenceStringSpecifies a preference of which shard to retrieve results from. Available options are _local, which tells the operation to retrieve results from a locally allocated shard replica, and a custom string value assigned to a specific shard replica. By default, OpenSearch executes the explain operation on random shards.No
qStringQuery in the Lucene query string syntax.No
stored_fieldsBooleanIf true, the operation retrieves document fields stored in the index rather than the document’s _source. Default is false.No
routingStringValue used to route the operation to a specific shard.No
_sourceStringWhether to include the _source field in the response body. Default is true.No
_source_excludesStringA comma-separated list of source fields to exclude in the query response.No
_source_includesStringA comma-separated list of source fields to include in the query response.No

Response

  1. {
  2. "_index" : "kibana_sample_data_ecommerce",
  3. "_id" : "EVz1Q3sBgg5eWQP6RSte",
  4. "matched" : true,
  5. "explanation" : {
  6. "value" : 3.5671005,
  7. "description" : "weight(customer_first_name:mary in 1) [PerFieldSimilarity], result of:",
  8. "details" : [
  9. {
  10. "value" : 3.5671005,
  11. "description" : "score(freq=1.0), computed as boost * idf * tf from:",
  12. "details" : [
  13. {
  14. "value" : 2.2,
  15. "description" : "boost",
  16. "details" : [ ]
  17. },
  18. {
  19. "value" : 3.4100041,
  20. "description" : "idf, computed as log(1 + (N - n + 0.5) / (n + 0.5)) from:",
  21. "details" : [
  22. {
  23. "value" : 154,
  24. "description" : "n, number of documents containing term",
  25. "details" : [ ]
  26. },
  27. {
  28. "value" : 4675,
  29. "description" : "N, total number of documents with field",
  30. "details" : [ ]
  31. }
  32. ]
  33. },
  34. {
  35. "value" : 0.47548598,
  36. "description" : "tf, computed as freq / (freq + k1 * (1 - b + b * dl / avgdl)) from:",
  37. "details" : [
  38. {
  39. "value" : 1.0,
  40. "description" : "freq, occurrences of term within document",
  41. "details" : [ ]
  42. },
  43. {
  44. "value" : 1.2,
  45. "description" : "k1, term saturation parameter",
  46. "details" : [ ]
  47. },
  48. {
  49. "value" : 0.75,
  50. "description" : "b, length normalization parameter",
  51. "details" : [ ]
  52. },
  53. {
  54. "value" : 1.0,
  55. "description" : "dl, length of field",
  56. "details" : [ ]
  57. },
  58. {
  59. "value" : 1.1206417,
  60. "description" : "avgdl, average length of field",
  61. "details" : [ ]
  62. }
  63. ]
  64. }
  65. ]
  66. }
  67. ]
  68. }
  69. }

Response body fields

FieldDescription
matchedIndicates if the document is a match for the query.
explanationThe explanation object has three properties: value, description, and details. The value shows the result of the calculation, the description explains what type of calculation is performed, and the details shows any subcalculations performed.
Term frequency (tf)How many times the term appears in a field for a given document. The more times the term occurs the higher is the relevance score.
Inverse document frequency (idf)How often the term appears within the index (across all the documents). The more often the term appears the lower is the relevance score.
Field normalization factor (fieldNorm)The length of the field. OpenSearch assigns a higher relevance score to a term appearing in a relatively short field.

The tf, idf, and fieldNorm values are calculated and stored at index time when a document is added or updated. The values might have some (typically small) inaccuracies as it’s based on summing the samples returned from each shard.

Individual queries include other factors for calculating the relevance score, such as term proximity, fuzziness, and so on.