# Elastic mapping flattened vs field wise

**URL:** <https://discuss.elastic.co/t/elastic-mapping-flattened-vs-field-wise/362151>\
**Category:** Elasticsearch\
**Created:** [June 27, 2024, 11:33am UTC](https://discuss.elastic.co/t/elastic-mapping-flattened-vs-field-wise/362151 "2024-06-27T11:33:30Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![leandrojmp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leandrojmp/32/107231_2.png) [@leandrojmp](https://discuss.elastic.co/u/leandrojmp)\
**Post date:** [August 2, 2024, 3:39am UTC](https://discuss.elastic.co/t/elastic-mapping-flattened-vs-field-wise/362151/7 "2024-08-02T03:39:05Z")

</div>

Sorry, didn't saw your last questions in this thread.

A couple of things to try to make it clear.

First, Elasticsearch is not a a direct replacement for MongoDB, they work in different ways, MongoDB is schemaless, Elasticsearch is not, you need to have a schema (mappings) defined as this can impact in performance, storage usage and how you will search your data.

If you do not define a mapping for your data before indexing it, Elasticsearch per default will try to infer the data type of a field on the first time it receives a document with the field, this works in some way for string fields, but it may cause issues for other data types like booleans, numeric data types and specially objects.

A wrong mapping leads to the error you shared, a mapping conflict, which makes Elasticsearch drop the documents when the value for a field does not match the expected mapping.

As mentioned [in this previous comment](https://discuss.elastic.co/t/elastic-mapping-flattened-vs-field-wise/362151/3), when you have a field that is highly dynamic, the solution is to map this field as **_flattened_** , this will allow Elasticsearch to store the entire json, but it will also map every nested field as a keyword, so you will not have full text search on this field.

One alternative to this is to also store those dynamics fields as a string, so you can at least get some kind of full text search on it, so you could end up with something like this:

 ![Screenshot from 2024-08-02 00-36-45](https://us1.discourse-cdn.com/elastic/original/3X/b/c/bc4b4fe863a7c732a5f8e62294545a5404e719a7.png)

On this example I used the following template:

```auto
PUT _index_template/discuss
{
  "template": {
    "settings": {
      "number_of_replicas": 0
    },
    "mappings": {
      "properties": {
        "name": {
          "type": "keyword"
        },
        "fieldA": {
          "type": "float"
        },
        "fieldB": {
          "type": "text"
        },
        "fieldC": {
          "type": "keyword"
        },
        "ValuesDocument": {
            "type": "flattened"
        },
        "ValuesDocumentAsText": {
          "type": "text"
        }
      }
    }
  },
  "index_patterns": [
    "discuss"
  ],
  "allow_auto_create": true
}

```

And the following sample document:

```auto
POST /discuss/_doc
{
  "name": "Document 1",
  "fieldA": 123.45,
  "fieldB": "Sample text",
  "fieldC": "keyword_value",
  "ValuesDocument": {
    "al6gq": "true",
    "63c095bebb3a6b02afcb2cbd": "ABC-369",
    "ab4jh" : "anc@gmail.com",
    "awb6p" : "1.2.3.4",
    "afl2f" : {
        "_t": "ValueSelection",
        "_id": "abc",
        "Value": "test string",
        "v": "xyz"
    }
  },
  "ValuesDocumentAsText": "\"ValuesDocument\": { \"al6gq\": \"true\", \"63c095bebb3a6b02afcb2cbd\": \"ABC-369\", \"ab4jh\" : \"anc@gmail.com\", \"awb6p\" : \"1.2.3.4\", \"afl2f\" : {\"_t\": \"ValueSelection\", \"_id\": \"abc\", \"Value\": \"test string\", \"v\": \"xyz\"}}"
}

```

How you transform the `ValuesDocument` field in a string depends entirely on how you are indexing your data.

---

_[View the full topic](https://discuss.elastic.co/t/elastic-mapping-flattened-vs-field-wise/362151)._
