【问题标题】:Elasticsearch: total term frequency and doc count from given set of documentsElasticsearch:来自给定文档集的总词频和文档计数
【发布时间】:2018-06-20 11:26:05
【问题描述】:

我试图从给定的一组文档中获取总词频和文档计数,但是 elasticsearch 中的 _termvectors 从索引内的所有文档中返回 ttf 和 doc_count。有什么方法可以让我指定文档列表(文档 ID),以便结果仅基于这些文档。

以下是获取总词频的文档详细信息和查询:

索引详情:

PUT /twitter
{ "mappings": {
    "tweets": {
      "properties": {
        "name": {
          "type": "text",
          "analyzer":"english"
        }
      }
    }
  },
  "settings" : {
    "index" : {
      "number_of_shards" : 1,
      "number_of_replicas" : 0
    }
  }
}

文档详情:

PUT /twitter/tweets/1
{
  "name":"Hello bar"
}

PUT /twitter/tweets/2
{
  "name":"Hello foo"
}

PUT /twitter/tweets/3
{
  "name":"Hello foo bar"
}

它将创建三个 ID 为 1、2 和 3 的文档。现在假设 ID 为 1 和 2 的推文属于 user1,3 属于另一个用户,我想获取 user1 的术语向量。

查询得到这个结果:

GET /twitter/tweets/_mtermvectors
{
  "ids" : ["1", "2"],
  "parameters": {
      "fields": ["name"],
      "term_statistics": true,
      "offsets":false,
      "payloads":false,
      "positions":false
  }
}

回复:

    {
  "docs": [
    {
      "_index": "twitter",
      "_type": "tweets",
      "_id": "1",
      "_version": 1,
      "found": true,
      "took": 1,
      "term_vectors": {
        "name": {
          "field_statistics": {
            "sum_doc_freq": 7,
            "doc_count": 3,
            "sum_ttf": 7
          },
          "terms": {
            "bar": {
              "doc_freq": 2,
              "ttf": 2,
              "term_freq": 1
            },
            "hello": {
              "doc_freq": 3,
              "ttf": 3,
              "term_freq": 1
            }
          }
        }
      }
    },
    {
      "_index": "twitter",
      "_type": "tweets",
      "_id": "2",
      "_version": 1,
      "found": true,
      "took": 1,
      "term_vectors": {
        "name": {
          "field_statistics": {
            "sum_doc_freq": 7,
            "doc_count": 3,
            "sum_ttf": 7
          },
          "terms": {
            "foo": {
              "doc_freq": 2,
              "ttf": 2,
              "term_freq": 1
            },
            "hello": {
              "doc_freq": 3,
              "ttf": 3,
              "term_freq": 1
            }
          }
        }
      }
    }
  ]
}

在这里我们可以看到hello 的 doc_count 为 3 和 ttf 为 3。我怎样才能让它只考虑具有给定 ID 的文档。

我正在考虑的一种方法是为不同的用户创建不同的索引。但我不确定这种方法是否正确。通过这种方法,指数将随着用户的增加而增加。或者可以有其他解决方案吗?

【问题讨论】:

    标签: elasticsearch term-vectors


    【解决方案1】:

    要获取文档子集上的术语文档计数,您可以尝试使用简单的聚合。

    您必须在字段映射中启用fielddata(虽然它可能会占用内存,请查看documentation page about fielddata 了解更多详细信息):

    PUT /twitter
    { 
      "mappings": {
        "tweets": {
          "properties": {
            "name": {
              "type": "text",
              "analyzer":"english",
              "fielddata": true,
              "term_vector": "yes"
            }
          }
        }
      }
    }
    

    然后使用terms聚合:

    POST /twitter/tweets/_search
    {
      "size": 0,
      "query": {
        "terms": {
          "_id": [
            "1",
            "2"
          ]
        }
      },
      "aggs": {
        "my_term_doc_count": {
          "terms": {
            "field": "name"
          }
        }
      }
    }
    

    响应将是:

    {
      "hits": ...,
      "aggregations": {
        "my_term_doc_count": {
          "doc_count_error_upper_bound": 0,
          "sum_other_doc_count": 0,
          "buckets": [
            {
              "key": "hello",
              "doc_count": 2
            },
            {
              "key": "bar",
              "doc_count": 1
            },
            {
              "key": "foo",
              "doc_count": 1
            }
          ]
        }
      }
    }
    

    但我找不到在文档子集上计算total_term_frequency 的方法,恐怕无法完成。

    我建议使用_analyze API 离线计算术语向量,并将它们显式存储在单独的索引中。通过这种方式,您将能够使用简单的聚合来计算总词频。这里我展示了一个_analyze API 的用法示例。

    POST twitter/_analyze
    {
      "text": "Hello foo bar"
    }
    
    {
      "tokens": [
        {
          "token": "hello",
          "start_offset": 0,
          "end_offset": 5,
          "type": "<ALPHANUM>",
          "position": 0
        },
        {
          "token": "foo",
          "start_offset": 6,
          "end_offset": 9,
          "type": "<ALPHANUM>",
          "position": 1
        },
        {
          "token": "bar",
          "start_offset": 10,
          "end_offset": 13,
          "type": "<ALPHANUM>",
          "position": 2
        }
      ]
    }
    

    希望有帮助!

    【讨论】:

      猜你喜欢
      • 2015-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-29
      • 1970-01-01
      相关资源
      最近更新 更多