# Mapping util.Map instances to Object fails

**URL:** <https://discuss.elastic.co/t/mapping-util-map-instances-to-object-fails/157684>\
**Category:** Elasticsearch\
**Created:** [November 21, 2018, 10:46am UTC](https://discuss.elastic.co/t/mapping-util-map-instances-to-object-fails/157684 "2018-11-21T10:46:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fabi\_Te](https://avatars.discourse-cdn.com/v4/letter/f/3ab097/32.png) [@Fabi\_Te](https://discuss.elastic.co/u/Fabi_Te)\
**Post date:** [November 21, 2018, 10:46am UTC](https://discuss.elastic.co/t/mapping-util-map-instances-to-object-fails/157684/1 "2018-11-21T10:46:50Z")

</div>

Hi!

Disclaimer: This question is related to Java and a bit Java Spring. This question isn't focusing on Java but the concept of field mapping.

I'd like to index instances of this Java class:

```
public class MyDoc {
  // Other fields omitted
  @Field(type = FieldType.Object, store = true)
  private Map<String, String> fields;
}

```

This class simply contains a classical map called fields. I'd like to have every value of the map be full-text-indexed. The @Field-Attribute tells the Java-Spring library to handle this field as an Object and to store the property.

So what I'd like to see is that when receiving a structure like this ...

```
{
  "fields": {
    "field1": "This should be"
  }
}

```

... Elasticsearch will map field1 and field2 dynamically as full-text-searchable. I expect Elasticsearch to generate this mapping dynamically:

```
"mappings": {
  "document": {
    "properties": {
      "fields": {
        "properties": {
          "field1": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
  }
}

```

But that's not what happens. When I index a document for the first time via my Java application, the dynamically generated mapping looks like this:

```
"mappings": {
  "document": {
  "properties": {
      "fields": {
        "type": "object"
      }
    }
  }
}

```

Even more strange, the indexed document does not contain the fields:

```
{
  "_index": "myindex",
  "_type": "document",
  "_id": "1401529699",
  "_version": 1,
  "found": true,
  "_source": {
    "id": "1401529699"
  }
}

```

I suspect that I need to create a custom mapping that instructs elastic to index every value of the Map field, is that correct?

---

<div class="post-metadata">

**Author:** ![Fabi\_Te](https://avatars.discourse-cdn.com/v4/letter/f/3ab097/32.png) [@Fabi\_Te](https://discuss.elastic.co/u/Fabi_Te)\
**Post date:** [November 21, 2018, 12:08pm UTC](https://discuss.elastic.co/t/mapping-util-map-instances-to-object-fails/157684/2 "2018-11-21T12:08:53Z")

</div>

I discovered that Dynamic Templates are the way to go. As described in [this Stackoverflow answer](https://stackoverflow.com/questions/38242825/spring-elasticsearch-hashmapstring-string-mapping-value-cannot-be-not-analyze) a Dynamic Template needs to be created that instructs Elasticsearch to index every sub-field of a given field in a specific way. In that sense, you are instructing Elasticsearch on how to create dynamic mappings.

Here is the Query that I use to create the template that I will use for my application:

```
PUT /archive_document
{
  "mappings": {
    "document": {
      "dynamic_templates": [
        {
          "map_string": {
            "match_mapping_type": "string",
            "path_match": "fields.*",
            "mapping": {
              "type": "text"
            }
          }
        }
      ]
    }
  }
}

```

**This of course needs further adjustments to really be usable.**

---

<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:** [December 19, 2018, 12:08pm UTC](https://discuss.elastic.co/t/mapping-util-map-instances-to-object-fails/157684/3 "2018-12-19T12:08:54Z")

</div>

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