Home > Enterprise >  elasticsearch aliases manipulation with jq
elasticsearch aliases manipulation with jq

Time:08-05

I'm kinda loosing my hair over this one so maybe you can help I need to manipulate elasticsearch output with jq to get a valid json that i can add to ES later on.

what i have is:

{
  "testindex_123": {
    "aliases": {
      "testindex": {}
    }
  },
  "testindex_122": {
    "aliases": {
      "testindex": {}
    }
  }
}

and i want to create a script/run jq on that so it would produce

{
    "actions" : [
    {
        "add" : {
        "index" : "testindex_122",
        "alias" : "testindex",
        "is_write_index": false
        }
    },
    {
        "add" : {
        "index" : "testindex_123",
        "alias" : "testindex",
        "is_write_index": true
        }
    }
    ]
}

And why? I'm setting up elasticsearch snapshot&restore workflow that would leave aliases and once reindex/restore is complete replace the indices based on epoch time so in this example

testindex_122

will get replaced with

testindex_123

while testindex alias will be active. It seems doable but i really have no idea how to set this up :/

any pointers would be great Thanks!

CodePudding user response:

I don't understand where the boolean value should come from, but the rest could be generated by:

{actions: to_entries | map({add: {
  index: .key,
  alias: .value.aliases | keys[],
  is_write_index: true
}})}
{
  "actions": [
    {
      "add": {
        "index": "testindex_123",
        "alias": "testindex",
        "is_write_index": true
      }
    },
    {
      "add": {
        "index": "testindex_122",
        "alias": "testindex",
        "is_write_index": true
      }
    }
  ]
}

Demo


Addressing the boolean values, OP clarified:

is it possible for boolean to come from higher number? for instance i will always want to restore from date in the future. so in that case 123 > 122therefore 122== false 123 == true. Does that make any sense?

Store the maximum of those values in a variable, and while iterating, compare the current one to it:

def keynum: (.key / "_")[-1] | tonumber;

{actions: (to_entries | (map(keynum) | max) as $max | map({add: {
  index: .key,
  alias: .value.aliases | keys[],
  is_write_index: (keynum == $max)
}}))}
{
  "actions": [
    {
      "add": {
        "index": "testindex_123",
        "alias": "testindex",
        "is_write_index": true
      }
    },
    {
      "add": {
        "index": "testindex_122",
        "alias": "testindex",
        "is_write_index": false
      }
    }
  ]
}

Demo

  • Related