Use solrj to add, delete, and query SOLR.
Reference:
Http://wiki.apache.org/solr/Solrj
Create a new project in Eclipse: testsolr
The LIB package of sorlj includes:
Code:
Create an item entity
package com.my.entity;import java.util.Date;import org.apache.solr.client.solrj.beans.Field;public class Item { @Field private long id; @Field private String subject; @Field private String content; @Field private Date last_update_time; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public Date getLast_update_time() { return last_update_time; } public void setLast_update_time(Date last_update_time) { this.last_update_time = last_update_time; }}
Test code:
Package COM. my. SOLR; import Java. io. ioexception; import Java. util. date; import Java. util. list; import Org. apache. SOLR. client. solrj. solrquery; import Org. apache. SOLR. client. solrj. solrquery. order; import Org. apache. SOLR. client. solrj. solrquery. sortclause; import Org. apache. SOLR. client. solrj. solrserverexception; import Org. apache. SOLR. client. solrj. impl. httpsolrserver; import Org. apache. SOLR. client. solrj. impl. XM Lresponseparser; import Org. apache. SOLR. client. solrj. response. queryresponse; import COM. my. entity. item; public class testsolr {public static void main (string [] ARGs) throws ioexception, solrserverexception {string url = "http: // localhost: 8899/SOLR/mycore "; httpsolrserver core = new httpsolrserver (URL); core. setmaxretries (1); core. setconnectiontimeout (5000); core. setparser (New xmlresponseparser ()); // Binary parser is used by default core. setsotimeout (1000); // socket read timeout core. setdefamaxmaxconnectionsperhost (100); core. setmaxtotalconnections (100); core. setfollowredirects (false); // defaults to false core. setallowcompression (true); // -------------------------------------------------------- // remove all data // extract core. deletebyquery ("*: *"); // -------------------------------------------------------- // Add item = new item (); item. setid (1); item. setsubject ("solrj test"); item. setcontent ("this is my solrj test, ha ha. "); item. setlast_update_time (new date (); core. addbean (item); // -------------------------------------------------------- // Add Unicode item //----- Using item item_cn = new item (); item_cn.setid (2); item_cn.setsubject ("My tests"); item_cn.setcontent ("This is a magical website! "); Item. setlast_update_time (new date (); core. addbean (item_cn); // commit core. commit (); // queries // search // ---------------------------------------------------------- solrquery query = new solrquery (); query. setquery ("*: *"); query. addsort (New sortclause ("ID", order. DESC); queryresponse response = core. query (query); List <item> items = response. getbeans (item. class); For (item I: Items) {system. out. println ("ID =" + I. GETID () + "\ tcontent =" + I. getcontent ());}}}
Running result:
[SOLR]-solrj: add, delete, and query