Elasticsearch:获取给定文档中每个术语的tf-idf

mel*_*mel 5 nlp tf-idf elasticsearch

我在Elasticsearch中有一个具有以下ID的文档:AVosj8FEIaetdb3CXpP-我正在尝试访问tf-idf字段中的每个单词,我做了以下工作:

GET /cnn/cnn_article/AVosj8FEIaetdb3CXpP-/_termvectors
{
  "fields" : ["author_wording"],
  "term_statistics" : true,
  "field_statistics" : true
}'
Run Code Online (Sandbox Code Playgroud)

我得到的答复是:

{
  "_index": "dailystormer",
  "_type": "dailystormer_article",
  "_id": "AVosj8FEIaetdb3CXpP-",
  "_version": 3,
  "found": true,
  "took": 1,
  "term_vectors": {
    "author_wording": {
      "field_statistics": {
        "sum_doc_freq": 3408583,
        "doc_count": 16111,
        "sum_ttf": 7851321
      },
      "terms": {
        "318": {
          "doc_freq": 4,
          "ttf": 4,
          "term_freq": 1,
          "tokens": [
            {
              "position": 121,
              "start_offset": 688,
              "end_offset": 691
            }
          ]
        },
        "742": {
          "doc_freq": 1,
          "ttf": 1,
          "term_freq": 1,
          "tokens": [
            {
              "position": 122,
              "start_offset": 692,
              "end_offset": 695
            }
          ]
        },
        "9971": {
          "doc_freq": 1,
          "ttf": 1,
          "term_freq": 1,
          "tokens": [
            {
              "position": 123,
              "start_offset": 696,
              "end_offset": 700
            }
          ]
        },
        "a": {
          "doc_freq": 14921,
          "ttf": 163268,
          "term_freq": 11,
          "tokens": [
            {
              "position": 1,
              "start_offset": 13,
              "end_offset": 14
            },
            ...
            "you’re": {
          "doc_freq": 1112,
          "ttf": 1647,
          "term_freq": 1,
          "tokens": [
            {
              "position": 80,
              "start_offset": 471,
              "end_offset": 477
            }
          ]
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

它返回了一些有趣的字段,例如频率(tf)而不是tf-idf。我应该自己重新计算吗?这是一个好主意吗?我该怎么办?

art*_*dev 7

您可以使用这个API:

\n\n

https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-termvectors.html

\n\n
{\n   "_index": "imdb",\n   "_type": "_doc",\n   "_version": 0,\n   "found": true,\n   "term_vectors": {\n      "plot": {\n         "field_statistics": {\n            "sum_doc_freq": 3384269,\n            "doc_count": 176214,\n            "sum_ttf": 3753460\n         },\n         "terms": {\n            "armored": {\n               "doc_freq": 27,\n               "ttf": 27,\n               "term_freq": 1,\n               "score": 9.74725\n            },\n            "industrialist": {\n               "doc_freq": 88,\n               "ttf": 88,\n               "term_freq": 1,\n               "score": 8.590818\n            },\n            "stark": {\n               "doc_freq": 44,\n               "ttf": 47,\n               "term_freq": 1,\n               "score": 9.272792\n            }\n         }\n      }\n   }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

术语频率 - 术语频率。术语在一个特定文档的字段中出现的次数。

\n\n

文档频率 - 文档频率。某个术语出现的文档数量。

\n\n

ttf - 总词频。该term在所有文档中出现的次数,即所有文档的tf之和。按字段计算。

\n\n

df 和 ttf 是按分片计算的,因此这些数字可能会根据当前文档所在的分片而有所不同。

\n\n

分数是如何计算的?

\n\n

返回的分数数字主要用于合理地对不同建议进行排名,而不是最终用户易于理解的内容。分数是根据前景和背景集中的文档频率得出的。简而言之,如果某个术语在子集中和背景中出现的频率存在明显差异,则该术语被认为是重要的。可以配置术语的排名方式,请参阅“参数”部分。

\n\n

记住这些定义:

\n\n

\xe2\x80\x93 Elasticsearch 集群由一个或多个节点组成,可通过其集群名称进行识别。

\n\n

节点\xe2\x80\x93 单个 Elasticsearch 实例。在大多数环境中,每个节点都在单独的机器或虚拟机上运行。

\n\n

指数 \xe2\x80\x93 在 Elasticsearch 中,索引是文档的集合。

\n\n

shard \xe2\x80\x93 因为 Elasticsearch 是一个分布式搜索引擎,索引通常被分割成分布在多个节点上的称为分片的元素。Elasticsearch 自动管理这些分片的排列。它还会根据需要重新平衡分片,因此用户无需担心细节。

\n\n

副本\xe2\x80\x93 默认情况下,Elasticsearch 为每个索引创建 5 个主分片和一个副本。这意味着每个索引将由五个主分片组成,每个分片将有一个副本。

\n\n

分配多个分片和副本是分布式搜索功能设计的本质,为索引内的文档搜索提供高可用性和快速访问。主分片和副本分片之间的主要区别在于,只有主分片可以接受索引请求。副本分片和主分片都可以服务查询请求。

\n


Mys*_*ion 5

是的,它返回给您一个tf-术语频率(您在此字段中同时拥有术语频率,还有ttf-这是总术语频率,例如所有字段中所有tf的总和)和df-文档频率(您在响应中也有它) 。您需要确定仅在您的字段或所有字段中要计算哪个tf-idf。要计算tf-idf,您需要执行以下操作:

tf-idf = tf * idf
Run Code Online (Sandbox Code Playgroud)

哪里

idf = log (N / df)
Run Code Online (Sandbox Code Playgroud)

以及N = doc_count您的回应。Elasticsearch不提供用于计算tf-idf的实现,因此您需要自己执行。