Phil B
Phil B

Reputation: 6037

Does an empty field in a document take up space in elasticsearch?

Does an empty field in a document take up space in elasticsearch? For example, in the case below, is the total amount of space used to store the document the same in Case A as in Case B (assuming the field "colors" is defined in the mapping).

Case A
    {"features":
      "price": 1,
      "colors":[]
    }

Case B
    {"features":
      "price": 1,
    }

Upvotes: 3

Views: 811

Answers (1)

ThomasC
ThomasC

Reputation: 8165

If you keep the default settings, the original document is stored in the _source field, there will be a difference as the original document of case A is bigger than case B.

Otherwise, there should be no difference : for case A, no term is added in the index for the colors field as it's empty.

You can use the _size field to see the size of the original document indexed, which is the size of the _source field :

POST stack
{
  "mappings":{
    "features":{
      "_size": {"enabled":true, "store":true},
      "properties":{
        "price":{
          "type":"byte"
        },
        "colors":{
          "type":"string"
        }
      }
    }
  }
}

PUT stack/features/1
{
  "price": 1
}

PUT stack/features/2
{
  "price": 1,
  "colors": []
}

POST stack/features/_search
{
  "fields": [
    "_size"
  ]
}

The last query will output this result, which shows than document 2 takes more space than 1:

{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 2,
      "max_score": 1,
      "hits": [
         {
            "_index": "stack",
            "_type": "features",
            "_id": "1",
            "_score": 1,
            "fields": {
               "_size": 16
            }
         },
         {
            "_index": "stack",
            "_type": "features",
            "_id": "2",
            "_score": 1,
            "fields": {
               "_size": 32
            }
         }
      ]
   }
}

Upvotes: 4

Related Questions