SOLR INSTALLATION AND USAGE BRIEFLY
Solr is a standalone server that makes all the indexing works and that is related with your data which will be examined in searching.
Briefly : In a java program you will create a document from your object in your program and you will send it to your solr engine. Solr will get this information to its own indexing files. Then you can query, add new, update the existed, delete the documents that are in the solr. These processes can be done using solr user interface or programmatically from Java with solrj. In the first strategy if you use the user interface of the solr, you will realize that after querying the results will be returned as XML. If you pay attention to the url this operations have special urls. That you can also use this functionality if you do not want to use solrj. However solrj is easy to deal with the data that are returned by the solr. The Solr XML has two sections, the first part and the second part. The data that you send for the indexing will be kept in the second part. This is the brief description of Solr as far as i used. Of course it has much more usage areas and strategies.
First of all we need to set up the solr environment.
To start this download the solr from this url
I downloaded the Apache Solr 3.6.0d when i set. After you download extract in one of your directory i put it to D:\apache-solr-3.6.0 there will be some folders and files you will see.
Secondly
open your eclipse and create a new project i created Dynamic Web Project.
Add these libraries into your classpath - lib
Thirdly
Open your solr folders again. In your solr folder there will be a example folder, there is solr folder. This folder will be your example solr folder.
In this folder you will see bin, conf, data and solr.xml. The data folder contains your indexes. Conf folder contains files about your solr configuration.
Then open a command line and go to your solr../example directory from the command line and type java -jar start.jar Then you will see the running of the solr server. Then open this url from your browser http://localhost:8983/solr/ after successful go to the Solr Admin then you can make your queries from your solr user interface.
Using solr from java is the SOLRJ.
Lets code the project that we opened at step two.
HttpSolrServer httpSolrServer = new HttpSolrServer("http://localhost:8983/solr/");
SolrInputDocument doc1 = new SolrInputDocument();
doc1.addField( "id", "1", 1.0f );
doc1.addField( "name", "can", 1.0f );
doc1.addField( "price", 1000 );
Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
docs.add( doc1 );
httpSolrServer.add( docs );
httpSolrServer.commit();
UpdateRequest req = new UpdateRequest();
req.setAction( UpdateRequest.ACTION.COMMIT, false, false );
req.add( docs );
UpdateResponse rsp = req.process(httpSolrServer);
SolrQuery query = new SolrQuery();
query.setQuery("id:*");
query.setHighlight(true).setHighlightSnippets(1);
//httpSolrServer.deleteByQuery("id:*",11);
QueryResponse queryResponse = httpSolrServer.query(query);
Iterator<SolrDocument> results= queryResponse.getResults().iterator();
while (results.hasNext()) {
String id = (String) results.next().getFieldValue("id");
}
In the code above we created a document and added to the docs and then we created a query that we for the all ids and get response from the result of query is taken by the field values. If you want to delete the all the records that includes id field uncomment the line below from code.
//httpSolrServer.deleteByQuery("id:*",11);
the 11 is the ms that this action will be committed
You can also rollback the operations with the code below
httpSolrServer.rollback();

No comments:
Post a Comment