# Add mapping to unmapped field

**URL:** <https://discuss.elastic.co/t/add-mapping-to-unmapped-field/242369>\
**Category:** Elasticsearch\
**Created:** [July 23, 2020, 4:10pm UTC](https://discuss.elastic.co/t/add-mapping-to-unmapped-field/242369 "2020-07-23T16:10:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![JulienCarnec](https://avatars.discourse-cdn.com/v4/letter/j/3ec8ea/32.png) [@JulienCarnec](https://discuss.elastic.co/u/JulienCarnec)\
**Post date:** [July 23, 2020, 4:10pm UTC](https://discuss.elastic.co/t/add-mapping-to-unmapped-field/242369/1 "2020-07-23T16:10:21Z")

</div>

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](http://localhost:9200/twitter)

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

```

2- create a doc with 2 fields.

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

```

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

```auto
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"

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

```

5- search for the doc

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

```

6- create a second doc

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

```

7- search for the new doc

```auto
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
        }
      }
    ]
  }
}

```

---

<div class="post-metadata">

**Author:** ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)\
**Post date:** [July 23, 2020, 8:13pm UTC](https://discuss.elastic.co/t/add-mapping-to-unmapped-field/242369/2 "2020-07-23T20:13:50Z")

</div>

No, unfortunately, currently this is not possible, you would have to reindex.  
But we are working on a new feature: [runtime fields](https://github.com/elastic/elasticsearch/issues/59332), where it would be possible to define fields during runtime without reindexing.

---

<div class="post-metadata">

**Author:** ![JulienCarnec](https://avatars.discourse-cdn.com/v4/letter/j/3ec8ea/32.png) [@JulienCarnec](https://discuss.elastic.co/u/JulienCarnec)\
**Post date:** [July 23, 2020, 8:45pm UTC](https://discuss.elastic.co/t/add-mapping-to-unmapped-field/242369/3 "2020-07-23T20:45:16Z")

</div>

Thank you for the quick confirmation.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 20, 2020, 8:45pm UTC](https://discuss.elastic.co/t/add-mapping-to-unmapped-field/242369/4 "2020-08-20T20:45:18Z")

</div>

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