1. Download Solr Engine

http://lucene.apache.org/solr/downloads.html

2. Java Example

Connect to solr engine

SolrServer server = new HttpSolrServer(“http://localhost:8983/solr/<collection>”);

Create a document

SolrInputDocument doc1 = new SolrInputDocument();
doc1.addField( “id”, “1”, 1.0f );
doc1.addField( “name”, “Paul”, 1.0f );
doc1.addField( “type”, “excel” );
doc1.addField( “content”, “some content to search” );
doc1.addField( “tag”, “tag1” );
doc1.addField( “tag”, “tag2” );

Add documents to a collection

Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
docs.add( doc1 );
docs.add( doc2 );
server.add( docs );
server.commit();

Search documents

SolrQuery query = new SolrQuery();
query.setHighlight(true).setHighlightSnippets(1);
query.setParam(“hl.fl”, “content,type”);
query.setQuery( “content:criteria1 OR content:criteria2 OR content:criteria3” );

QueryResponse rsp = server.query( query );
SolrDocumentList documents = rsp.getResults();

You can find more info at http://lucene.apache.org/solr/index.html

By admin