# Explain API

**Explain API**计算查询和特定文档的分数说明。这可以提供有用的反馈，无论文档是否匹配特定查询。

index 和 type 参数分别期望单个索引和单个类型。

## 用法 <a href="#explainapi-yong-fa" id="explainapi-yong-fa"></a>

完整查询示例：

```
GET /twitter/tweet/0/_explain
{
      "query" : {
        "match" : { "message" : "elasticsearch" }
      }
}
```

这将产生以下结果：

```
{
  "_index" : "twitter",
  "_type" : "tweet",
  "_id" : "0",
  "matched" : true,
  "explanation" : {
    "value" : 1.55077,
    "description" : "sum of:",
    "details" : [ {
      "value" : 1.55077,
      "description" : "weight(message:elasticsearch in 0) [PerFieldSimilarity], result of:",
      "details" : [ {
        "value" : 1.55077,
        "description" : "score(doc=0,freq=1.0 = termFreq=1.0\n), product of:",
        "details" : [ {
          "value" : 1.3862944,
          "description" : "idf(docFreq=1, docCount=5)",
          "details" : [ ]
        }, {
          "value" : 1.1186441,
          "description" : "tfNorm, computed from:",
          "details" : [
            { "value" : 1.0, "description" : "termFreq=1.0", "details" : [ ] },
            { "value" : 1.2, "description" : "parameter k1", "details" : [ ] },
            { "value" : 0.75, "description" : "parameter b", "details" : [ ] },
            { "value" : 5.4, "description" : "avgFieldLength", "details" : [ ] },
            { "value" : 4.0, "description" : "fieldLength", "details" : [ ] }
          ]
        } ]
      } ]
    }, {
      "value" : 0.0,
      "description" : "match on required clause, product of:",
      "details" : [ {
        "value" : 0.0,
        "description" : "# clause",
        "details" : [ ]
      }, {
        "value" : 1.0,
        "description" : "_type:tweet, product of:",
        "details" : [
          { "value" : 1.0, "description" : "boost", "details" : [ ] },
          { "value" : 1.0, "description" : "queryNorm", "details" : [ ] }
        ]
      } ]
    } ]
  }
}
```

还有一种更简单的通过 q 参数指定查询的方法。然后解析指定的 q 参数值，就像使用 query\_string 查询一样。在api中的 q 参数的用法示例：

```
GET /twitter/tweet/0/_explain?q=message:search
```

这将产生与先前请求相同的结果。

## 所有的参数 <a href="#explainapi-suo-you-de-can-shu" id="explainapi-suo-you-de-can-shu"></a>

| Name                       | Description                                                                                      |
| -------------------------- | ------------------------------------------------------------------------------------------------ |
| `_source`                  | 设置为true以检索所解释的文档的\_source。您还可以使用\_source\_include＆\_source\_exclude检索文档的一部分（有关更多详细信息，请参阅Get API） |
| `stored_fields`            | 允许控制哪些存储字段作为文档说明的一部分返回。                                                                          |
| `routing`                  | 在索引期间使用路由的情况下控制路由。                                                                               |
| `parent`                   | 与设置`routing`参数的效果相同。                                                                             |
| `preference`               | 控制执行解释的分片。                                                                                       |
| `source`                   | 允许请求的数据放在url的查询字符串中。                                                                             |
| `q`                        | 查询字符串（映射到 query\_string 查询）。                                                                     |
| `df`                       | 在查询中未定义字段前缀时使用的默认字段。默认为\_all字段。                                                                  |
| `analyzer`                 | 分析查询字符串时使用的分析器名称。默认为\_all字段的分析器。                                                                 |
| `analyze_wildcard`         | 是否分析通配符和前缀查询。默认为false。                                                                           |
| `lowercase_expanded_terms` | 术语是否自动小写，默认为**true**。                                                                            |
| `lenient`                  | 如果设置为**true**将导致基于格式的失败（例如向数字字段提供文本）被忽略。默认为**false**。                                            |
| `default_operator`         | 要使用的默认运算符，可以是**AND**或**OR**。默认为**OR**。                                                           |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://xiaoxiami.gitbook.io/elasticsearch/ji-chu/33-apis/343sou-suo-apis-search-apis/explain-api.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
