1 2 3 4 package net.nutch.searcher; 5 6 import org.apache.lucene.search.Searcher; 7 import org.apache.lucene.search.BooleanQuery; 8 import org.apache.lucene.search.BooleanClause; 9 import org.apache.lucene.search.TermQuery; 10 import org.apache.lucene.search.QueryFilter; 11 import org.apache.lucene.search.Filter; 12 import org.apache.lucene.search.TopDocs; 13 14 import java.util.LinkedHashMap ; 15 import java.util.Map ; 16 import java.io.IOException ; 17 18 24 class LuceneQueryOptimizer { 25 private LinkedHashMap cache; 27 private float threshold; 28 29 34 public LuceneQueryOptimizer(final int cacheSize, float threshold) { 35 this.cache = new LinkedHashMap (cacheSize, 0.75f, true) { 36 protected boolean removeEldestEntry(Map.Entry eldest) { 37 return size() > cacheSize; } 39 }; 40 this.threshold = threshold; 41 } 42 43 public TopDocs optimize(BooleanQuery original, 44 Searcher searcher, int numHits) 45 throws IOException { 46 47 BooleanQuery query = new BooleanQuery(); 48 BooleanQuery filterQuery = null; 49 50 BooleanClause[] clauses = original.getClauses(); 51 for (int i = 0; i < clauses.length; i++) { 52 BooleanClause c = clauses[i]; 53 if (c.required && c.query.getBoost() == 0.0f && c.query instanceof TermQuery && (searcher.docFreq(((TermQuery)c.query).getTerm()) 57 / (float)searcher.maxDoc()) >= threshold) { if (filterQuery == null) 59 filterQuery = new BooleanQuery(); 60 filterQuery.add(c.query, true, false); } else { 62 query.add(c); } 64 } 65 66 Filter filter = null; 67 if (filterQuery != null) { 68 synchronized (cache) { filter = (Filter)cache.get(filterQuery); 70 } 71 if (filter == null) { filter = new QueryFilter(filterQuery); synchronized (cache) { 74 cache.put(filterQuery, filter); } 76 } 77 } 78 79 return searcher.search(query, filter, numHits); 80 } 81 } 82 | Popular Tags |