【问题标题】:Spring data elastic search MultiField annotation on nested object ignored忽略嵌套对象上的Spring数据弹性搜索MultiField注释
【发布时间】:2021-02-26 15:27:28
【问题描述】:

我创建了一个 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
                        }
                    }
                }
            }
        }
    }
}

知道我缺少什么吗?

【问题讨论】:

    标签: kotlin spring-data-elasticsearch


    【解决方案1】:

    映射构建器不会考虑未使用 @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 注释仅在定义索引的实体上检查,所以在顶层。它在嵌套类中被忽略。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-31
      • 1970-01-01
      • 1970-01-01
      • 2014-05-06
      • 1970-01-01
      • 2015-07-30
      • 2013-11-05
      • 1970-01-01
      相关资源
      最近更新 更多