1. Obtain solrserver 1.1 commonshttpsolrserver
Commonshttpsolrserver uses httpclient to communicate with the SOLR server.
Java code
- String url = "http: // localhost: 8983/SOLR ";
- 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
- Server. setparser (New xmlresponseparser ());
Changing other connection settings
Commonshttpsorlrserver allows you to set Link Attributes.
Java code
- String url = "http: // localhost: 8983/SOLR"
- Commonshttpsolrserver Server = new commonshttpsolrserver (URL );
- Server. setsotimeout (1000); // socket read timeout
- Server. setconnectiontimeout (100 );
- Server. setdefamaxmaxconnectionsperhost (100 );
- Server. setmaxtotalconnections (100 );
- Server. setfollowredirects (false); // defaults to false
- // Allowcompression ults to false.
- // Server side must support gzip or deflate for this to have any effect.
- Server. setallowcompression (true );
- 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
- Solrcore core = solrcore. getsolrcore ();
- Solrserver Server = new embeddedsolrserver (CORE );
- ...
If you want to use the multicore feature, you can use:
Java code
- File home = new file (getsolrhome ());
- File F = new file (home, "SOLR. xml ");
- Multicore. Load (getsolrhome (), F );
- 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
- Solrserver Server = getsolrserver ();
- If you are using a remote solrserver, you may implement the getsolrserver () method as follows:
Java code
- Public solrserver getsolrserver (){
- // The instance can be reused
- Return new commonshttpsolrserver ();
- }
- If you are using a local solrserver, you may implement the getsolrserver () method as follows:
Java code
- Public solrserver getsolrserver (){
- // The instance can be reused
- Return new embeddedsolrserver ();
- }
- If you want to clear the existing index before adding data, you can do this:
Java code
- Server. deletebyquery ("*: *"); // delete everything!
Java code solrinputdocument doc1 = new solrinputdocument ();
- Doc1.addfield ("ID", "id1", 1.0f );
- Doc1.addfield ("name", "doc1", 1.0f );
- 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
- Solrinputdocument doc2 = new solrinputdocument ();
- Doc2.addfield ("ID", "Id2", 1.0f );
- Doc2.addfield ("name", "doc2", 1.0f );
- Doc2.addfield ("price", 20 );
- Construct a collection of documentsJava code
- Collection <solrinputdocument> docs = new arraylist <solrinputdocument> ();
- Docs. Add (doc1 );
- Docs. Add (doc2 );
Java code
- Server. Add (DOCS );
Java code
- Server. Commit ();
- After adding documents, create a commit immediately. You can write your program as follows:
Java code
- Updaterequest Req = new updaterequest ();
- Req. setaction (updaterequest. Action. Commit, false, false );
- Req. Add (DOCS );
- 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
- Import org. Apache. SOLR. Client. solrj. Beans. field;
- Public class item {
- @ Field
- String ID;
- @ Field ("CAT ")
- String [] categories;
- @ Field
- List <string> features;
- }
- Java annotations can also be used in the setter method, as shown in the following example:
Java code
- @ Field ("CAT ")
- Public void setcategory (string [] C ){
- This. Categories = C;
- }
Here there should be a relative get method (without Java comments) to read the attribute.
- Get an instance of server
Java code
- Solrserver Server = getsolrserver ();
Java code
- Item = new item ();
- Item. ID = "one ";
- Item. Categories = new string [] {"AAA", "BBB", "CCC "};
Java code
- Server. addbean (item );
- Submit multiple beans to SOLR
Java code
- List <item> beans;
- // Add item objects to the list
- Server. addbeans (beans );
Note: You can reuse solrserver to improve performance.
4. Reading data from SOLR
- Obtain solrserver instances
Java code
- Solrserver Server = getsolrserver ();
Java code
- Solrquery query = new solrquery ();
- Query. setquery ("*: *");
- Query. addsortfield ("price", solrquery. Order. ASC );
- Send a query request to the server
Java code
- Queryresponse RSp = server. Query (query );
Java code
- 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
- 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
- Solrserver Server = getsolrserver ();
- Solrquery = new solrquery ().
- Setquery ("iPod ").
- Setfacet (true ).
- Setfacetmincount (1 ).
- Setfacetlimit (8 ).
- Addfacetfield ("category ").
- Addfacetfield ("instock ");
- 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"); } }