gpt4 book ai didi

spring - Spring Elastic Search存储库将不存储数据

转载 作者:行者123 更新时间:2023-12-03 01:56:55 25 4
gpt4 key购买 nike

我遇到了一个非常奇怪的问题,我基本上是在尝试使用springboot和存储库在 Elasticsearch 中存储对象,并且由于某些原因,我的对象从未持久化。

这是使用scala完成的

案例类:

 @Document(collection = "SPECTRUM")
@org.springframework.data.elasticsearch.annotations.Document(indexName = "spectrum", `type` = "spectrum", shards = 1, replicas = 0, refreshInterval = "-1")
case class Spectrum(
@(Field@field)(`type` = FieldType.Nested)
biologicalCompound: Compound,
@(Field@field)(`type` = FieldType.Nested)
chemicalCompound: Compound,
@(Field@field)(`type` = FieldType.Nested)
predictedCompound: Compound,
@(Indexed@field)
deleted: Boolean,
@(Id@field)
id: String,
lastUpdated: String,
@(Field@field)(`type` = FieldType.Nested)
metaData: Array[MetaData],
score: Score,
spectrum: String,
splash: Splash,
submitter: Submitter,
@(Field@field)(`type` = FieldType.Nested)
tags: Array[Tags],
@(Field@field)(`type` = FieldType.Nested)
authors: Array[Author]
)

}

存储库
@Repository("spectrumElasticRepository")
trait ISpectrumElasticRepositoryCustom extends ElasticsearchRepository[Spectrum, String] with SpectrumElasticRepositoryCustom{
def findByBiologicalCompoundInchiKey(inchiKey: String) : java.util.List[Spectrum]
}

测试代码
  getRepository.deleteAll()
assert(getRepository.count() == 0)

s"we should be able to store our data" in {
for (spectrum <- exampleRecords) {
val result = getRepository.save(spectrum)
assert(result.isInstanceOf[Spectrum])
}

assert(getRepository.count() == 58)

val data:Iterable[Spectrum] = getRepository.findAll()
}

一旦测试代码达到这一行
            val data:Iterable[Spectrum] = getRepository.findAll()

它导致以下异常
failed to map source [ {}] to class Spectrum
org.springframework.data.elasticsearch.ElasticsearchException: failed to map source [ {}] to class Spectrum

并直接查看 Elasticsearch 服务器
localhost:9200/spectrum/_search

似乎没有任何报告的匹配实际附加任何数据
  {
"_index": "spectrum",
"_type": "spectrum",
"_id": "AVNhcpHjnm4IHnHomcXj",
"_score": 1,
"_source": {}
},

因为源是空的。

任何想法是什么原因造成的?

最佳答案

这似乎与scala有关。

一旦确定了要存储在 Elasticsearch 数据库中的每个属性都将用

@BeanProperty

像这儿

  @Document(collection = "SPECTRUM")
@org.springframework.data.elasticsearch.annotations.Document(indexName = "spectrum", `type` = "spectra", shards = 1, replicas = 0, refreshInterval = "-1")
case class Spectrum(
@BeanProperty
@(Field@field)(`type` = FieldType.Nested)
biologicalCompound: Compound,
@BeanProperty
@(Field@field)(`type` = FieldType.Nested)
chemicalCompound: Compound,
@BeanProperty
@(Field@field)(`type` = FieldType.Nested)
predictedCompound: Compound,
@BeanProperty
@(Indexed@field)
deleted: Boolean,
@BeanProperty
@(Id@field)
id: String,
lastUpdated: String,
@BeanProperty
@(Field@field)(`type` = FieldType.Nested)
metaData: Array[MetaData],
@BeanProperty
score: Score,
@BeanProperty
spectrum: String,
@BeanProperty
splash: Splash,
@BeanProperty
submitter: Submitter,
@BeanProperty
@(Field@field)(`type` = FieldType.Nested)
tags: Array[Tags],
@(Field@field)(`type` = FieldType.Nested)
authors: Array[Author]
)

}

它可以正常工作并保留该对象。

显然,这太丑陋了,它使您想知道是否还有更好的选择,或者编写一个可识别scala的包装器是否有意义。

或者,您可以选择自己的一种覆盖使用的对象映射器和实体impl。这可以在您的配置类中完成。
  @Autowired
val objectMapper:ObjectMapper = null

@Bean
def elasticsearchTemplate: ElasticsearchOperations = {
new ElasticsearchTemplate(client,new EntityMapperImpl(objectMapper))
}

@Bean
def client: Client = {
val client = new TransportClient()
val address = new InetSocketTransportAddress(hostname, port)
client.addTransportAddress(address)

client
}

class EntityMapperImpl(val mapper: ObjectMapper) extends EntityMapper {

override def mapToString(`object`: scala.Any): String = mapper.writeValueAsString(`object`)

override def mapToObject[T](source: String, clazz: Class[T]): T = mapper.readValue(source, clazz)
}

对象映射器的确切配置如下所示
val mapper = new ObjectMapper() with ScalaObjectMapper

mapper.registerModule(DefaultScalaModule)

//required, in case we are provided with a list of value
mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
mapper.setSerializationInclusion(Include.NON_NULL);
mapper

只需要在某处注册为bean

关于spring - Spring Elastic Search存储库将不存储数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35922722/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com