cvs commit: spice/sandbox/repository/componenthaus/src/java/org/componenthaus/search LuceneSearchService.java
Mike Hogan <[email protected]>
| Newsgroups | gmane.comp.java.spice.cvs |
|---|---|
| Message-ID | <[email protected]> |
hogie 03/11/03 13:15:30
Modified: sandbox/repository/componenthaus/src/java/org/componenthaus/search
LuceneSearchService.java
Log:
Can submit more than one interface per jar
Revision Changes Path
1.3 +25 -7 spice/sandbox/repository/componenthaus/src/java/org/componenthaus/search/LuceneSearchService.java
Index: LuceneSearchService.java
===================================================================
RCS file: /cvsroot/spice/spice/sandbox/repository/componenthaus/src/java/org/componenthaus/search/LuceneSearchService.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- LuceneSearchService.java 5 Oct 2003 12:26:38 -0000 1.2
+++ LuceneSearchService.java 3 Nov 2003 21:15:29 -0000 1.3
@@ -10,22 +10,30 @@
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Searcher;
+import org.apache.lucene.search.HitCollector;
import java.io.File;
import java.io.IOException;
import java.util.List;
public class LuceneSearchService implements SearchService {
+ static final String ID_FIELD_NAME = "id";
+ static final String CONTENTS_FIELD_NAME = "contents";
private static final String INDEX_FILE_PATH = "index";
- //This is impossible to unit test as IndexWriter is final
+ private final LuceneObjectFactory luceneObjectFactory;
+
+ public LuceneSearchService(LuceneObjectFactory luceneObjectFactory) {
+ this.luceneObjectFactory = luceneObjectFactory;
+ }
+
public void index(String componentId, String componentDescription) throws SearchService.Exception {
IndexWriter writer = null;
try {
- writer = new IndexWriter(INDEX_FILE_PATH, new StandardAnalyzer(), !indexExists());
+ writer = luceneObjectFactory.createIndexWriter(INDEX_FILE_PATH, new StandardAnalyzer(), !indexExists());
final Document document = new Document();
- document.add(Field.Text("id", componentId));
- document.add(Field.Text("contents", componentDescription));
+ document.add(Field.Text(ID_FIELD_NAME, componentId));
+ document.add(Field.Text(CONTENTS_FIELD_NAME, componentDescription));
writer.addDocument(document);
writer.optimize();
writer.close();
@@ -38,23 +46,33 @@
return new File(INDEX_FILE_PATH).exists();
}
- //This is impossible to unit test as Hits is final, and the constructor is package protected
+ //This is tough to unit test as Hits is final, and the constructor is package protected
public int search(final String query, int beginIndex, final int endIndex, final List collector) throws SearchService.Exception {
int numHits = 0;
try {
final Searcher searcher = new IndexSearcher(INDEX_FILE_PATH);
final Analyzer analyzer = new StandardAnalyzer();
- final Query q = QueryParser.parse(query, "contents", analyzer);
+ final Query q = QueryParser.parse(query, CONTENTS_FIELD_NAME, analyzer);
final Hits hits = searcher.search(q);
numHits = hits.length();
while (beginIndex <= endIndex && beginIndex < numHits) {
final Document doc = hits.doc(beginIndex++);
- collector.add(doc.get("id"));
+ collector.add(doc.get(ID_FIELD_NAME));
}
} catch (java.lang.Exception e) {
throw new SearchService.Exception("Exception performing Lucene search",e);
}
return numHits;
+ }
+
+ static interface LuceneObjectFactory {
+ IndexWriter createIndexWriter(String indexFilePath, Analyzer analyzer, boolean createIndex) throws IOException;
+ }
+
+ public static final class DefaultLuceneObjectFactory implements LuceneObjectFactory {
+ public IndexWriter createIndexWriter(String indexFilePath, Analyzer analyzer, boolean createIndex) throws IOException {
+ return new IndexWriter(indexFilePath,analyzer,createIndex);
+ }
}
}
-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
Does SourceForge.net help you be more productive? Does it
help you create better code? SHARE THE LOVE, and help us help
YOU! Click Here: http://sourceforge.net/donate/