1 5 package org.exoplatform.services.indexing; 6 7 import org.apache.lucene.document.Document; 8 import org.apache.lucene.document.Field; 9 import org.apache.lucene.index.Term; 10 import org.apache.lucene.search.IndexSearcher; 11 import org.apache.lucene.search.TermQuery; 12 import org.picocontainer.Startable; 13 18 abstract public class BaseIndexerPlugin implements IndexerPlugin, Startable { 19 final static public String [] MANDATORY_FIELDS = 20 {IndexingService.IDENTIFIER_FIELD, IndexingService.MODULE_FIELD, 21 IndexingService.TITLE_FIELD, IndexingService.DOCUMENT_FIELD}; 22 23 private String [] indexField_ ; 24 private Searcher searcher_ ; 25 protected IndexingService iservice_ ; 26 27 public BaseIndexerPlugin(IndexingService iservice) { 28 iservice.addIndexerPlugin(this) ; 29 iservice_ = iservice ; 30 } 31 32 33 abstract public String getPluginIdentifier() ; 34 35 final public String [] getMandatoryIndexFields() { return MANDATORY_FIELDS ; } 36 public String [] getCustomizedIndexFields() { return indexField_ ; } 37 38 synchronized public Searcher getSearcher() throws Exception { 39 if(searcher_ == null) { 40 IndexSearcher isearcher = new IndexSearcher(iservice_.getIndexDatabaseLocation()) ; 41 searcher_ = new Searcher(isearcher, iservice_.getAnalyzer()); 42 searcher_.setQueryModules(new TermQuery(new Term(IndexingService.MODULE_FIELD, getPluginIdentifier()))) ; 43 } 44 return searcher_ ; 45 } 46 47 synchronized public void resetSearcher() { 48 searcher_ = null ; 49 } 50 51 protected Document createBaseDocument(String identifier, String author, 52 String title, String description, 53 String textToIndex, String viewRole) { 54 Document doc = new Document(); 55 if(description == null) { 56 if(textToIndex.length() < 50 ) description = textToIndex ; 57 else description = textToIndex.substring(0, 50) ; 58 } 59 if(viewRole == null) viewRole = "owner" ; 60 doc.add(Field.Keyword(IndexingService.IDENTIFIER_FIELD, identifier)) ; 61 doc.add(Field.Keyword(IndexingService.MODULE_FIELD, getPluginIdentifier())) ; 62 doc.add(Field.Keyword(IndexingService.AUTHOR_FIELD, author)) ; 63 doc.add(Field.Text(IndexingService.TITLE_FIELD, title)) ; 64 doc.add(Field.Text(IndexingService.DESCRIPTION_FIELD, description)) ; 65 doc.add(Field.UnStored(IndexingService.DOCUMENT_FIELD, textToIndex)) ; 66 doc.add(Field.UnStored(IndexingService.DOCUMENT_ACCESS_ROLE, viewRole)) ; 67 return doc ; 68 } 69 70 public void removeIndex() throws Exception { 71 Term term = new Term(IndexingService.MODULE_FIELD, getPluginIdentifier()) ; 72 iservice_.queueDeleteDocuments(term) ; 73 } 74 75 public void reindex() throws Exception { 76 removeIndex() ; 77 } 78 79 protected String getContentDescription(String text, int size) { 80 char[] chars = text.toCharArray() ; 81 StringBuffer b = new StringBuffer () ; 82 int counter = 0 ; 83 for(int i = 0 ; i < chars.length && counter < size; i++ ) { 84 if(chars[i] == '<') { 85 while(chars[i] != '>' && i < chars.length) { 86 i++ ; 87 } 88 b.append(" - ") ; 89 } else { 90 b.append(chars[i]) ; 91 counter++ ; 92 } 93 } 94 return b.toString() ; 95 } 96 97 public void start() {} 98 99 public void stop() {} 100 } | Popular Tags |