2

我有下面的 bean 类来定义我的 solrInputDocument

public class VenueDocumentSolr extends SolrInputDocument {
@Field
private int id;
@Field
    private String uid;
...

}

我尝试使用以下代码获取结果:

SolrQuery query = new SolrQuery();
    query.setQuery(SearchForRestaurants);
    QueryResponse rsp = SolrUtil.issueSolrQuery(query);
    for (SolrDocument s : rsp.getResults())
        System.out.println(s);
    List<VenueDocumentSolr> beans = rsp.getBeans(VenueDocumentSolr.class);

上面的代码有时会起作用,并在其余时间抛出下面的异常。

我交叉检查并将缺少的字段添加到我的 bean 类中。但是没有用。我仍然收到错误:(

org.apache.solr.client.solrj.beans.BindingException: Could not instantiate object of class com.zvents.common.entities.solr.VenueDocumentSolr
    at org.apache.solr.client.solrj.beans.DocumentObjectBinder.getBean(DocumentObjectBinder.java:68)
    at org.apache.solr.client.solrj.beans.DocumentObjectBinder.getBeans(DocumentObjectBinder.java:47)
    at org.apache.solr.client.solrj.response.QueryResponse.getBeans(QueryResponse.java:480)
    at com.zvents.webapp.api.DeleteData.QueryAndUpdateVenuesForSearch(DeleteData.java:117)
    .........

引用 Schema 的一部分

<int name="has_images">*</int>
<arr name="cuisine">
    <str>*</str>
</arr>
<arr name="venue_type">
    <int>*</int>
</arr>
<double name="location_0_latLon">*</double>
<float name="venue_imp">*</float>
<date name="last_indexed">*</date>

引用 bean 类中的适用字段

@Field
private int has_images;
@Field
private List<String> cuisine;
@Field
private List<Integer> venue_type;
@Field
private double location_0_latLon;
@Field
private float venue_imp;
@Field
private String last_indexed;
4

3 回答 3

2

我是通过以下方式做到的:

DocumentObjectBinder binder = new DocumentObjectBinder(); beanlist = binder.getBeans(PortalBean.class, list);

package hello;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.beans.DocumentObjectBinder;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SearchOnNameController {
    @CrossOrigin(origins = "*")
    @RequestMapping("search")
    public List<PortalBean> searchName(@RequestParam(value = "query", defaultValue = "*") String name) {
        String urlString = "http://localhost:8983/solr/kislay";
        SolrClient solr = new HttpSolrClient(urlString);
        SolrQuery query = new SolrQuery();
        // query.addField("name");
        query.setQuery("name:" + "*" + name + "*");
        PortalBean bean = null;
        List<PortalBean> beanlist = new ArrayList<PortalBean>();
        try {
            QueryResponse response = solr.query(query);
            SolrDocumentList list = response.getResults();
            DocumentObjectBinder binder = new DocumentObjectBinder();
            beanlist = binder.getBeans(PortalBean.class, list);
        } catch (SolrServerException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return beanlist;
    }
}

bean 映射为:

package hello;

import org.apache.lucene.document.TextField;
import org.apache.solr.client.solrj.beans.Field;
import org.apache.solr.schema.StrField;

public class PortalBean {
    private String id;
    private String name;
    private String image;
    private String description;
    private String branding;
    private double rating;
    private double setup_fee;
    private String transaction_fees;
    private String how_to_url;
    private String [] currencies;

    public String getId() {
        return id;
    }

    @Field("id")
    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    @Field("name")
    public void setName(String name) {
        this.name = name;
    }

    public String getImage() {
        return image;
    }

    @Field("image")
    public void setImage(String image) {
        this.image = image;
    }

    public String getDescription() {
        return description;
    }

    @Field("description")
    public void setDescription(String description) {
        this.description = description;
    }

    public String getBranding() {
        return branding;
    }

    @Field("branding")
    public void setBranding(String branding) {
        this.branding = branding;
    }

    public double getRating() {
        return rating;
    }

    @Field("rating")
    public void setRating(double rating) {
        this.rating = rating;
    }

    public double getSetup_fee() {
        return setup_fee;
    }

    @Field("setup_fee")
    public void setSetup_fee(double setup_fee) {
        this.setup_fee = setup_fee;
    }

    public String getTransaction_fees() {
        return transaction_fees;
    }

    @Field("transaction_fees")
    public void setTransaction_fees(String transaction_fees) {
        this.transaction_fees = transaction_fees;
    }

    public String getHow_to_url() {
        return how_to_url;
    }

    @Field("how_to_url")
    public void setHow_to_url(String how_to_url) {
        this.how_to_url = how_to_url;
    }

    public String [] getCurrencies() {
        return currencies;
    }

    @Field("currencies")
    public void setCurrencies(String [] currencies) {
        this.currencies = currencies;
    }

}

另请注意,托管模式文件需要正确定义字段:
根据我的用例示例:

  <field name="branding" type="string"/>
  <field name="currencies" type="strings"/>
  <field name="description" type="string"/>
  <field name="how_to_url" type="string"/>
  <field name="id" type="string" multiValued="false" indexed="true" required="true" stored="true"/>
  <field name="image" type="string"/>
  <field name="name" type="string"/>
  <field name="rating" type="tdouble"/>
  <field name="setup_fee" type="tdouble"/>
  <field name="transaction_fee" type="string"/>
于 2016-03-06T06:21:15.217 回答
1

我的错误是:BindingException: Could not instantiate object

我向 POJO 添加了一个无变量构造函数,它开始工作。

public class MyItem {
    String id;
    @Field("id")
    public void setId(String id) {
        this.id = id;
    }
    public String getId() {
        return id;
    }   
    public MyItem() {
    }  
}

要到达这里,我的架构中的唯一字段是:

<field name="_version_" type="long" indexed="true" stored="true"/>
   <field name="_root_" type="string" indexed="true" stored="false"/>
   <field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" /> 
   <field name="text" type="string" indexed="true" stored="false" multiValued="true"/> 
   <uniqueKey>id</uniqueKey>

这是在 solr 中转储 id 的代码。

 private void dumpSolr2() throws SolrServerException {
        System.out.println("dumpSolr2");
        SolrQuery query = new SolrQuery();        
        query.setQuery( "*:*" ); // get all
        query.setFields("id");  // get the id field
        query.setStart(0);   // row to start on.  First row is 0.
        query.setRows(10);   // number of rows to get at a time.      

        System.out.println(query.toString());                

        QueryResponse rsp = _solr.query( query );
        List<BulkLoad1Data> bulkLoad1DataList = rsp.getBeans(BulkLoad1Data.class);

        RplHelper1.rplSay("bulkLoad1DataList.size():  " + bulkLoad1DataList.size());

        int count = 0;
        Iterator<BulkLoad1Data> i = bulkLoad1DataList.iterator();
        while (i.hasNext()) { 

            BulkLoad1Data bld = (BulkLoad1Data) i.next();
            RplHelper1.rplSay("dumpSolr2 - count:  " +  ++count + "   " +                               
                              " id: " +  bld.getId());
       }         
    }   
于 2014-12-07T17:59:10.480 回答
1

不幸的是,您没有发布所有的 pojo 和所有的模式,所以我只能猜测。

从您发布的代码中,我唯一看到的是您正试图让 solr 为您进行某种转换。

那是为了

@Field
private String last_indexed;

<date name="last_indexed">*</date>

Solr 不会为您将日期转换为字符串,只是不会。但是如果没有完整的架构和您的 Bean 的完整代码,我无法给您完整的答案。

我从您发布的代码中注意到的其他内容

  • 据我所知,您为什么要放入*架构的元素中,这没有任何效果
  • 你为什么要扩展 SolrInputDocument?你不需要。@Field旨在使您不需要子类化任何东西,但可以立即使用模型的 POJO。
于 2013-10-14T22:57:01.617 回答