Solrj provides java API to access the Apache Solr web application.
1. Checking the document is allready indexed or not
2. Indexing a document:
3. Committing the changes:
1. Checking the document is allready indexed or not
import java.io.*;
import org.apache.solr.client.solrj.request.*;
import org.apache.solr.client.solrj.response.*;
import org.apache.solr.client.solrj.util.ClientUtils;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.util.XML;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.params.FacetParams;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
import org.apache.solr.client.solrj.SolrServerException;
public class TestIndexed
{
public static void main(String args[])
{
String docId = args[0];
CommonsHttpSolrServer solrServer = new
CommonsHttpSolrServer("http://localhost:8983/solr");
ModifiableSolrParams params = new ModifiableSolrParams();
params.set("q", "id: " + docId);
// Limit the response fields to id only.
params.set("fl", "id");
QueryResponse qResponse= solrServer.query(params);
// Check Response is SUCCESS
if (qResponse.getStatus() == 0) {
SolrDocumentList sdl = qResponse.getResults();
if (sdl.getNumFound() >= 1L) {
System.out.println("Document with ID: " + docID + " is indexed.");
} else {
System.out.println("Document with ID: " + docID + " is not indexed.");
}
}
}
}
2. Indexing a document:
// Create a solr document
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", "MYID_PAG_1-11-1983", 1.0f);
doc.addField("name", "Prashant Gadekar", 1.0f);
doc.addFiedl("dob", "01-11-1983", 1.0f);
...
UpdateResponse upres = this.solrServer.add(doc);
System.out.println("Response from Solr server : " + upres.toString());
if (upres.getStatus() == 0) {
System.out.println("Document added successfully");
}
3. Committing the changes:
UpdateResponse upres = solrServer.commit(true, true);
System.out.println("Response from solr server: " + upres.toString());
if (upres.getStatus() == 0) {
System.out.println("Committed successfully");
}
No comments:
Post a Comment