SOLR Study Notes

Source: Internet
Author: User
Tags solr

 

1. Obtain solrserver 1.1 commonshttpsolrserver

Commonshttpsolrserver uses httpclient to communicate with the SOLR server.

Java code
  1. String url = "http: // localhost: 8983/SOLR ";
  2. Solrserver Server = new commonshttpsolrserver (URL );
Setting xmlresponseparser

Sorlr J currently uses the binary format as the default format. Solr1.2 users can use the XML format only through the display settings.

Java code
  1. Server. setparser (New xmlresponseparser ());
Changing other connection settings

Commonshttpsorlrserver allows you to set Link Attributes.

Java code
  1. String url = "http: // localhost: 8983/SOLR"
  2. Commonshttpsolrserver Server = new commonshttpsolrserver (URL );
  3. Server. setsotimeout (1000); // socket read timeout
  4. Server. setconnectiontimeout (100 );
  5. Server. setdefamaxmaxconnectionsperhost (100 );
  6. Server. setmaxtotalconnections (100 );
  7. Server. setfollowredirects (false); // defaults to false
  8. // Allowcompression ults to false.
  9. // Server side must support gzip or deflate for this to have any effect.
  10. Server. setallowcompression (true );
  11. Server. setmaxretries (1); // defaults to 0.> 1 not recommended.
1.2 embeddedsolrserver

Embeddedsorrserver provides the same interface as commonshttpsorlrserver, and does not require HTTP connections.

Java code
  1. Solrcore core = solrcore. getsolrcore ();
  2. Solrserver Server = new embeddedsolrserver (CORE );
  3. ...

If you want to use the multicore feature, you can use:

Java code
  1. File home = new file (getsolrhome ());
  2. File F = new file (home, "SOLR. xml ");
  3. Multicore. Load (getsolrhome (), F );
  4. Embeddedsolrserver Server = new embeddedsolrserver (multicore, "core name as defined in SOLR. xml ");

If you embed the SOLR service in your project, this will be a good choice. Whether you can use HTTP or not, it provides the same interface.

Usage

Solrj is designed as an extensible framework to submit requests to the SOLR server and receive responses.

We have encapsulated some of the most common commands in the solrserver class.

2. Adding data to SOLR
  • First, you need to obtain a server instance,
Java code
  1. Solrserver Server = getsolrserver ();
  • If you are using a remote solrserver, you may implement the getsolrserver () method as follows:
Java code
  1. Public solrserver getsolrserver (){
  2. // The instance can be reused
  3. Return new commonshttpsolrserver ();
  4. }
  • If you are using a local solrserver, you may implement the getsolrserver () method as follows:
Java code
  1. Public solrserver getsolrserver (){
  2. // The instance can be reused
  3. Return new embeddedsolrserver ();
  4. }
  • If you want to clear the existing index before adding data, you can do this:
Java code
  1. Server. deletebyquery ("*: *"); // delete everything!
  • Construct a document
Java code solrinputdocument doc1 = new solrinputdocument ();
  1. Doc1.addfield ("ID", "id1", 1.0f );
  2. Doc1.addfield ("name", "doc1", 1.0f );
  3. Doc1.addfield ("price", 10 );
  • Construct another document. Each document can be submitted to SOLR independently. However, batch submission is more efficient. Each solrserver request is an HTTP request. Of course, it is different for embeddedsolrserver.

    Java code
  1. Solrinputdocument doc2 = new solrinputdocument ();
  2. Doc2.addfield ("ID", "Id2", 1.0f );
  3. Doc2.addfield ("name", "doc2", 1.0f );
  4. Doc2.addfield ("price", 20 );
  • Construct a collection of documents

    Java code
  1. Collection <solrinputdocument> docs = new arraylist <solrinputdocument> ();
  2. Docs. Add (doc1 );
  3. Docs. Add (doc2 );
  • Submit documents to SOLR
Java code
  1. Server. Add (DOCS );
  • Submit a commit
Java code
  1. Server. Commit ();
  • After adding documents, create a commit immediately. You can write your program as follows:
Java code
  1. Updaterequest Req = new updaterequest ();
  2. Req. setaction (updaterequest. Action. Commit, false, false );
  3. Req. Add (DOCS );
  4. Updateresponse RSp = Req. Process (server );
3. directly adding pojos to SOLR
  • Use Java annotations to create a Java Bean. @ Field, which can be used in the domain or the setter method. If the name of a domain is different from the bean name, enter an alias in the Java comment. For details, refer to the following domain categories.
Java code
  1. Import org. Apache. SOLR. Client. solrj. Beans. field;
  2. Public class item {
  3. @ Field
  4. String ID;
  5. @ Field ("CAT ")
  6. String [] categories;
  7. @ Field
  8. List <string> features;
  9. }
  • Java annotations can also be used in the setter method, as shown in the following example:
Java code
  1. @ Field ("CAT ")
  2. Public void setcategory (string [] C ){
  3. This. Categories = C;
  4. }

Here there should be a relative get method (without Java comments) to read the attribute.

  • Get an instance of server
Java code
  1. Solrserver Server = getsolrserver ();
  • Create a bean instance
Java code
  1. Item = new item ();
  2. Item. ID = "one ";
  3. Item. Categories = new string [] {"AAA", "BBB", "CCC "};
  • Add to SOLR
Java code
  1. Server. addbean (item );
  • Submit multiple beans to SOLR
Java code
  1. List <item> beans;
  2. // Add item objects to the list
  3. Server. addbeans (beans );

Note: You can reuse solrserver to improve performance.

4. Reading data from SOLR
  • Obtain solrserver instances
Java code
  1. Solrserver Server = getsolrserver ();
  • Construct solrquery
Java code
  1. Solrquery query = new solrquery ();
  2. Query. setquery ("*: *");
  3. Query. addsortfield ("price", solrquery. Order. ASC );
  • Send a query request to the server
Java code
  1. Queryresponse RSp = server. Query (query );
  • Obtain the result.
Java code
  1. Solrdocumentlist docs = RSP. getresults ();
  • To obtain results in the form of JavaBean, the JavaBean must have Java annotations as in the previous example.

    Java code
    1. List <item> beans = RSP. getbeans (item. Class );
5. Advanced usage

Solrj provides a set of APIs to help us create a query. The following is an example of a faceted query.

Java code
  1. Solrserver Server = getsolrserver ();
  2. Solrquery = new solrquery ().
  3. Setquery ("iPod ").
  4. Setfacet (true ).
  5. Setfacetmincount (1 ).
  6. Setfacetlimit (8 ).
  7. Addfacetfield ("category ").
  8. Addfacetfield ("instock ");
  9. Queryresponse RSp = server. Query (solrquery );

All setter/Add Methods return their own instances, so as you can see, the above usage is chained.

 

 

Highlighting

 

Highlighting parameters are set like other common parameters.

 

    SolrQuery query = new SolrQuery();    query.setQuery("foo");    query.setHighlight(true).setHighlightSnippets(1); //set other params as needed    query.setParam("hl.fl", "content");    QueryResponse queryResponse = getSolrServer().query(query);Then to get back the highlight results you need something like this:    Iterator<SolrDocument> iter = queryResponse.getResults().iterator();    while (iter.hasNext()) {      SolrDocument resultDoc = iter.next();      String content = (String) resultDoc.getFieldValue("content");      String id = (String) resultDoc.getFieldValue("id"); //id is the uniqueKey field      if (queryResponse.getHighlighting().get(id) != null) {        List<String> highlightSnippets = queryResponse.getHighlighting().get(id).get("content");      }    }

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.