Elasticsearch, get average document length

后端 未结 4 1182
陌清茗
陌清茗 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:02

    In ElasticSearch 6.2 you should just use the following line (no need to add 'terms'):

      "aggs" : 
          {"avg_size" : 
               {"avg" : 
                   {"field" : "_size"}}}
    

    See details here: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-avg-aggregation.html

    0 讨论(0)
  • 2021-01-02 05:09

    Shot in the dark, but facets or aggregations combined with a script might do it.

    {
        ...,
    
        "aggs" : {
            "avg_length" : { "avg" : { "script" : "doc['_all'].length" } }
        }
    }
    
    0 讨论(0)
  • 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"}}
      }
    }
    
    0 讨论(0)
  • 2021-01-02 05:26

    The _size mapping field, if enabled, should give you the size of each document for free. Combining this with the avg aggregation should get you what you want. Something like:

    {
      "query" : {"match_all" : {}},
      "aggs" : {"avg_size" : {"avg" : {"terms" : {"field" : "_size"}}}}
    }
    
    0 讨论(0)
提交回复
热议问题