1 23 24 25 26 package org.apache.slide.index; 27 28 import org.apache.slide.search.basic.*; 29 30 import java.util.Collection ; 31 import org.apache.lucene.analysis.Analyzer; 32 import org.apache.lucene.analysis.standard.StandardAnalyzer; 33 import org.apache.lucene.document.Document; 34 import org.apache.lucene.queryParser.QueryParser; 35 import org.apache.lucene.search.Hits; 36 import org.apache.lucene.search.IndexSearcher; 37 import org.apache.lucene.search.Query; 38 import org.apache.lucene.search.Searcher; 39 import org.apache.slide.common.SlideException; 40 import org.apache.slide.search.BadQueryException; 41 import org.apache.slide.search.RequestedResource; 42 import org.apache.slide.search.SearchException; 43 import org.apache.slide.structure.ObjectNode; 44 import org.apache.slide.structure.SubjectNode; 45 46 54 public class BasicExpressionTxtContainsSample implements IBasicExpression 55 { 56 57 String searchedText; 58 59 String indexPath; 60 61 62 IBasicExpressionFactory factory; 63 64 69 BasicExpressionTxtContainsSample (String searchedText, String rootPath) 70 { 71 this.searchedText = searchedText; 72 this.indexPath = rootPath; 73 } 74 75 78 BasicExpressionTxtContainsSample (String mergeOperator, 79 Collection children, 80 IBasicExpressionFactory factory) 81 throws BadQueryException 82 { 83 } 98 99 107 public IBasicResultSet execute() throws SearchException 108 { 109 IBasicResultSet result = new BasicResultSetImpl (false); 110 111 try 112 { 113 Searcher searcher = new IndexSearcher(indexPath); 114 Analyzer analyzer = new StandardAnalyzer(); 115 116 Query query = QueryParser.parse(searchedText, "contents", analyzer); 117 Hits hits = searcher.search (query); 118 int noOfHits = hits.length(); 119 120 for (int i = 0; i < noOfHits; i++) 121 { 122 Document doc = hits.doc(i); 123 String uri = doc.get("documentId"); 124 System.out.println(uri); 125 RequestedResource resource = createResource(uri); 126 result.add (resource); 127 } 128 } 129 catch (Exception e) 130 { 131 throw new SearchException (e); 132 } 133 134 return result; 135 } 136 137 private RequestedResource createResource(String uri) throws SearchException 138 { 139 ObjectNode node = new SubjectNode(uri); RequestedResource resource = null; 141 IBasicQuery query = factory.getQuery(); 142 143 try 144 { 145 resource = new ComparableResourceImpl 146 (node, query.getSearchToken(), query.getScope(), 147 factory.getPropertyProvider()); 148 } 149 catch (SlideException e) 150 { 151 throw new SearchException (e); 152 } 153 return resource; 154 } 155 156 public void setFactory (IBasicExpressionFactory factory) 157 { 158 this.factory = factory; 159 } 160 161 public IBasicExpressionFactory getFactory() 162 { 163 return this.factory; 164 } 165 } 166 167 | Popular Tags |