This article uses solr3.4 as an example of use, which has been debugged. Later versions may require code changes.
1. Start solr. After unpacking the solr archive, there is a war package under solr_home\dist. One way is to deploy the war package into a web container. I'm here to save trouble, just use the built-in jetty. The startup method is to run Java –jar start.jar in the example directory. The background will show that the port 8983 is occupied by default. At this point, there should be a response to visiting http://localhost:8983/solr.
2. Enter data into solr. One way is to use the api (see code below), and another way is to use the command line to index some disk files. Such as executing in the exampledoc directory: java–jar post.jar *.xml.
The following code is to display data entry, query and other functions. The project needs to introduce the following jar packages: apache-solr-solrj-3.4.0.jar, commons-httpclient-3.1.jar, commons-codec-1.4.jar, slf4j-api-1.6.1.jar, commons-logging- 1.1.3.jar
import java.util.*;
import org.apache.solr.client.solrj.*;
import org.apache.solr.client.solrj.impl.*;
import org.apache.solr.client.solrj.response.*;
import org.apache.solr.common.*;
public class SolrTest {
public static void main(String[] args)throws Exception{
String url = "http://localhost:8983/solr";
SolrServer server = new CommonsHttpSolrServer(url);
server.deleteByQuery( "*:*" );
SolrInputDocument doc1 = new SolrInputDocument();
doc1.addField( "id", "book1" );
doc1.addField( "name", "The Legend of Heaven and Dragon Slayer");
doc1.addField( "price", 29 );
SolrInputDocument doc2 = new SolrInputDocument();
doc2.addField( "id", "book2" );
doc2.addField( "name", "The Condor Heroes" );
doc2.addField( "price", 20 );
SolrInputDocument doc3 = new SolrInputDocument();
doc3.addField( "id", "book3");
doc3.addField( "name", "Dragon Babu");
doc3.addField( "price", 33.8 );
SolrInputDocument doc4 = new SolrInputDocument();
doc4.addField( "id", "book4" );
doc4.addField( "name", "The Legend of the Condor Heroes");
doc4.addField( "price", 25.5 );
List<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
docs.add(doc1);
docs.add(doc2);
docs.add(doc3);
docs.add(doc4);
server.add(docs);
server.commit();
SolrQuery query = new SolrQuery();
query.setQuery( "name:" );
query.setStart(0);
query.setRows(5);
query.addSortField( "price", SolrQuery.ORDER.desc );
query.setHighlight(true);
query.addHighlightField("name");
query.setHighlightSimplePre("<font color=\"red\">");
query.setHighlightSimplePost("</font>");
query.set("hl.usePhraseHighlighter",true);
query.set("hl.highlightMultiTerm",true);
query.set("hl.snippets",3);
query.set("hl.fragsize", 5);
QueryResponse rsp = server.query( query );
System.out.println(rsp);
SolrDocumentList docsList = rsp.getResults();
//Get the highlighted result
Map<String,Map<String,List<String>>> hl = rsp.getHighlighting();
for(Iterator<SolrDocument> doc =docsList.iterator();doc.hasNext();){
SolrDocument d = doc.next();
System.out.print(d.getFieldValue("id")+":");
System.out.println(d.getFieldValue("name"));
}
}
}