1 5 package org.exoplatform.services.indexing; 6 7 import java.util.List ; 8 import org.apache.lucene.analysis.Analyzer; 9 import org.apache.lucene.index.Term; 10 import org.apache.lucene.search.*; 11 16 public class Searcher { 17 private org.apache.lucene.search.Searcher searcher_ ; 18 private Query modules_ ; 19 private Analyzer analyzer_ ; 20 private Hits lastSearchResult_ ; 21 22 public Searcher(IndexSearcher searcher, 23 Analyzer analyzer) throws Exception { 24 searcher_ = searcher; 25 analyzer_ = analyzer ; 26 } 27 28 public void setQueryModules(Query query) { 29 modules_ = query ; 30 } 31 32 public Hits getLastSearchResult() { return lastSearchResult_ ; } 33 34 public HitPageList search(SearchInput input) throws Exception { 35 BooleanQuery query = new BooleanQuery() ; 36 if(modules_ != null) { 37 query.add(modules_, true, false) ; 38 } 39 40 Query subQuery = input.getQuery(analyzer_) ; 41 query.add(subQuery, true, false) ; 42 lastSearchResult_ = searcher_.search(query) ; 43 lastSearchResult_ = searcher_.search(query) ; 44 return new HitPageList(this) ; 45 } 46 47 public HitPageList search(List inputs) throws Exception { 48 BooleanQuery query = new BooleanQuery() ; 49 if(modules_ != null) { 50 query.add(modules_, true, false) ; 51 } 52 for(int i = 0; i < inputs.size(); i++) { 53 SearchInput input = (SearchInput) inputs.get(i) ; 54 if(input.hasTerm()) { 55 Query subQuery = input.getQuery(analyzer_) ; 56 query.add(subQuery, true, false) ; 57 } 58 } 59 lastSearchResult_ = searcher_.search(query) ; 60 return new HitPageList(this) ; 61 } 62 63 public HitPageList search(SearchInput input, List modules) throws Exception { 64 BooleanQuery query = new BooleanQuery() ; 65 if(modules != null && modules.size() > 0) { 66 Query modulesQuery = createModuleQuery(modules); 67 query.add( modulesQuery, true, false) ; 68 } 69 70 Query subQuery = input.getQuery(analyzer_) ; 71 query.add(subQuery, true, false) ; 72 lastSearchResult_ = searcher_.search(query) ; 73 return new HitPageList(this) ; 74 } 75 76 77 public HitPageList search(List inputs, List modules) throws Exception { 78 BooleanQuery query = new BooleanQuery() ; 79 if(modules != null && modules.size() > 0) { 80 Query modulesQuery = createModuleQuery(modules); 81 query.add( modulesQuery, true, false) ; 82 } 83 84 for(int i = 0; i < inputs.size(); i++) { 85 SearchInput input = (SearchInput) inputs.get(i) ; 86 if(input.hasTerm()) { 87 Query subQuery = input.getQuery(analyzer_) ; 88 query.add(subQuery, true, false) ; 89 } 90 } 91 lastSearchResult_ = searcher_.search(query) ; 92 return new HitPageList(this) ; 93 } 94 95 private Query createModuleQuery(List modules) { 96 BooleanQuery modulesQuery = new BooleanQuery() ; 97 for(int i = 0 ; i < modules.size(); i++) { 98 String module = (String ) modules.get(i) ; 99 Query moduleQuery = new TermQuery(new Term(IndexingService.MODULE_FIELD, module)) ; 100 modulesQuery.add(moduleQuery, false, false) ; 101 } 102 return modulesQuery; 103 } 104 105 protected void finalize() { 106 try { 107 searcher_.close() ; 108 } catch (Exception ex) { 109 ex.printStackTrace() ; 110 } 111 } 112 } | Popular Tags |