Search code examples
kotlinspring-data-elasticsearch

Spring data elastic search MultiField annotation on nested object ignored


I created a asciifolding.json file holding the configuration for the "ignore accents" analyser.

I used the @MultiField annotation on fields of my top level object and this works without a problem.

But, when I use the same annotation on a field of a nested object, it is ignored.

@Document(indexName = "indexName")
@Setting(settingPath = "asciifolding.json")
class TopLevelObject(
   @Id
   val id: String,
   // ----------------------------------------------------------- THIS WORKS
   @MultiField(
        mainField = Field(type = FieldType.Text, analyzer = "ascii_folding"),
        otherFields = [
            InnerField(type = FieldType.Keyword, suffix = "keyword")
        ]
    )
   val name: String,
   val nestedObject: NestedObject
)

@Setting(settingPath = "asciifolding.json")
class NestedObject(
   val aField: String,
   // --------------------------------------------------------- THIS DOESN'T
   @MultiField(
        mainField = Field(type = FieldType.Text, analyzer = "ascii_folding"),
        otherFields = [
            InnerField(type = FieldType.Keyword, suffix = "keyword")
        ]
    )
   val anotherField: String
)

Result of GET /indexName/_mapping:

{
    "indexName": {
        "mappings": {
            "properties": {
                "_class": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "id": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "name": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    },
                    "analyzer": "ascii_folding"      // APPEARS HERE
                },
                "nestedObject": {
                    "properties": {
                        "aField": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "anotherField": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }                       // DOESN'T HERE
                        }
                    }
                }
            }
        }
    }
}

Any idea on what what I'm missing?


Solution

  • The mapping builder won't consider properties that aren't annotated with @Field. So you need to add this:

    @Document(indexName = "indexName")
    @Setting(settingPath = "asciifolding.json")
    class TopLevelObject(
       @Id
       val id: String,
       // ----------------------------------------------------------- THIS WORKS
       @MultiField(
            mainField = Field(type = FieldType.Text, analyzer = "ascii_folding"),
            otherFields = [
                InnerField(type = FieldType.Keyword, suffix = "keyword")
            ]
        )
       val name: String,
       @Field(type = FieldType.Nested)  // <-- !!! add this field type definition
       val nestedObject: NestedObject
    )
    

    btw, the @Setting annotation is only checked on the entity defining the index, so on the top level. It is ignored on the nested class.