Elastic mapping flattened vs field wise

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, 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:

On this example I used the following template:

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:

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.