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 HossMan:
http://wiki.apache.org/solr/EmbeddedSolr

The comment on the change is:
per ryan's comments, solr-user thread "Unique field and EmbeddedSolr issue.."

------------------------------------------------------------------------------
- '''It is recommended that indexing and querying Solr be done via Solr's 
standard HTTP interfaces.
+ The concept of "Embedded Solr" came about in 2007 when some Java developers 
wanted to get the goodies Solr added on top of the Lucene Java API, but without 
running a separate Java process.
+ 
+ This wiki page originally served as an example of how, by directly compiling 
against the classes provided in the Solr WAR, you could add code directly to an 
existing Java application to do the necessary setup to initialize a !SolrCore, 
add documents to the index, and execute queries.
+ 
+ Those examples are still available in the historic versions of this page, but 
they are no longer recommended (not that they ever really were).
+ 
+ '''The simplest, safest, way to use Solr is via Solr's standard HTTP 
interfaces.
  Embedding Solr is less flexible, harder to support, not as well tested, and 
should be reserved for special circumstances.'''
  
- /!\ :TODO: /!\ This entire page seems to be fairly out of date ... 
particularly since it doesn't refer to the 
org.apache.solr.client.solrj.embedded package at all.
+ That said, the ["Solrj"] Java API provides an 
[:Solrj#EmbeddedSolrServer:EmbeddedSolrServer] implementation.  This is the 
recommended way to "embed" Solr into a Java application.   It provides the 
exact same API as you would use if you were connecting to a remote Solr 
instance -- which makes it easy to convert later if you'd like; and by using it 
you can be sure you will be Using API calls that will be supported in the 
future.
  
-  * see also [#head-b67988f1360f5f68860fbddb0af55a61060724db 
DirectSolrConnection]
- 
- 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.  This 
example was used with the Solr example schema and configuration.  It is run 
using a JVM system property setting {{{-Dsolr.solr.home=...}}} (point it to 
Solr'e example "solr" directory).
- 
- If processing batches of records, performance is much better if commits are 
done at intervals.  For millions of documents being added you should optimize 
after completing indexing to reduce the number of files in the index.  For 
example, 3 million documents resulted in 858 files with a mergeFactor of 100.  
See chapter 2.8 of Lucene in Action or 
[http://lucene.zones.apache.org:8080/hudson/job/Lucene-Nightly/javadoc/org/apache/lucene/index/IndexWriter.html#optimize()
 IndexWriter] for more information.  To optimize, use the true flag for the 
[http://lucene.apache.org/solr/api/org/apache/solr/update/CommitUpdateCommand.html
 CommitUpdateCommand].
- 
- Indexing using Embedded Solr is faster 
[:SolrPerformanceFactors#head-d6f1e7a04c5b62bcb776dfcbb8950d2197a65ad8: Solr 
Performance Factors].
- 
- {{{
- 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 {
- 
-     // Rather then require system properties or JNDI, you could set the 
instance directory manually.
-     //Config.setInstanceDir( "set/the/file/programmatically" );
- 
-     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");
-     // For multiple fields, use this
-     //params.put("facet.field",new String[]{"cat","manu_exact"});
- 
-     // Include a "null" result in the facet count which indicates the
-     // number of items that don't have a value for the facet
-     params.put("facet.missing","true");
-     
-     // Add this if you use faceted browsing and want to limit the results 
-     // based on the selection made by the user
-     //params.put("fq",new String[]{"manu_exact:dell","cat:laptop"});
- 
-     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(boolean optimize) throws IOException {
-     UpdateHandler updateHandler = core.getUpdateHandler();
-     CommitUpdateCommand commitcmd = new CommitUpdateCommand(optimize);
-     updateHandler.commit(commitcmd);
-   }
-   
-   private static void commit() throws IOException {
-     commit(false);
-   }
- 
-   private static void optimize() throws IOException {
-     commit(true);
-   }
- 
-   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);
-     }
- 
-   }
- }
- }}}
- 
- = DirectSolrConnection =
- 
- In some cases, you may want to access solr through the standard interface, 
but without running an HTTP server.  In this case, consider using 
[http://svn.apache.org/repos/asf/lucene/solr/trunk/src/webapp/src/org/apache/solr/servlet/DirectSolrConnection.java
 DirectSolrConnection.java].  Unlike the above embedded example that gives you 
direct access to 
[http://lucene.zones.apache.org:8080/hudson/job/Lucene-Nightly/javadoc/org/apache/lucene/document/Document.html
 Documents], the !DirectSolrConnection interface returns Strings.
- 
- {{{
-   DirectSolrConnection solr = new DirectSolrConnection();
-   solr.request( "/update", "<add><doc><field name=\"id\">42</field><field 
name=\"subject\">Kittens</field></doc></add>" );
-   solr.request( "/update", "<commit/>" );
-     
-   String xml = solr.request( "/select?q=*:*&indent=true", null );
-   System.out.println( xml );
-     
-   // For JSON output:
-   String json = solr.request( "/select?q=*:*&wt=json", null );
-   System.out.println( json );
- }}}
- 
- /!\ Note -- !DirectSolrConnection works only with handlers registered in 
[http://wiki.apache.org/solr/SolrConfigXml solrconfig.xml].  A 
[http://wiki.apache.org/solr/SolrRequestHandler RequestHandler] must be mapped 
to /update for a request to /update to function.
- 
- [http://issues.apache.org/jira/browse/SOLR-212 SOLR-212] includes a Cocoa JNI 
example program that may be useful.
- 

Reply via email to