Actually, I implemented that feature for the 1.2.0 version of solr
(the one I use)

It allows you to speficy the IndexSearcher used by solr in the schema
configuration file:

<!-- Custom Searcher class that must inherit from the lucene IndexSearcher
     and have a contructor with only one (IndexReader r) argument -->
<searcher class="com.acme.lucene.search.MyGreatCustomSearcher"/>

If the specified class cant be loaded, a severe message is issued in
the log and solr
falls back to the hardcoded lucene IndexSearcher .

The patch to apply is attached to this email.

I also created an issue in the solr jira:
https://issues.apache.org/jira/browse/SOLR-288
but I didn t find the way to upload the patch.

Thanks for your comments.

Jerome.

On 7/5/07, Jérôme Etévé <[EMAIL PROTECTED]> wrote:
Hi all !

I need a new feature in solr : to allow the configuration of the
IndexSearcher class in the schema configuration to override the lucene
IndexSearcher .
I noticed that there's only one point in the code where the searcher is built:

<code>
in org/apache/solr/search/SolrIndexSearcher.java:

  private SolrIndexSearcher(IndexSchema schema, String name,
IndexReader r, boolean closeReader, boolean enableCache) {
    this.schema = schema;
    this.name = "Searcher@" + Integer.toHexString(hashCode()) +
(name!=null ? " "+name : "");

    log.info("Opening " + this.name);

    reader = r;
/****** HERE *********/
   searcher = new IndexSearcher(r);
</code>

I'd like to allow a new tag in the schema :
<schema name="..." version="1.1">
  <luceneIndexSearcher class="com.acme.lucene.searcher.MyOwnSearcher" />
....
</schema>

I dont exactly know what is the best way to do it.
I was think of:

* In IndexSchema:

implement a method
String getLuceneIndexSearcherClassName()

* In SolrIndexSearcher
  in  private SolrIndexSearcher:

  String idxSearcherClassName = schema.getLuceneIndexSearcherClassName()
  // Then load the class itself
  // Then build a new instance of this class with the IndexReader r

 What solr special class loader and instance builder do I have to use
to do the last two operation ?

Can I use directly :

Class idxSearcherClass = Config.findClass(idxSearcherClassName)

and then build a idxSearcher by using the standard java.lang.Class methods ?

Am I in the right and does it fit with the solr architecture to do that ?

I'd be perfectly happy to implement that and submit a patch.

Thanks for your comments and answers.

Jerome

--
Jerome Eteve.
[EMAIL PROTECTED]
http://jerome.eteve.free.fr/



--
Jerome Eteve.
[EMAIL PROTECTED]
http://jerome.eteve.free.fr/
diff -Nurp src_old/java/org/apache/solr/schema/IndexSchema.java src/java/org/apache/solr/schema/IndexSchema.java
--- src_old/java/org/apache/solr/schema/IndexSchema.java	2007-05-30 16:51:06.000000000 +0100
+++ src/java/org/apache/solr/schema/IndexSchema.java	2007-07-05 16:46:11.000000000 +0100
@@ -125,6 +125,13 @@ public final class IndexSchema {
   public Collection<SchemaField> getRequiredFields() { return requiredFields; }
 
   private Similarity similarity;
+  private String searcherClassName = null ;
+
+    /**
+     * Returns the indexSearcherClassName to use with this index
+     */
+    public String getSearcherClassName() { return searcherClassName ;}
+
 
   /**
    * Returns the Similarity used for this index
@@ -449,6 +456,15 @@ public final class IndexSchema {
       similarity = (Similarity)Config.newInstance(node.getNodeValue().trim());
       log.fine("using similarity " + similarity.getClass().getName());
     }
+    
+    // Grab indexSearcher class
+    node = (Node) xpath.evaluate("/schema/searcher/@class" , document, XPathConstants.NODE);
+    if ( node != null ){
+	searcherClassName  = node.getNodeValue().trim() ;
+	log.info("will use " + searcherClassName + " for IndexSearcher class");
+    }else{
+	log.info("No customized index searcher class - will use default");
+    }
 
     node = (Node) xpath.evaluate("/schema/defaultSearchField/text()", document, XPathConstants.NODE);
     if (node==null) {
diff -Nurp src_old/java/org/apache/solr/search/SolrIndexSearcher.java src/java/org/apache/solr/search/SolrIndexSearcher.java
--- src_old/java/org/apache/solr/search/SolrIndexSearcher.java	2007-05-30 16:51:15.000000000 +0100
+++ src/java/org/apache/solr/search/SolrIndexSearcher.java	2007-07-05 17:45:18.000000000 +0100
@@ -41,6 +41,8 @@ import java.util.*;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
+import java.lang.reflect.Constructor ;
+
 
 /**
  * SolrIndexSearcher adds schema awareness and caching functionality
@@ -104,7 +106,33 @@ public class SolrIndexSearcher extends S
     log.info("Opening " + this.name);
 
     reader = r;
-    searcher = new IndexSearcher(r);
+    //searcher = new IndexSearcher(r);
+    
+    // Eventually build a searcher according to configuration
+    String idxSearcherClassName = schema.getSearcherClassName() ;
+    if ( idxSearcherClassName == null ){
+	log.info("Using hardcoded standard lucene IndexSearcher");
+	searcher = new IndexSearcher(r);
+    }else{
+	log.info("Attempting to load " + idxSearcherClassName );
+	IndexSearcher customsearcher ;
+	try{
+	    Class idxSearcherClass = SolrConfig.config.findClass(idxSearcherClassName) ;
+	    Constructor constr = idxSearcherClass.getConstructor( new Class[]{ r.getClass() } );
+	    customsearcher = (org.apache.lucene.search.IndexSearcher)constr.newInstance(new Object[]{ r }) ;
+	    log.info("Instance of "+ idxSearcherClassName+ " built");
+	}catch(Exception e){
+	    log.severe("Couldnt built an instance of " + idxSearcherClassName );
+	    log.severe("Root cause: " + e.getMessage() );
+	    log.info("Falling back to hardcoded standard lucene IndexSearcher");
+	    customsearcher = new IndexSearcher(r);
+	}
+	searcher = customsearcher ;
+    }
+    
+    
+    
+    
     this.closeReader = closeReader;
     searcher.setSimilarity(schema.getSimilarity());
 

Reply via email to