Hibernate Search查询中的fullTextQuery.getResultList()上的com.google.gson.JsonArray.getAsString错误

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate Search查询中的fullTextQuery.getResultList()上的com.google.gson.JsonArray.getAsString错误相关的知识,希望对你有一定的参考价值。

我正在尝试使用hibernate搜索全文查询从idex中获取数据。以下是索引结构:

 {
    "_index" : "basclt1400",
    "_type" : "com.csc.pt.svc.data.to.Basclt1400TO",
    "_id" : "00,0006682,CPP,05,00",
    "_score" : 1.0,
    "_source" : {
      "id" : "00,0006682,CPP,05,00",
      "location" : "00",
      "master0co" : "05",
      "policy0num" : "0006682",
      "symbol" : "CPP",
      "module" : "00",
      "cltseqnum" : 281,
      "addrseqnum" : "1",
      "policies_location" : [
        "00",
        "00"
      ],
      "policies_master0co" : [
        "05",
        "05"
      ],
      "policies_policy0num" : [
        "0006682",
        "0006682"
      ],
      "policies_trans0stat" : [
        "V",
        "P"
      ],
      "policies_id02" : [
        "02",
        "02"
      ],
      "policies_symbol" : [
        "CPP",
        "CPP"
      ],
      "policies_module" : [
        "00",
        "00"
      ],
      "policies_tot0ag0prm" : [
        "1532.00",
        "1532.00"
      ],
      "policies_issue0code" : [
        "N",
        "N"
      ],
      "policies_id" : [
        "02,00,0006682,CPP,05,00,V",
        "02,00,0006682,CPP,05,00,P"
      ]
    }

这个结构可能会根据索引下的数据进行更改,在某些地方,“policies_policy0num”字段下的数据可能只有一条记录,如下所示,并且它可以正常使用此结构:

"_index" : "basclt1400",
    "_type" : "com.csc.pt.svc.data.to.Basclt1400TO",
    "_id" : "00,0012410,CPP,05,00",
    "_score" : 1.0,
    "_source" : {
      "id" : "00,0012410,CPP,05,00",
      "location" : "00",
      "master0co" : "05",
      "policy0num" : "0012410",
      "symbol" : "CPP",
      "module" : "00",
      "cltseqnum" : 281,
      "addrseqnum" : "1",
      "policies_location" : [
        "00"
      ],
      "policies_master0co" : [
        "05"
      ],
      "policies_policy0num" : [
        "0012410"
      ],
      "policies_trans0stat" : [
        "P"
      ],
      "policies_id02" : [
        "02"
      ],
      "policies_symbol" : [
        "CPP"
      ],
      "policies_module" : [
        "00"
      ],
      "policies_tot0ag0prm" : [
        "0.00"
      ],
      "policies_issue0code" : [
        "N"
      ],
      "policies_id" : [
        "02,00,0012410,CPP,05,00,P"
      ]
    }
  }

我试着像下面这样取这个:

Iterator itr = fullTextQuery.getResultList().iterator();
        List<MasterSearchPmsp0200DataArr> policyArrayFinal = new ArrayList<MasterSearchPmsp0200DataArr>();
        List<MasterSearchPmsp0200DataArr> quoteArrayFinal = new ArrayList<MasterSearchPmsp0200DataArr>();
        while(itr.hasNext()){
           Object[] obj = (Object[]) itr.next();
           char issueCode = (char) obj[5];
           if(issueCode == 'N' || issueCode == 'R') {
               policyArrayFinal.add( new MasterSearchPmsp0200DataArr((String) obj[0], Long.valueOf(to.getCltseqnum()), 
                        (String) obj[1], (String) obj[2], (String) obj[3], (String) obj[4],
                        (char) obj[5], (char) obj[6]));
           }else {
               quoteArrayFinal.add( new MasterSearchPmsp0200DataArr((String) obj[0], Long.valueOf(to.getCltseqnum()), 
                        (String) obj[1], (String) obj[2], (String) obj[3], (String) obj[4],
                        (char) obj[5], (char) obj[6])); 
           }

           }

并且它抛出了以下错误,仅针对我们在policies_policy0num下有多个数据的记录。

java.lang.IllegalStateException
at com.google.gson.JsonArray.getAsString(JsonArray.java:226)
at org.hibernate.search.elasticsearch.query.impl.PrimitiveProjection.addDocumentField(PrimitiveProjection.java:69)
at org.hibernate.search.elasticsearch.query.impl.PrimitiveProjection.addDocumentField(PrimitiveProjection.java:43)
at org.hibernate.search.elasticsearch.query.impl.TwoWayFieldBridgeProjection.convertFieldValue(TwoWayFieldBridgeProjection.java:60)
at org.hibernate.search.elasticsearch.query.impl.TwoWayFieldBridgeProjection.convertHit(TwoWayFieldBridgeProjection.java:43)
at org.hibernate.search.elasticsearch.query.impl.QueryHitConverter.convert(QueryHitConverter.java:186)
at org.hibernate.search.elasticsearch.query.impl.IndexSearcher.convertQueryHit(IndexSearcher.java:138)
at org.hibernate.search.elasticsearch.query.impl.ElasticsearchHSQueryImpl.queryEntityInfos(ElasticsearchHSQueryImpl.java:233)
at org.hibernate.search.query.hibernate.impl.FullTextQueryImpl.doHibernateSearchList(FullTextQueryImpl.java:238)
at org.hibernate.search.query.hibernate.impl.FullTextQueryImpl.list(FullTextQueryImpl.java:223)
at org.hibernate.search.query.hibernate.impl.FullTextQueryImpl.getResultList(FullTextQueryImpl.java:122)

附加错误点snaspshot:error snapshot我应该如何在hibernate搜索java代码下处理这个场景。

添加查询代码:

Query query = queryBuilder.keyword().onField("cltseqnum").matching(to.getCltseqnum()).createQuery();

        FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(query, Basclt1400TO.class);
        fullTextQuery.setProjection( "policies_policy0num", "policies_symbol", 
                "policies_module", "policies_master0co","policies_location", "policies_issue0code",
                "policies_trans0stat");

以上是关于Hibernate Search查询中的fullTextQuery.getResultList()上的com.google.gson.JsonArray.getAsString错误的主要内容,如果未能解决你的问题,请参考以下文章

[Hibernate Search] 基础查询

使用 Hibernate Search 查询并获取所有项目而无需分页

如何使用带有通配符查询的 Hibernate Search 并输出结果对象列表

Hibernate Search:如何正确使用通配符?

Hibernate Search 不索引/重新索引实体

结合hibernate全文搜索和JPA查询