1

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).

speedplane
  • 15,673
  • 16
  • 86
  • 138

1 Answers1

2

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

Val
  • 207,596
  • 13
  • 358
  • 360
  • But can you use `_meta` in a query? We would need to run a query along the lines of `(_meta.constant=1 AND text=apple) OR (_meta.constant=2 AND text=orange)` – speedplane Jun 21 '19 at 21:39
  • No you cannot, we since each index has a contant value, why not using the index name in the query in the first place? – Val Jun 22 '19 at 05:10
  • any feedback so far? – Val Jun 27 '19 at 07:26