1 5 package org.roller.business.search.operations; 6 7 import java.io.IOException ; 8 9 import org.apache.commons.logging.Log; 10 import org.apache.commons.logging.LogFactory; 11 import org.apache.lucene.analysis.standard.StandardAnalyzer; 12 import org.apache.lucene.index.IndexReader; 13 import org.apache.lucene.index.Term; 14 import org.apache.lucene.queryParser.MultiFieldQueryParser; 15 import org.apache.lucene.queryParser.ParseException; 16 import org.apache.lucene.search.BooleanQuery; 17 import org.apache.lucene.search.Hits; 18 import org.apache.lucene.search.IndexSearcher; 19 import org.apache.lucene.search.Query; 20 import org.apache.lucene.search.Sort; 21 import org.apache.lucene.search.SortField; 22 import org.apache.lucene.search.TermQuery; 23 import org.roller.business.IndexManagerImpl; 24 import org.roller.business.search.FieldConstants; 25 import org.roller.business.search.IndexUtil; 26 import org.roller.model.IndexManager; 27 28 29 34 public class SearchOperation extends ReadFromIndexOperation 35 { 36 38 private static Log mLogger = 39 LogFactory.getFactory().getInstance(SearchOperation.class); 40 41 private static String [] SEARCH_FIELDS = new String []{ 42 FieldConstants.CONTENT, FieldConstants.TITLE, 43 FieldConstants.C_CONTENT, FieldConstants.CATEGORY 44 }; 45 46 private static Sort SORTER = new Sort( new SortField( 47 FieldConstants.PUBLISHED, SortField.STRING, true) ); 48 49 51 private String term; 52 private String username; 53 private String category; 54 private Hits searchresults; 55 private String parseError; 56 57 59 62 public SearchOperation(IndexManager mgr) 63 { 64 super((IndexManagerImpl)mgr); 66 } 67 68 70 public void setTerm(String term) 71 { 72 this.term = term; 73 } 74 75 78 public void doRun() 79 { 80 searchresults = null; 81 82 IndexSearcher searcher = null; 83 84 try 85 { 86 IndexReader reader = manager.getSharedIndexReader(); 87 searcher = new IndexSearcher(reader); 88 89 Query query = 90 MultiFieldQueryParser.parse( 91 term, SEARCH_FIELDS, new StandardAnalyzer()); 92 93 Term tUsername = 94 IndexUtil.getTerm(FieldConstants.USERNAME, username); 95 96 if (tUsername != null) 97 { 98 BooleanQuery bQuery = new BooleanQuery(); 99 bQuery.add(query, true, false); 100 bQuery.add(new TermQuery(tUsername), true, false); 101 query = bQuery; 102 } 103 104 Term tCategory = 105 IndexUtil.getTerm(FieldConstants.CATEGORY, category); 106 107 if (tCategory != null) 108 { 109 BooleanQuery bQuery = new BooleanQuery(); 110 bQuery.add(query, true, false); 111 bQuery.add(new TermQuery(tCategory), true, false); 112 query = bQuery; 113 } 114 searchresults = searcher.search(query, null, SORTER); 115 } 116 catch (IOException e) 117 { 118 mLogger.error("Error searching index", e); 119 parseError = e.getMessage(); 120 } 121 catch (ParseException e) 122 { 123 parseError = e.getMessage(); 125 } 126 } 128 129 public Hits getResults() 130 { 131 return searchresults; 132 } 133 134 public int getResultsCount() 135 { 136 if (searchresults == null) return -1; 137 138 return searchresults.length(); 139 } 140 141 public String getParseError() 142 { 143 return parseError; 144 } 145 146 149 public void setUsername(String username) 150 { 151 this.username = username; 152 } 153 154 157 public void setCategory(String category) 158 { 159 this.category = category; 160 } 161 162 } 163 | Popular Tags |