Spring 忽略嵌套对象上的数据弹性搜索 MultiField 注释

Spring data elastic search MultiField annotation on nested object ignored

我创建了一个 asciifolding.json 文件,其中包含“忽略重音”分析器的配置。

我在顶级对象的字段上使用了 @MultiField 注释,这没有问题。

但是,当我在嵌套对象的字段上使用相同的注释时,它会被忽略。

@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
)

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
                        }
                    }
                }
            }
        }
    }
}

知道我遗漏了什么吗?

映射构建器不会考虑未使用 @Field 注释的属性。所以你需要添加这个:

@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
)

顺便说一句,@Setting 注释只在定义索引的实体上检查,所以在顶层。它在嵌套的 class.

上被忽略