Tuan Anh Tran
Tuan Anh Tran

Reputation: 7237

How to set ttl for elasticsearch with the official nodejs library?

So i have an existing index. I close it to updateSettings and then reopen.

client.indices.putSettings({
    index: 'myindex',
    type: 'mytype',
    body: {
        '_ttl': {
            "enabled" : true,
            'default': '1m'
        }
    }
})
.then(function (result) {
    console.log('result', result)
})

And then i create a new document to see if ttl is actually working but it doesn't work.

What i see from the index

Any idea on what i did wrong?

Upvotes: 0

Views: 211

Answers (1)

Val
Val

Reputation: 217304

The _ttl shall be set on the mapping type directly and not on the index so you want to use putMapping instead of putSettings.

client.indices.putMapping({
    index: 'myindex',
    type: 'mytype',
    body: {
        'mytype': {
            '_ttl': {
                "enabled" : true,
                'default': '1m'
            }
        }
    }
})
.then(function (result) {
    console.log('result', result)
})

It is the equivalent of this REST call:

curl -XPUT 'http://localhost:9200/myindex/_mapping/mytype' -d '{
    "mytype": {
        "_ttl": {
             "enabled": true, "default": "1m"
        }
    }
}'

Upvotes: 1

Related Questions