Elasticsearch获取文档中某个字段的大小

Elasticsearch access the size of a certain field in the documents

我知道通过字段 _size 我们可以告诉 Elasticsearch 存储文档的大小。这是我为索引定义的映射:

'{
   "mappings":{
      "_default_":{
         "_size":{
            "enabled":true,
            "store":true
         }
      }
   }
}'

_size 字段给出了整个文档的大小,而我只需要某个字段的大小。

例如,假设我们在映射中有两个字段。 messageurl。即

curl -XPUT localhost:9200/indexname/tweet/00001 -d '{"message": "some message", "url" : "http://someurl.url"}'

是否可以仅获取上述映射中消息的长度(或存储大小)?

您可以使用 script fields

{
    {
    "query" : {
        ...
    },
    "script_fields" : {
        "message_length" : {
            "script" : "doc['message'].value.size()"
        }
    }
}