Mapping util.Map instances to Object fails


(Fabi Te) #1

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?


(Fabi Te) #2

I discovered that Dynamic Templates are the way to go. As described in this Stackoverflow answer 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.