Ryan et al - I've been experimenting with Solr running embedded as shown in the wiki post below. I know there are better ways to do some of these things and I'd love feedback on where this can use improvement. For example, creating a document is a little clunky with DocumentBuilder, but I think some of the patches available in JIRA make this cleaner. I also am unsure if LocalSolrQueryRequest has better alternatives.

Thoughts?

Thanks,
        Erik



On Apr 15, 2007, at 9:33 PM, Apache Wiki wrote:

Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Solr Wiki" for change notification.

The following page has been changed by ErikHatcher:
http://wiki.apache.org/solr/EmbeddedSolr

---------------------------------------------------------------------- --------
- Coming soon...
+ Below is a program experimenting with Solr embedded via direct Java API access, demonstrating, adding/updating, deleting by id and query, and searching.

+ This program is compiled against both the Solr and Lucene JAR files. It is run using a JVM system property setting {{{- Dsolr.solr.home=...}}}
+
+ {{{
+ import org.apache.solr.request.SolrRequestHandler;
+ import org.apache.solr.request.SolrQueryResponse;
+ import org.apache.solr.request.SolrQueryRequest;
+ import org.apache.solr.request.LocalSolrQueryRequest;
+ import org.apache.solr.search.DocIterator;
+ import org.apache.solr.search.DocList;
+ import org.apache.solr.core.SolrCore;
+ import org.apache.solr.update.*;
+ import org.apache.lucene.index.IndexReader;
+ import org.apache.lucene.document.Document;
+
+ import java.io.IOException;
+ import java.util.HashMap;
+ import java.util.Map;
+
+ public class EmbeddedSolr {
+   private static SolrCore core;
+
+   public static void main(String[] args) throws IOException {
+     core = SolrCore.getSolrCore();
+     System.out.println("-----\nBefore:");
+     search("embedded");
+
+     System.out.println("-----\ndoc1 added:");
+     addDocument("doc1", "embedded solr");
+     search("embedded");
+
+     System.out.println("-----\ndoc2 added:");
+     addDocument("doc2", "solr embedded");
+     search("embedded");
+
+     System.out.println("-----\ndoc1 deleted:");
+     deleteById("doc1");
+     search("embedded");
+
+     System.out.println("-----\n\"embedded\" deleted:");
+     deleteByQuery("embedded");
+     search("embedded");
+
+
+     System.out.println("-----\nwith facets:");
+     HashMap params = new HashMap();
+     params.put("facet","true");
+     params.put("facet.field","cat");
+
+     SolrQueryResponse response = search("ipod", params);
+ System.out.println("response = " + response.getValues().get ("facet_counts"));
+
+     core.close();
+   }
+
+ private static void deleteByQuery(String query) throws IOException {
+     DeleteUpdateCommand cmd = new DeleteUpdateCommand();
+     cmd.query = query;
+     cmd.fromPending=true;
+     cmd.fromCommitted=true;
+
+     UpdateHandler updateHandler = core.getUpdateHandler();
+     updateHandler.deleteByQuery(cmd);
+
+     commit();
+   }
+
+   private static void deleteById(String id) throws IOException {
+     DeleteUpdateCommand cmd = new DeleteUpdateCommand();
+     cmd.id = id;
+     cmd.fromPending=true;
+     cmd.fromCommitted=true;
+
+     UpdateHandler updateHandler = core.getUpdateHandler();
+     updateHandler.delete(cmd);
+
+     commit();
+   }
+
+ private static void addDocument(String id, String name) throws IOException {
+     UpdateHandler updateHandler = core.getUpdateHandler();
+     AddUpdateCommand addcmd = new AddUpdateCommand();
+
+     DocumentBuilder builder = new DocumentBuilder(core.getSchema());
+     builder.startDoc();
+     builder.addField("id", id);
+     builder.addField("name", name);
+     addcmd.doc = builder.getDoc();
+     addcmd.allowDups = false;
+     addcmd.overwritePending = true;
+     addcmd.overwriteCommitted = true;
+     updateHandler.addDoc(addcmd);
+
+     commit();
+   }
+
+   private static void commit() throws IOException {
+     UpdateHandler updateHandler = core.getUpdateHandler();
+     CommitUpdateCommand commitcmd = new CommitUpdateCommand(false);
+     updateHandler.commit(commitcmd);
+   }
+
+ private static SolrQueryResponse search(String query) throws IOException {
+     return search(query, new HashMap());
+   }
+
+ private static SolrQueryResponse search(String query, Map params) throws IOException {
+     SolrRequestHandler handler = core.getRequestHandler("");
+
+ SolrQueryRequest request = new LocalSolrQueryRequest(core, query, "standard", 0, 100, params);
+     SolrQueryResponse response = new SolrQueryResponse();
+     core.execute(handler, request, response);
+
+     DocList docs = (DocList) response.getValues().get("response");
+     printDocs(docs);
+
+     return response;
+   }
+
+   private static void printDocs(DocList docs) throws IOException {
+     IndexReader reader = core.getSearcher().get().getReader();
+     System.out.println("response.size() = " + docs.size());
+     DocIterator iter = docs.iterator();
+     while (iter.hasNext()) {
+       Document doc = reader.document(iter.next());
+       System.out.println("doc = " + doc);
+     }
+
+   }
+ }
+ }}}
+

Reply via email to