Retrieve Object from Solr

前端 未结 3 1592
梦毁少年i
梦毁少年i 2020-12-06 14:03

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

相关标签:
3条回答
  • 2020-12-06 14: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

    • Using SolrJ - Apache's Solr Reference Guide
    • SolrJ - Solr Wiki
    0 讨论(0)
  • 2020-12-06 14:43

    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.

    0 讨论(0)
  • 2020-12-06 14:59
    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);
    
    0 讨论(0)
提交回复
热议问题