Elasticsearch: Constant Data Field Type

360 Views Asked by At

Is there a way to add an Elasticsearch data field to an index mapping, such that it always returns a constant numeric value?

I know I can just add a numeric datatype, and then reindex everything with the constant, but I would like to avoid reindexing, and I'd also like to be able to change the constant dynamically without reindexing.


Motivation: Our cluster has a lot of different indexes. We routinely search multiple indexes at once for various reasons. However, when searching multiple indices, our search logic still needs to treat each index slightly differently. One way we could do this is by adding a constant numeric field to each index, and then use that field in our search query.

However, because this is a constant, it seems like we should not need to reindex everything (seems pointless to add a constant value to every record).

1

There are 1 best solutions below

3
Val On

You could use the _meta field for that purpose:

PUT index1
{
  "mappings": {
    "_meta": { 
      "constant": 1
    },
    "properties": {
      ... your fields
    }
  }
}

PUT index2
{
  "mappings": {
    "_meta": { 
      "constant": 2
    },
    "properties": {
      ... your fields
    }
  }
}

You can change that constant value anytime, without any need for reindexing anything. The value is stored at the index level and can be retrieved anytime by simply retrieve the index mapping with GET index1,index2/_mapping