0

我无法从 QueryResponse 读取时间戳数据(想要将结果转换为 POJO)。如果我使用 SolrDocumentList 没有错误。

db-data-config.xml:

<entity name="discussion" dataSource="mssqlDatasource" pk="id"
        transformer="TemplateTransformer, DateFormatTransformer"
        query="SELECT d.id, d.title, d.created,
               u.first_name, u.last_name,
               db.type AS boardType, db.country
               FROM discussion d
               INNER JOIN wtb_user u ON d.author = u.id
               INNER JOIN discussion_category dc ON d.category = dc.id
               INNER JOIN discussion_board db ON dc.board = db.id">                   
        <!--<field column="created" dateTimeFormat='yyyyMMdd HH:mm:ss z' />-->
        <!--<field column="created" dateTimeFormat='yyyyMMdd' />-->
        <field column="first_name" name="firstName" />
        <field column="last_name" name="lastName" />
        <field column="tableType" template="DISCUSSION" />

        <entity name="discussion_post" dataSource="mssqlDatasource"
            transformer="ClobTransformer"
            query="SELECT dp.message
                   FROM discussion_post dp
                   WHERE dp.discussion = '${discussion.id}'">
            <field column="message" name="discussionPostMessage" clob="true" />
        </entity>
    </entity>

架构.xml:

<field name="created" type="date" indexed="false" stored="true"/> 

“创建”字段是我数据库中的时间戳,插入索引数据后,结果如下所示(使用浏览器管理控制台调用):

  <result name="response" numFound="246" start="0">
  <doc>
   <str name="boardType">1</str>
   <date name="created">2012-10-05T07:29:23.387Z</date>
   <arr name="discussionPostMessage">
    <str>message test</str>
    <str>second</str>
    <str>third</str>
   </arr>
   <str name="firstName">Ashley</str>
   <str name="id">10</str>
   <str name="lastName">Morgan</str>
   <str name="tableType">DISCUSSION</str>
   <str name="title">headline test</str>
  </doc>
  ...

现在我试图查询“所有结果”

public void search(String searchString) {
    SolrQuery query = new SolrQuery();
    QueryResponse rsp;
    try {
        query = new SolrQuery();
        query.setQuery(DEFAULT_QUERY);
        query.setRows(246);

        rsp = getServer().query(query);

        SolrDocumentList solrDocumentList = rsp.getResults(); // IllegalArgumentException
        List<SearchRequestResponseObject> beans = rsp.getBeans(SearchRequestResponseObject.class); //works
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

SearchRequestResponseObject.class:

public class SearchRequestResponseObject {
@Field
private String id;

@Field
private String title;

@Field
@Temporal(TemporalType.TIMESTAMP)
    //@DateTimeFormat(style = "yyyyMMdd HH:mm:ss z")
//@DateTimeFormat(style = "yyyyMMdd")
private Timestamp created;
    ...
}

例外:

Caused by: java.lang.IllegalArgumentException: Can not set java.sql.Timestamp field com.solr.SearchRequestResponseObject.created to java.util.Date
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:146)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:150)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:63)
at java.lang.reflect.Field.set(Field.java:657)
at org.apache.solr.client.solrj.beans.DocumentObjectBinder$DocField.set(DocumentObjectBinder.java:374)
... 45 more

详细留言:

Exception while setting value : Fri Oct 05 09:29:23 CEST 2012 on private java.sql.Timestamp com.solr.SearchRequestResponseObject.created

我做错了什么?为什么要“创建”一个日期?

4

1 回答 1

2

Your 'created' field should be a date because even though you are passing in a TimeStamp from your database, it is being stored internally as a Date in Solr and is therefore being exposed as a Date from Solr. SolrJ does not understand how to handle a TimeStamp. If you really want to have TimeStamp property on your POJO, you will need to get the field from Solr as a Date and then have a separate TimeStamp property on your POJO that converts the Date value.

于 2012-10-15T15:59:53.613 回答