Retrieve Object from Solr

2020-02-01 05:59发布

So, I have a java based web project that displays information retrieved from 3 separate services, hosted on different servers, I use Apache Http Client to retrieve information via REST API in JSON, using Gson library. I convert the Json into POJO's that I use to display information.

Now I want to implement search feature in my project, so I installed Solr on a separate server, what I want is:

  1. Index the JSON in solr server for all 3 services.

  2. fetch search result from Solr in form of POJO's described in my project

I know that point (1) can be done by jsonRequestHandler, but I don't want to write separate logic to index, I am using Solrj in my project to extract information.

So I want to know

  • can solrj use my POJO definition to parse the search results?
  • also any possible workflow for above working scenario, and tools required (I am new to solrj)?

3条回答
孤傲高冷的网名
2楼-- · 2020-02-01 06:29
String id; //input parameter to search with
SolrQuery q = new SolrQuery();
q.set("q","id:" + id);
q.set("fl","*"); // fetch all fields to map to corresponding POJO

QueryResponse queryResponse = solrClient.query("My Collection Name", q);

//because I search by ID, I expect a list of just one element
MyPojo myPojo = queryResponse.getBeans(MyPojo.class).get(0);
查看更多
你好瞎i
3楼-- · 2020-02-01 06:37

Mapping a POJO for Solr

To do so you need to annotate the fields/access-methods of your POJO with the org.apache.solr.client.solrj.beans.Field-Annotation.

Of course those fields need to match the fields of your schema.xml either by their name directly or by the name you point Solr to by giving the name in the Field annotation.

As example you have the following definition of fields in your schema.xml

<fields>
  <field name="id"    type="int"    indexed="true" stored="true" multiValued="false" />
  <field name="title" type="string" indexed="true" stored="true" multiValued="false" />
</fields>

Then you would have a POJO like this

import org.apache.solr.client.solrj.beans.Field;

public class SampleDocument {

    public SampleDocument() {
        // required for solrj to make an instance
    }

    public SampleDocument(int id, String title) {
        this.id = id;
        this.title = title;
    }

    public String getTitle() {
        return title;
    }

    @Field("title")
    public void setTitle(String title) {
        this.title = title;
    }

}

Using a POJO to update Solr's Index

The code to index those POJOs is rather straight forward. You can use solrj's SolrServer for that purpose.

// connect to your solr server
SolrServer server = new HttpSolrServer("http://HOST:8983/solr/");

// adding a single document
SampleDocument document = new SampleDocument(1, "title 1");
server.addBean(document);

// adding multiple documents
List<SampleDocument> documents = Arrays.asList(
        new SampleDocument(2, "title 2"), 
        new SampleDocument(3, "title 3"));
server.addBeans(documents);

// commit changes
server.commit();

// query solr for something
QueryResponse response = server.query(new SolrQuery("*:*"));
// get the response as List of POJO type
List<SampleDocument> foundDocuments = response.getBeans(SampleDocument.class);

Further reading

The results are a write up of our code and the following references

查看更多
甜甜的少女心
4楼-- · 2020-02-01 06:39

Yes that is possible, I was doing some similar thing once. But I think you should write some converter, which would fetch the results from SolrDocumentList and from each SolrDocument put the result to your POJO's.

Read about setting the query parameters and getting the results with solrj, and I think it wouldn't be a problem for you to do this. The converter itself should be easy to write, because you can access every field indexed from the result. Also pay attention to narrowing the results itself.

These are some guides, as I don't feel experienced with solr searching. I don't know if it will help you much, but I hope so.

查看更多
登录 后发表回答