elasticsearchelasticsearch-mapping

Use condition in Elasticsearch mapping


I'd like to know if it's possible to use conditions when I define the mapping of an index, because depending on certain values of a field, other fields are then required.

For example :

PUT /my-index
{
  "mappings": {
    "properties": {
      "age": { "type": "integer" },  
      "email": { "type": "keyword" }, 
      "name": { "type": "text"  }, 
      "is_external": {"type": "boolean" }
      if [is_external] == true {
        "adress": { "type": "text" },
        "date": { "type": "date" }
      }
    }
  }
}

If there's no way to do this, how can I deal with it ?


Solution

  • It doesn't make sense to do this kind of checks at the mapping level, because ultimately your index will contain both documents with is_external: true and is_external: false and so the mapping will have to also contain the address and date field definitions for the documents where is_external: true and there can only be one single mapping for each index.

    If you want to enforce that a document with is_external: true also contains the address and date fields, then you can do this using an ingest pipeline with a drop processor:

    ...
    {
      "drop": {
        "if" : "ctx.is_external == true && (ctx.date == null || ctx.address == null)"
      }
    }