In the previous chapter, how to use SOLR admin to add an index to the SOLR server, SOLR is a standalone enterprise Search application server that provides an API interface similar to Web-service. The user can submit an XML file of a certain format to the search engine server via an HTTP request, generate an index, or make a lookup request through an HTTP GET operation and get the returned result in XML format. So we can use HttpClient as a client to add an index to the SOLR server, but fortunately, SOLR provides us with a dedicated client jar package, SOLRJ, which we can use to submit an index to the SOLR server.
First we import the relevant packages, and the related jar packages can be obtained by downloading the SOLR package, which includes:
1.dist/solrj-lib
2.dist/solr-solrj-*.jar
By importing these jar packages into classpath, we can start using SOLRJ.
Httpsolrserver Httpsolrserver is the class used to connect to the SOLR server, its underlying implementation is dependent on Apache HttpClient, the following is the specific code to connect to the server:
String url = "HTTP://LOCALHOST:8080/SOLR";
Httpsolrserver Server = new Httpsolrserver (URL); Server.setmaxretries (1); Defaults to 0.
> 1 not recommended. Server.setconnectiontimeout (5000); 5 Seconds to establish TCP//Setting The XML response parser are only required for cross//version compatibility a
nd if one side is 1.4.1 or//earlier and the other side is 3.1 or later. Server.setparser (New Xmlresponseparser ());
Binary parser is used by//default//The following settings be provided here for completeness. They would not normally is required, and should only being used//after consulting Javadocs to know whether they is Tru
ly required. Server.setsotimeout (1000);
Socket read Timeout server.setdefaultmaxconnectionsperhost (100);
Server.setmaxtotalconnections (100); Server.setfollowredirects (FALSE);
Defaults to False//allowcompression defaults to False. Server side must support gzip or deflate for this to have any effect. Server.setallowcompression (TRUE);
EmbeddedsolrserverIn addition, SOLRJ provides another way to connect SOLR directly by accessing the files locally, without using the HTTP connection to SOLR.
Note that the following property could is set through JVM level arguments too
system.setproperty ("Solr.solr.home", "/HOME/SHALINSMANGAR/WORK/OSS/BRANCH-1.3/EXAMPLE/SOLR");
Corecontainer.initializer Initializer = new Corecontainer.initializer ();
Corecontainer Corecontainer = Initializer.initialize ();
Embeddedsolrserver Server = new Embeddedsolrserver (Corecontainer, "");
If more than one core is used, you can do so in the following way.
File Home = new file ("/path/to/solr/home");
File F = new file (home, "Solr.xml");
Corecontainer container = new Corecontainer ();
Container.load ("/path/to/solr/home", f);
Embeddedsolrserver Server = new Embeddedsolrserver (container, "core name as defined in Solr.xml");
adding data to the SOLR serverOnce the connection is successful, we can use the server to submit the data.
Solrinputdocument Doc1 = new Solrinputdocument ();
Doc1.addfield ("id", "333333333333333", 1.0f);
Doc1.addfield ("name", "3333333333333333333", 1.0f);
Doc1.addfield ("Price", ten);
Server.add (Doc1);
Server.commit ();
After successful submission, you can see if the index is increased by using SOLR admin, but the simplest way is to call the Query method and query.
Solrquery solrquery = new Solrquery ();
Solrquery.setquery ("q=3333333333333333333");
Queryresponse queryresponse= server.query (solrquery);
System.out.println (Queryresponse.getresults (). Get (0). GetFieldValue ("name"));
Add Pojo data to the SOLR service
First we need to create a new Java bean.
public class Item {
@Field
private String ID;
@Field
private String category;
@Field
private list<string> features;
Public String getId () {
return ID;
}
public void SetId (String id) {
this.id = ID;
}
Public String getcategory () {
return category;
}
public void Setcategory (String category) {
this.category = category;
}
Public list<string> Getfeatures () {
return features;
}
public void Setfeatures (List<string> features) {
this.features = features;
}
}
Add data to SOLR.
Item item = new Item ();
Item.setid ("201404300001");
Item.setcategory ("category");
Item.setfeatures (Arrays.aslist ("Feature1", "Feature2", "feature3"));
Server.addbean (item);
Server.commit ();
Search for it and verify that it is added successfully.
Solrquery solrquery = new Solrquery ();
Solrquery.setquery ("Q=feature1");
Queryresponse queryresponse= server.query (solrquery);
list<item> items = Queryresponse.getbeans (item.class);
System.out.println (Items.get (0). GetCategory ());