Example code for using solr in java
- 2020-08-22 21:58:31
- OfStack
SolrJ is the Java client that operates on Solr. It provides the JAVA interface for adding, modifying, deleting, and querying the Solr index. SolrJ encapsulates Rest's HTTP interface for Solr, and the bottom layer of SolrJ USES the methods in httpClient to complete the operation of Solr.
References to jar packages (maven ES17en.xml) :
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>5.3.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.3</version>
</dependency>
java code:
package entity;
import java.io.IOException;
import java.util.List;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
public class SolrJTest {
// The specified solr Server address
private final static String SOLR_URL = "http://localhost:8080/solr/";
/**
* create SolrServer object
*
* This object can be used in two ways, both of which are thread safe
* 1 , CommonsHttpSolrServer : starting web Server used by, via http The request of
* 2 , EmbeddedSolrServer : Inline, import solr the jar The package is ready to use
* 3 , solr 4.0 And then I kind of added a bunch of things, one of them CommonsHttpSolrServer This class is renamed to HttpSolrClient
*
* @return
*/
public HttpSolrClient createSolrServer(){
HttpSolrClient solr = null;
solr = new HttpSolrClient(SOLR_URL);
return solr;
}
/**
* Add a document to the index library
* @throws IOException
* @throws SolrServerException
*/
public void addDoc() throws SolrServerException, IOException{
// structure 1 document
SolrInputDocument document = new SolrInputDocument();
// to doc Add field in , Fields added on the client side must be defined on the server side
document.addField("id", "8");
document.addField("name", " Zhou Xinxing ");
document.addField("description", "1 A brilliant military strategist ");
// To obtain 1 a solr Server side request, to submit , Choose something specific 1 a solr core
HttpSolrClient solr = new HttpSolrClient(SOLR_URL + "my_core");
solr.add(document);
solr.commit();
solr.close();
}
/**
* According to the id Delete a document from the index library
*/
public void deleteDocumentById() throws Exception {
// Choose something specific 1 a solr core
HttpSolrClient server = new HttpSolrClient(SOLR_URL+"my_core");
// Delete the document
server.deleteById("8");
// Delete all indexes
//solr.deleteByQuery("*:*");
// Commit changes
server.commit();
server.close();
}
/**
* The query
* @throws Exception
*/
public void querySolr() throws Exception{
HttpSolrClient solrServer = new HttpSolrClient(SOLR_URL+"my_core/");
SolrQuery query = new SolrQuery();
// The following Settings solr Query parameters
//query.set("q", "*:*");// parameter q Query all
query.set("q"," Stephen chow ");// Related query, such as a certain data a certain field contains week, star, chi 3 A word It's going to query it , this function applies to associative query
// parameter fq, to query Added filter query criteria
query.addFilterQuery("id:[0 TO 9]");//id for 0-4
// to query Increase the Boolean filter condition
//query.addFilterQuery("description: actor "); //description Data containing the word "actor" in the field
// parameter df, to query Set the default search domain
query.set("df", "name");
// parameter sort, Sets the collation that returns the result
query.setSort("id",SolrQuery.ORDER.desc);
// Set paging parameters
query.setStart(0);
query.setRows(10);// every 1 How much value on page
// parameter hl, Set the highlight
query.setHighlight(true);
// Set the highlighted fields
query.addHighlightField("name");
// Set the highlighted style
query.setHighlightSimplePre("<font color='red'>");
query.setHighlightSimplePost("</font>");
// Get query results
QueryResponse response = solrServer.query(query);
// Two results are obtained: a document collection or an entity object
// The query results in a collection of documents
SolrDocumentList solrDocumentList = response.getResults();
System.out.println(" Get the results of the query through a collection of documents ");
System.out.println(" Total number of query results: " + solrDocumentList.getNumFound());
// Traverse the list
for (SolrDocument doc : solrDocumentList) {
System.out.println("id:"+doc.get("id")+" name:"+doc.get("name")+" description:"+doc.get("description"));
}
// Get the entity object
List<Person> tmpLists = response.getBeans(Person.class);
if(tmpLists!=null && tmpLists.size()>0){
System.out.println(" Get the results of the query through a collection of documents ");
for(Person per:tmpLists){
System.out.println("id:"+per.getId()+" name:"+per.getName()+" description:"+per.getDescription());
}
}
}
public static void main(String[] args) throws Exception {
SolrJTest solr = new SolrJTest();
//solr.createSolrServer();
solr.addDoc();
solr.deleteDocumentById();
solr.querySolr();
}
}
Reference: http: / / www doc88. com/p - 6763747939865. html