1 package org.apache.lucene.search.highlight; 2 17 18 import java.io.IOException ; 19 import java.util.HashSet ; 20 21 import org.apache.lucene.index.IndexReader; 22 import org.apache.lucene.index.Term; 23 import org.apache.lucene.search.BooleanClause; 24 import org.apache.lucene.search.BooleanQuery; 25 import org.apache.lucene.search.PhraseQuery; 26 import org.apache.lucene.search.Query; 27 import org.apache.lucene.search.TermQuery; 28 29 36 public final class QueryTermExtractor 37 { 38 39 45 public static final WeightedTerm[] getTerms(Query query) 46 { 47 return getTerms(query,false); 48 } 49 50 59 public static final WeightedTerm[] getIdfWeightedTerms(Query query, IndexReader reader, String fieldName) 60 { 61 WeightedTerm[] terms=getTerms(query,false); 62 int totalNumDocs=reader.numDocs(); 63 for (int i = 0; i < terms.length; i++) 64 { 65 try 66 { 67 int docFreq=reader.docFreq(new Term(fieldName,terms[i].term)); 68 float idf=(float)(Math.log((float)totalNumDocs/(double)(docFreq+1)) + 1.0); 70 terms[i].weight*=idf; 71 } 72 catch (IOException e) 73 { 74 } 76 } 77 return terms; 78 } 79 80 87 public static final WeightedTerm[] getTerms(Query query, boolean prohibited) 88 { 89 HashSet <WeightedTerm> terms=new HashSet <WeightedTerm>(); 90 getTerms(query,terms,prohibited); 91 return (WeightedTerm[]) terms.toArray(new WeightedTerm[0]); 92 } 93 94 private static final void getTerms(Query query, HashSet <WeightedTerm> terms,boolean prohibited) 95 { 96 if (query instanceof BooleanQuery) 97 getTermsFromBooleanQuery((BooleanQuery) query, terms, prohibited); 98 else 99 if (query instanceof PhraseQuery) 100 getTermsFromPhraseQuery((PhraseQuery) query, terms); 101 else 102 if (query instanceof TermQuery) 103 getTermsFromTermQuery((TermQuery) query, terms); 104 } 114 115 private static final void getTermsFromBooleanQuery(BooleanQuery query, HashSet <WeightedTerm> terms, boolean prohibited) 116 { 117 BooleanClause[] queryClauses = query.getClauses(); 118 int i; 119 120 for (i = 0; i < queryClauses.length; i++) 121 { 122 if (prohibited || !queryClauses[i].prohibited) 123 getTerms(queryClauses[i].query, terms, prohibited); 124 } 125 } 126 127 private static final void getTermsFromPhraseQuery(PhraseQuery query, HashSet <WeightedTerm> terms) 128 { 129 Term[] queryTerms = query.getTerms(); 130 int i; 131 132 for (i = 0; i < queryTerms.length; i++) 133 { 134 terms.add(new WeightedTerm(query.getBoost(),queryTerms[i].text())); 135 } 136 } 137 138 private static final void getTermsFromTermQuery(TermQuery query, HashSet <WeightedTerm> terms) 139 { 140 terms.add(new WeightedTerm(query.getBoost(),query.getTerm().text())); 141 } 142 143 144 } 145 | Popular Tags |