gpt4 book ai didi

grails - 在 Elasticsearch Grails 中解码域类错误

转载 作者:行者123 更新时间:2023-12-02 14:10:29 25 4
gpt4 key购买 nike

我正在尝试在 Grails 3.1.6 中使用 ElasticSearch 1.0.0.2。

域类是:

class Post {

String content
Date dateCreated

static belongsTo = [user: User]

static hasMany = [tags: Tag]

static searchable = {
tags component : true
user component: true
}
}

我在我的 SearchController 中注入(inject)了 ElasticSearchService 并尝试获取搜索结果:
try {
def searchResult = elasticSearchService.search("${params.q}")
// def searchResult = Post.search("${params.q}")
println("search result: "+searchResult)
return [searchResult: searchResult]
}catch (e){
println e.message
return [searchError: true]
}

但是得到这样的错误:
 ERROR grails.plugins.elasticsearch.conversion.unmarshall.DomainClassUnmarshaller - Error unmarshalling property 'user' of Class Post with id 4
java.lang.IllegalStateException: Property Post.user is not mapped as [component], but broken search hit found.
at sun.reflect.GeneratedConstructorAccessor116.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at org.springsource.loaded.ri.ReflectiveInterceptor.jlrConstructorNewInstance(ReflectiveInterceptor.java:1075)
at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:83)
at org.codehaus.groovy.reflection.CachedConstructor.doConstructorInvoke(CachedConstructor.java:77)
at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrap.callConstructor(ConstructorSite.java:84)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:247)
at grails.plugins.elasticsearch.conversion.unmarshall.DomainClassUnmarshaller.unmarshallProperty(DomainClassUnmarshaller.groovy:206)
at sun.reflect.GeneratedMethodAccessor339.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1432)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:210)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:59)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:64)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:190)

有人可以告诉我我犯了什么错误。谢谢。

最佳答案

有同样的问题,使用 elastisearch 插件文档中的这篇文章将我的组件更改为解决了我的问题的引用:

3.8.1.可搜索引用
searchable-reference 映射模式是用于关联的默认模式,并且要求关联的可搜索类进行根映射以便拥有自己的索引。使用此模式,关联的域不会完全编码在生成的 JSON 文档中:仅保留实例的 id 和类型。当从索引中检索文档时,插件将使用存储的 id 从索引中自动重建关联。

示例

class MyDomain {
// odom is an association with the OtherDomain class, set as a reference
OtherDomain odom

static searchable = {
odom reference:true
}
}

// The OtherDomain definition, with default searchable
class OtherDomain {
static searchable = true

String field1 = "val1"
String field2 = "val2"
String field3 = "val3"
String field4 = "val4"
}

在索引 MyDomain 实例时,生成的 JSON 文档将被发送到 ElasticSearch:
{
"mydomain": {
"_id":1,
"odom": { "id":1 }
}
}

{
"otherdomain": {
"_id":1,
"field1":"val1",
"field2":"val2",
"field3":"val3",
"field4":"val4"
}
}

这是文档的链接
http://noamt.github.io/elasticsearch-grails-plugin/docs/index.html#searchableComponentReference

关于grails - 在 Elasticsearch Grails 中解码域类错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37556603/

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