1

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

YeeP
  • 61
  • 1
  • 6
  • whats is that timeReference variable for. can you post how that json object looks. – user3775217 Jun 09 '17 at 07:34
  • the timeReference variable is merely a string that currently contains 'START_TS'. I hope to change it to other strings as I move forward, but it will always be just one string. – YeeP Jun 09 '17 at 12:36

1 Answers1

0

I think what you are asking is more of javascript to convert strings to symbols.

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 : {
                        toSymbol(timeReference): {
                            lte: dataOlderThan
                        }
                    }
                }
            }
        }
    }
}


function toSymbol(variable) {
  return Symbol(variable);
};

In rails we use something like following for this.

key_name = 'age'

hash = {
    name: 'john',
    key_name.to_sym => '23'
}
user3775217
  • 4,675
  • 1
  • 22
  • 33