Add mapping to unmapped field

Is there a way to store an unmapped field in documents, and then later on, create a mapping for this unmapped field to have it searchable, without having to reindex all my docs in a new index?

Here is what I do:

1- create an index with a single mapped field, and dynamic mapping disabled

PUT http://localhost:9200/twitter

{
    "settings" : {
        "number_of_shards" : 1
    },
    "mappings" : {
        "_doc" : {
             "dynamic":"false",
            "properties" : {
                "field1" : { "type" : "text" }
            }
        }
    }
}

2- create a doc with 2 fields.

POST http://localhost:9200/twitter/_doc/1
{
    "field1" : "foo",
    "value": 1234
}

3- read the doc to check that the "value" field is stored

GET http://localhost:9200/twitter/_doc/1
returns: 
{
  "_index": "twitter",
  "_type": "_doc",
  "_id": "1",
  "_version": 1,
  "_seq_no": 0,
  "_primary_term": 1,
  "found": true,
  "_source": {
    "field1": "foo",
    "value": 1234
  }
}

4- add a mapping for the unmapped field "value"

PUT  http://localhost:9200/twitter/_doc/_mapping
{
  "properties": {
    "value": {
      "type": "long"
    }
  }
}

5- search for the doc

POST 
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "value": "1234"
          }
        }
      ]
    }
  }
}
returns no hit

6- create a second doc

POST http://localhost:9200/twitter/_doc/2
{
    "field1" : "bar",
    "value": 5678
}

7- search for the new doc

POST 
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "value": "5678"
          }
        }
      ]
    }
  }
}
The newly created doc is returned:
{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "twitter",
        "_type": "_doc",
        "_id": "2",
        "_score": 1,
        "_source": {
          "field": "bar",
          "value": 5678
        }
      }
    ]
  }
}

No, unfortunately, currently this is not possible, you would have to reindex.
But we are working on a new feature: runtime fields, where it would be possible to define fields during runtime without reindexing.

1 Like

Thank you for the quick confirmation.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.