Thursday, 8 June 2017

Dynamically change elasticsearch query definition

So I am trying to dynamically delete specific documents from elastic and would like to use one function without hard coding the name of the item I am looking for the time range in. See code below:

exports.cleanElastic = function(type, timeReference){
    return new Promise (function (resolve, reject){
        let dataOlderThan = "now-".concat(config.amountOfData);
        elastic.deleteByQuery({
            index: 'canary',
            type: type,
            body: {
                query: {
                    range : {
                        START_TS: {
                            lte: dataOlderThan
                        }
                    }
                }
            }
        },

As you can see 'START_TS' is the name of the date field I care about in this instance. That will not always be the case with the project. So I am trying to pass 'timeReference' or at least its' value in for where the query reads 'START_TS'. Any suggestions would be much appreciated.

Thank you,

Ryan



via YeeP

No comments:

Post a Comment