Looks good. I added a comment to the wiki page that shows how to manually configure the instance directory.

Building a document with DocumentBuilder is easy enough, but it requires you to do have to have the schema and is a clunky interface to talk with your model objects. In SOLR-193, I started a general SolrDocument interface that I hope we can use for SOLR-20 and for 'modifiable' (ie, load then store) documents. The documents returned from SOLR-20 should also implement SolrDocument.

I hope the SOLR-20 will work the same with an embedded server as it does with http. This would also make a bunch of testing easier as we could avoid using xpath...

The choice to use LocalSolrQueryRequest just depends how you want to fill up SolrParams. You can fill them directly (I do) or use the utility functions in LocalSolrQueryRequest. But note that the "standard" parameter is not used, you are selecting the handler directly.

One last comment... deleteByQuery() forces a commit internally. Calling commit() after delteByQuery() is redundant.

ryan


Erik Hatcher wrote:
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