1 23 24 package org.apache.slide.index; 25 26 import org.apache.slide.search.basic.*; 27 import org.apache.slide.search.BadQueryException; 28 import org.apache.slide.search.SearchException; 29 import org.apache.slide.search.RequestedResource; 30 import org.apache.slide.structure.ObjectNode; 31 import org.apache.slide.structure.SubjectNode; 32 import org.apache.slide.common.SlideException; 33 import org.apache.slide.common.Domain; 34 import org.apache.slide.util.logger.Logger; 35 import org.apache.lucene.search.Searcher; 36 import org.apache.lucene.search.IndexSearcher; 37 import org.apache.lucene.search.Query; 38 import org.apache.lucene.search.Hits; 39 import org.apache.lucene.analysis.Analyzer; 40 import org.apache.lucene.queryParser.QueryParser; 41 import org.apache.lucene.document.Document; 42 43 import java.util.Collection ; 44 45 49 public class TextContainsExpression implements IBasicExpression { 50 51 protected static final String LOG_CHANNEL = TextContainsExpression.class.getName(); 52 53 String searchedText; 54 String indexPath; 55 Analyzer analyzer; 56 57 58 IBasicExpressionFactory factory; 59 60 65 TextContainsExpression (String searchedText, String rootPath, Analyzer analyzer) 66 { 67 this.searchedText = searchedText; 68 this.indexPath = rootPath; 69 this.analyzer = analyzer; 70 } 71 72 75 TextContainsExpression (String mergeOperator, 76 Collection children, 77 IBasicExpressionFactory factory) 78 throws BadQueryException 79 { 80 } 95 96 104 public IBasicResultSet execute() throws SearchException 105 { 106 IBasicResultSet result = new BasicResultSetImpl (false); 107 108 try 109 { 110 Searcher searcher = new IndexSearcher(indexPath); 111 112 Query query = QueryParser.parse(searchedText, TextContentIndexer.CONTENT_TEXT, analyzer); 113 Hits hits = searcher.search (query); 114 int noOfHits = hits.length(); 115 116 for (int i = 0; i < noOfHits; i++) 117 { 118 Document doc = hits.doc(i); 119 String uri = doc.get(TextContentIndexer.URI_FIELD); 120 121 IBasicQuery q = factory.getQuery(); 122 String scope = q.getSearchToken().getSlideContext().getSlidePath(q.getScope().getHref()); 123 if (uri.startsWith(scope)) { 124 RequestedResource resource = createResource(uri); 125 result.add (resource); 126 } 127 } 128 } 129 catch (Exception e) 130 { 131 throw new SearchException (e); 132 } 133 134 Domain.log( 135 "Executed Search for '" + searchedText + "' in the index", 136 LOG_CHANNEL, 137 Logger.INFO); 138 139 return result; 140 } 141 142 private RequestedResource createResource(String uri) throws SearchException 143 { 144 ObjectNode node = new SubjectNode(uri); RequestedResource resource = null; 146 IBasicQuery query = factory.getQuery(); 147 148 try 149 { 150 resource = new ComparableResourceImpl 151 (node, query.getSearchToken(), query.getScope(), 152 factory.getPropertyProvider()); 153 } 154 catch (SlideException e) 155 { 156 throw new SearchException (e); 157 } 158 return resource; 159 } 160 161 public void setFactory (IBasicExpressionFactory factory) 162 { 163 this.factory = factory; 164 } 165 166 public IBasicExpressionFactory getFactory() 167 { 168 return this.factory; 169 } 170 } 171 | Popular Tags |