我有这个文档的索引:
{
"brand": "Renault",
"brand_suggest": [
{
"input": "Renault",
"weight": 10
},
]
}
在另一边,我试图从我的后台映射中检索此对象:
@JsonInclude(JsonInclude.Include.NON_NULL)
@Document(indexName = "brands", createIndex = false)
data class ESBrands(
@Id
@JsonProperty("id")
val id: String? = null,
@JsonProperty("brand_suggest")
@Field(name = "brand_suggest")
@CompletionField(preservePositionIncrements = false, preserveSeparators = false])
val brandSuggest: List<String>? = null,
)
但它不起作用,因为我尝试放置嵌套对象:
{
"input": "Renault",
"weight": 10
}
进入字符串列表。但我需要它是一个字符串列表。 有没有办法拥有这种语法:
@Field(name = "brand_suggest.input.*")
这也不起作用
谢谢
最终添加转换器解决了问题:
@JsonInclude(JsonInclude.Include.NON_NULL)
@Document(indexName = "brands", createIndex = false)
data class ESBrands(
@Id
@JsonProperty("id")
val id: String? = null,
@JsonProperty("brand_suggest")
@Field(name = "brand_suggest")
@CompletionField(preservePositionIncrements = false, preserveSeparators = false])
@ValueConverter(SuggestConverter::class)
val brandSuggest: List<String>? = null,
)
然后是转换器:
import org.springframework.data.elasticsearch.core.mapping.PropertyValueConverter
class SuggestConverter : PropertyValueConverter {
override fun write(value: Any): Any {
return value
}
override fun read(value: Any): String {
val valueString: java.util.HashMap<*, *> = value as HashMap<*, *>
return valueString.get("input").toString()
}
}