如何在 Elasticsearch 中计算嵌套文档相关性分数 (TF/IDF)?

How is a nested document relevance score (TF/IDF) calculated in Elasticsearch?

当运行对嵌套字段进行匹配查询时,每个嵌套文档的相关性分数是基于所有根文档中的所有嵌套文档计算的,还是仅基于单个根文档下的嵌套文档计算的?基本上在计算TF/IDF时,用于IDF的集合范围是多少?

这是一个嵌套文档:

PUT /channels_index
{
  "mappings": {
    "channel": {
      "properties": {
        "username": { "type": "string" },
        "posts": {
          "type": "nested", 
          "properties": {
            "link":    { "type": "string" },
            "caption": { "type": "string" },
          }
        }
      }
    }
  }
}

这里是查询:

GET channels/_search
{
  "query": {
    "nested": {
      "path": "posts",
      "query": {
        "match": {
          "posts.caption": "adidas"
        }
      },
      "inner_hits": {}
    }
  }
}

然而,在我的结果中,即使第二个文档的内部命中最高得分更高,第一个文档的根得分也更高。

{
  "hits": {
    "total": 2,
    "max_score": 4.3327584,
    "hits": [
      {
        "_index": "channels",
        "_type": "channel",
        "_id": "1",
        "_score": 4.3327584,
        "_source": {
          "username": "user1",
          "posts": [...]
        },
        "inner_hits": {
          "posts": {
            "hits": {
              "total": 2,
              "max_score": 5.5447335,
              "hits": [...]
            }
          }
        }
      },
      {
        "_index": "channels",
        "_type": "channel",
        "_id": "2",
        "_score": 4.2954993,
        "_source": {
          "username": "user2",
          "posts": [...]
        },
        "inner_hits": {
          "posts": {
            "hits": {
              "total": 13,
              "max_score": 11.446381,
              "hits": [...]
            }
          }
        }
      }
    ]
  }
}

在对我的查询进行 运行 解释后,我可以看到内部命中的 TF/IDF 分数确实使用了从所有根文档的嵌套文档计算得出的 IDF。

对于根文档评分,嵌套文档默认的评分方式是平均评分。如果我想使用嵌套文档的最大分数,我可以通过定义 score_mode 来设置它。下面的查询显示了如何运行 在文档上进行解释以及设置不同的评分模式。

GET channels/channel/1/_explain
{
  "query": {
    "nested": {
      "path": "posts",
      "score_mode": "max", 
      "query": {
        "match": {
          "posts.caption": "adidas"
        }
      },
      "inner_hits": {}
    }
  }
}