弹性搜索Grails中的解组域类错误

时间:2016-05-31 22:22:19

标签: grails elasticsearch grails-plugin grails-domain-class grails-3.1

我正在尝试在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中注入了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)

有人请告诉我我犯的是什么错误。感谢。

1 个答案:

答案 0 :(得分:0)

有同样的问题,使用弹性支持插件文档中的这篇文章将我的组件更改为修复我的问题的引用:

<强> 3.8.1。可搜索的参考 可搜索引用映射模式是用于关联的默认模式,并且要求关联的可搜索类是根映射的,以便具有其自己的索引。使用此模式,关联的域未在生成的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