Neel
Neel

Reputation: 21243

elasticsearch not return ttl in fields if not enabled in mappings

I created index and doc in . Add mapping for doc.

curl http://localhost:9200/test -X POST
{"acknowledged":true}

curl http://localhost:9200/test/student_doc/_mappings -X PUT -d '{
   "student_doc" : {
     "properties" : {
       "name" : {
         "properties" : {
           "student_id" : {
             "type" : "string"
           },
           "tags": {
             "type" : "string"
           }
         }
       }
     }
   }
 }'
{"acknowledged":true}

When I create doc, I gave ttl for the doc.

curl http://localhost:9200/test/student_doc/4?ttl=2500 -X PUT -d '{"student_id": "4", "tags": ["test"]}' -H 'Content-type: application/json'
{"_index":"test","_type":"student_doc","_id":"4","_version":1,"created":true}'

When I try to get the ttl in fields

curl http://localhost:9200/test/_search?pretty -X POST -d '{"fields": ["_ttl"]}'
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "test",
      "_type" : "student_doc",
      "_id" : "4",
      "_score" : 1.0
    } ]
  }
}

I enable ttl in index using new mappings.

curl http://localhost:9200/test/student_doc/_mappings -X PUT -d '{
  "student_doc" : {
    "_ttl": {"enabled": true},
    "properties" : {
      "name" : {
        "properties" : {
          "student_id" : {
            "type" : "string"
          },
          "tags": {
            "type" : "string"
          }
        }
      }
    }
  }
}'

Then add new record.

curl "http://localhost:9200/test/student_doc/5?ttl=2500&pretty" -X PUT -d '{"student_id": "5", "tags": ["test"]}' -H 'Content-type: application/json'
{
  "_index" : "test",
  "_type" : "student_doc",
  "_id" : "5",
  "_version" : 1,
  "created" : true
}

And try to get ttl again and it returns the ttl in fields.

curl http://localhost:9200/test/_search?pretty -X POST -d '{"fields": ["_ttl"]}'
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "test",
      "_type" : "student_doc",
      "_id" : "4",
      "_score" : 1.0
    }, {
      "_index" : "test",
      "_type" : "student_doc",
      "_id" : "5",
      "_score" : 1.0,
      "fields" : {
        "_ttl" : -420
      }
    } ]
  }
}

It is compulsory to enabled ttl in mappings to get it effected in document ?

Upvotes: 0

Views: 83

Answers (1)

Val
Val

Reputation: 217294

Yes, _ttl is not enabled by default, so you need to enable it in order for TTL to work, but it won't affect already created documents.

Note that the ttl parameter is silently ignored if _ttl is not enabled in your mapping, you won't get any error because of that. It's part of your job to know your mappings and if you enabled TTL or not.

You can enable _ttl at any time, so given the increased work to support it, you should only enable it when needed.

Upvotes: 1

Related Questions