【问题标题】:how to get total tokens count in documents in elasticsearch如何在elasticsearch中获取文档中的总令牌数
【发布时间】:2019-11-10 07:22:24
【问题描述】:

我正在尝试获取与查询匹配的文档中的令牌总数。我还没有定义任何自定义映射,并且我想要获取令牌计数的字段是“字符串”类型。

我尝试了以下查询,但它给出了一个非常大的数字,大约为 10^20,这不是我的数据集的正确答案。

curl -XPOST 'localhost:9200/nodename/comment/_search?pretty' -d '
{
   "query": {
      "match_all": {}
   },
   "aggs": {
      "tk_count": {
         "sum": {
            "script": "_index[\"body\"].sumttf()"
         }
      }
   },
   "size": 0
}

知道如何获得所有令牌的正确计数吗? (我不需要每个学期的计数,而是总计数)。

【问题讨论】:

    标签: elasticsearch elasticsearch-py


    【解决方案1】:

    这对我有用,是你需要的吗?

    我的解决方案stores the token count on indexing using the token_count datatype. 不是在查询中获取令牌计数(使用tk_count 聚合,如另一个答案中所建议的那样),以便我可以在查询结果中返回“name.stored_length”值。

    token_count 是一个“多字段”,它一次只能处理一个字段(即“名称”字段或“正文”字段)。我将示例稍微修改为 store "name.stored_length"

    请注意,在我的示例中,它确实计算令牌的基数(即不同的值),它计算总令牌; “John John Doe”中有 3 个标记; "name.stored_length"===3; (即使它的计数不同的标记只有 2)。注意我要求具体的"stored_fields" : ["name.stored_length"]

    最后,您可能需要重新更新您的文档(即发送PUT),或任何技术来获得您想要的值!在这种情况下,我PUT“John John Doe”,即使它在 elasticsearch 中已经是POST/PUT;直到PUT 再次,在将令牌添加到映射之后,令牌才被计算在内。!)

    PUT test_token_count
    {
      "mappings": {
        "_doc": {
          "properties": {
            "name": { 
              "type": "text",
              "fields": {
                "stored_length": { 
                  "type":     "token_count",
                  "analyzer": "standard",
         //------------------v
                  "store": true
                }
              }
            }
          }
        }
      }
    }
    
    PUT test_token_count/_doc/1
    {
        "name": "John John Doe" 
    }
    

    现在我们可以查询或搜索结果,并将结果配置为包含name.stored_length 字段(它既是多字段又是存储字段!):

    GET/POST test_token_count/_search
    {
          //------------------v
        "stored_fields" : ["name.stored_length"]
    }
    

    并且搜索结果应包括令牌总数为named.stored_length...

    {
      ...
      "hits": {
         ...
        "hits": [
          {
            "_index": "test_token_count",
            "_type": "_doc",
            "_id": "1",
            "_score": 1,
            "fields": {
     //------------------v
              "name.stored_length": [
                3
              ]
            }
          }
        ]
      }
    }
    

    【讨论】:

      【解决方案2】:

      您似乎想在正文字段中检索 cardinality 的总令牌。

      在这种情况下,您可以像下面这样使用cardinality aggregation

      curl -XPOST 'localhost:9200/nodename/comment/_search?pretty' -d '
      {
          "query": {
              "match_all": {}
          },
          "aggs": {
              "tk_count": {
                  "cardinality" : {
                      "field" : "body"
                  }
              }
          },
          "size": 0
      }
      

      详情请见this official document

      【讨论】:

      • 谢谢。但是“基数”聚合将计算不同的值。我想知道令牌的总数,而不是唯一令牌的数量。有什么想法吗?
      • 이승진,这个问题是给你的。
      猜你喜欢
      • 1970-01-01
      • 2012-11-04
      • 2023-04-03
      • 1970-01-01
      • 2021-03-14
      • 1970-01-01
      • 2021-12-04
      • 2023-01-19
      • 1970-01-01
      相关资源
      最近更新 更多