You are viewing a plain text version of this content. The canonical link for it is here.
Posted to slide-dev@jakarta.apache.org by un...@apache.org on 2004/09/13 18:52:23 UTC

cvs commit: jakarta-slide/src/stores/org/apache/slide/index TextContainsExpressionFactory.java TextContentIndexer.java TextContainsExpression.java

unico       2004/09/13 09:52:23

  Modified:    src/stores/org/apache/slide/index
                        TextContainsExpressionFactory.java
                        TextContentIndexer.java TextContainsExpression.java
  Log:
  make Lucene analyzer class configurable thanks to patch provided by Guido Casper
  (gcasper <at> apache org)
  
  Revision  Changes    Path
  1.3       +9 -5      jakarta-slide/src/stores/org/apache/slide/index/TextContainsExpressionFactory.java
  
  Index: TextContainsExpressionFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-slide/src/stores/org/apache/slide/index/TextContainsExpressionFactory.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TextContainsExpressionFactory.java	6 Sep 2004 12:40:51 -0000	1.2
  +++ TextContainsExpressionFactory.java	13 Sep 2004 16:52:23 -0000	1.3
  @@ -31,6 +31,8 @@
   import org.apache.slide.content.NodeProperty;
   import org.jdom.Element;
   
  +import org.apache.lucene.analysis.Analyzer;
  +
   import java.util.Collection;
   
   /**
  @@ -44,6 +46,7 @@
       protected PropertyProvider propertyProvider;
   
       private String rootPath;
  +    private Analyzer analyzer;
   
       /**
        * Constructor
  @@ -51,9 +54,10 @@
        * @param    rootPath           path to the content files
        *
        */
  -    public TextContainsExpressionFactory (String rootPath)
  +    public TextContainsExpressionFactory (String rootPath, Analyzer analyzer)
       {
           this.rootPath = rootPath;
  +        this.analyzer = analyzer;
       }
   
       /**
  @@ -127,7 +131,7 @@
           if (name.equals ("contains"))
           {
               String searchedText = e.getTextTrim();
  -            result = new TextContainsExpression (searchedText, rootPath);
  +            result = new TextContainsExpression (searchedText, rootPath, analyzer);
           }
   
           return result;
  
  
  
  1.6       +49 -9     jakarta-slide/src/stores/org/apache/slide/index/TextContentIndexer.java
  
  Index: TextContentIndexer.java
  ===================================================================
  RCS file: /home/cvs/jakarta-slide/src/stores/org/apache/slide/index/TextContentIndexer.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- TextContentIndexer.java	3 Aug 2004 15:01:46 -0000	1.5
  +++ TextContentIndexer.java	13 Sep 2004 16:52:23 -0000	1.6
  @@ -38,6 +38,7 @@
   import org.apache.lucene.index.IndexReader;
   import org.apache.lucene.index.Term;
   import org.apache.lucene.analysis.standard.StandardAnalyzer;
  +import org.apache.lucene.analysis.Analyzer;
   import org.apache.lucene.document.Document;
   import org.apache.lucene.document.Field;
   
  @@ -60,12 +61,15 @@
   
       private static final String INDEX_PATH = "indexpath";
       private static final String INCLUDES = "includes";
  +    private static final String ANALYZER = "analyzer";
       
       public static final String URI_FIELD = "uri";
       public static final String CONTENT_TEXT = "content";
   
       private String indexpath = "";
       private Collection includes;
  +    private String analyzerClassName;
  +    private Analyzer analyzer;
       private boolean started = false;
   
     /**
  @@ -79,10 +83,12 @@
       public void initialize(NamespaceAccessToken token)
           throws ServiceInitializationFailedException
      {
  +      initAnalyzer();
  +
         IndexWriter indexWriter = null;
         try
         {
  -         indexWriter = new IndexWriter(indexpath, new StandardAnalyzer(), false);
  +         indexWriter = new IndexWriter(indexpath, analyzer, false);
         }
         // will fail, if not yet exists
         catch (IOException e)
  @@ -90,7 +96,7 @@
            try
            {
               // create index
  -            indexWriter = new IndexWriter(indexpath, new StandardAnalyzer(), true);
  +            indexWriter = new IndexWriter(indexpath, analyzer, true);
            }
            catch (IOException ex)
            {
  @@ -127,7 +133,7 @@
         IndexWriter indexWriter = null;
         try
         {
  -         indexWriter = new IndexWriter(indexpath, new StandardAnalyzer(), false);
  +         indexWriter = new IndexWriter(indexpath, analyzer, false);
   
            // Create document
            Document doc = new Document();
  @@ -202,7 +208,7 @@
               indexReader.delete(term);
               indexReader.close();
   
  -            indexWriter = new IndexWriter(indexpath, new StandardAnalyzer(), false);
  +            indexWriter = new IndexWriter(indexpath, analyzer, false);
   
               // Create document
               Document doc = new Document();
  @@ -274,7 +280,7 @@
               indexReader.delete(term);
               indexReader.close();
   
  -            indexWriter = new IndexWriter(indexpath, new StandardAnalyzer(), false);
  +            indexWriter = new IndexWriter(indexpath, analyzer, false);
               indexWriter.optimize();
   
               if (getLogger().isEnabled(Logger.DEBUG)) {
  @@ -308,7 +314,7 @@
        */
      public IBasicExpressionFactory getBasicExpressionFactory()
      {
  -      return new TextContainsExpressionFactory(indexpath);
  +      return new TextContainsExpressionFactory(indexpath, analyzer);
      }
   
   
  @@ -364,6 +370,7 @@
                   this.includes.add(tokenizer.nextToken());
               }
           }
  +        analyzerClassName = (String)parameters.get (ANALYZER);
      }
   
       /**
  @@ -408,4 +415,37 @@
           }
           return false;
       }
  +
  +
  +    protected void initAnalyzer() throws ServiceInitializationFailedException {
  +
  +        if (analyzerClassName == null || analyzerClassName.length() == 0) {
  +            getLogger().log("using Lucene StandardAnalyzer", LOG_CHANNEL, Logger.INFO);
  +            analyzer = new StandardAnalyzer();
  +
  +        } else {
  +            getLogger().log("loading Lucene analyzer: " + analyzerClassName, LOG_CHANNEL, Logger.INFO);
  +
  +            try {
  +                Class analyzerClazz = Class.forName(analyzerClassName);
  +                analyzer = (Analyzer)analyzerClazz.newInstance();
  +
  +            } catch (ClassNotFoundException cnfe) {
  +                getLogger().log("Error while instantiating analyzer " + 
  +                                analyzerClassName + cnfe.getMessage(), LOG_CHANNEL, Logger.ERROR);
  +                throw new ServiceInitializationFailedException(this, cnfe);
  +
  +            } catch (InstantiationException ie) {
  +                getLogger().log("Error while instantiating analyzer " + 
  +                                analyzerClassName + ie.getMessage(), LOG_CHANNEL, Logger.ERROR);
  +                throw new ServiceInitializationFailedException(this, ie);
  +
  +            } catch (IllegalAccessException iae) {
  +                getLogger().log("Error while instantiating analyzer " + 
  +                                analyzerClassName + iae.getMessage(), LOG_CHANNEL, Logger.ERROR);
  +                throw new ServiceInitializationFailedException(this, iae);
  +            }
  +        }
  +    }
  +
   }
  
  
  
  1.3       +6 -7      jakarta-slide/src/stores/org/apache/slide/index/TextContainsExpression.java
  
  Index: TextContainsExpression.java
  ===================================================================
  RCS file: /home/cvs/jakarta-slide/src/stores/org/apache/slide/index/TextContainsExpression.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TextContainsExpression.java	6 Sep 2004 12:40:51 -0000	1.2
  +++ TextContainsExpression.java	13 Sep 2004 16:52:23 -0000	1.3
  @@ -37,7 +37,6 @@
   import org.apache.lucene.search.Query;
   import org.apache.lucene.search.Hits;
   import org.apache.lucene.analysis.Analyzer;
  -import org.apache.lucene.analysis.standard.StandardAnalyzer;
   import org.apache.lucene.queryParser.QueryParser;
   import org.apache.lucene.document.Document;
   
  @@ -52,8 +51,8 @@
       protected static final String LOG_CHANNEL = TextContainsExpression.class.getName();
   
       String searchedText;
  -
       String indexPath;
  +    Analyzer analyzer;
   
       /** backptr to the factory */
       IBasicExpressionFactory factory;
  @@ -63,10 +62,11 @@
        * For your concrete implementation you are free, which parameters have to
        * be passed, let the factory give you everything you need.
        */
  -    TextContainsExpression (String searchedText, String rootPath)
  +    TextContainsExpression (String searchedText, String rootPath, Analyzer analyzer)
       {
           this.searchedText = searchedText;
           this.indexPath = rootPath;
  +        this.analyzer = analyzer;
       }
   
       /**
  @@ -108,7 +108,6 @@
           try
           {
               Searcher searcher = new IndexSearcher(indexPath);
  -            Analyzer analyzer = new StandardAnalyzer();
   
               Query query = QueryParser.parse(searchedText, TextContentIndexer.CONTENT_TEXT, analyzer);
               Hits hits = searcher.search (query);
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: slide-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: slide-dev-help@jakarta.apache.org