Elasticsearch, get average document length

后端 未结 4 1217
陌清茗
陌清茗 2021-01-02 04:47

Is there any better way in elasticsearch (other than issuing a match all query and manually averaging over the length of all returned documents) to get the average document

4条回答
  •  半阙折子戏
    2021-01-02 05:18

    I have used this code (I have the _source enabled)

    {
      "query" : {"match_all" : {}},
      "aggs":{
        "avg_length" : { "avg" : { "script" : "_source.toString().length()"}}
      }
    }
    

    Well, the chars .. .if the string are UTF-8 to get the bytes:

    {
      "query" : {"match_all" : {}},
      "aggs":{
        "avg_length" : { "avg" : { "script" : "_source.toString().getBytes(\"UTF-8\").length"}}
      }
    }
    

提交回复
热议问题