Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@409994 13f79535-47bb-0310-9956-ffa450edef68

Mark Harwood1ef156da342d
diff --git a/contrib/highlighter/src/java/org/apache/lucene/search/highlight/QueryTermExtractor.java b/contrib/highlighter/src/java/org/apache/lucene/search/highlight/QueryTermExtractor.java
index 82b59c1848e..9b7e3ed172c 100644
--- a/contrib/highlighter/src/java/org/apache/lucene/search/highlight/QueryTermExtractor.java
+++ b/contrib/highlighter/src/java/org/apache/lucene/search/highlight/QueryTermExtractor.java
@@ -21,6 +21,9 @@ import java.util.Iterator;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
+import org.apache.lucene.search.BooleanClause;
+import org.apache.lucene.search.BooleanQuery;
+import org.apache.lucene.search.FilteredQuery;
import org.apache.lucene.search.Query;
/**
@@ -110,20 +113,54 @@ public final class QueryTermExtractor
{
try
{
- HashSet nonWeightedTerms=new HashSet();
- query.extractTerms(nonWeightedTerms);
- for (Iterator iter = nonWeightedTerms.iterator(); iter.hasNext();)
- {
- Term term = (Term) iter.next();
- if((fieldName==null)||(term.field()==fieldName))
+ if (query instanceof BooleanQuery)
+ getTermsFromBooleanQuery((BooleanQuery) query, terms, prohibited, fieldName);
+ else
+ if(query instanceof FilteredQuery)
+ getTermsFromFilteredQuery((FilteredQuery)query, terms,prohibited, fieldName);
+ else
+ {
+ HashSet nonWeightedTerms=new HashSet();
+ query.extractTerms(nonWeightedTerms);
+ for (Iterator iter = nonWeightedTerms.iterator(); iter.hasNext();)
{
- terms.add(new WeightedTerm(query.getBoost(),term.text()));
+ Term term = (Term) iter.next();
+ if((fieldName==null)||(term.field()==fieldName))
+ {
+ terms.add(new WeightedTerm(query.getBoost(),term.text()));
+ }
}
- }
+ }
}
catch(UnsupportedOperationException ignore)
{
//this is non-fatal for our purposes
}
}
+
+ /**
+ * extractTerms is currently the only query-independent means of introspecting queries but it only reveals
+ * a list of terms for that query - not the boosts each individual term in that query may or may not have.
+ * "Container" queries such as BooleanQuery should be unwrapped to get at the boost info held
+ * in each child element.
+ * Some discussion around this topic here:
+ * http://www.gossamer-threads.com/lists/lucene/java-dev/34208?search_string=introspection;#34208
+ * Unfortunately there seemed to be limited interest in requiring all Query objects to implement
+ * something common which would allow access to child queries so what follows here are query-specific
+ * implementations for accessing embedded query elements.
+ */
+ private static final void getTermsFromBooleanQuery(BooleanQuery query, HashSet terms, boolean prohibited, String fieldName)
+ {
+ BooleanClause[] queryClauses = query.getClauses();
+ for (int i = 0; i < queryClauses.length; i++)
+ {
+ if (prohibited || queryClauses[i].getOccur()!=BooleanClause.Occur.MUST_NOT)
+ getTerms(queryClauses[i].getQuery(), terms, prohibited, fieldName);
+ }
+ }
+ private static void getTermsFromFilteredQuery(FilteredQuery query, HashSet terms, boolean prohibited, String fieldName)
+ {
+ getTerms(query.getQuery(),terms,prohibited,fieldName);
+ }
+
}