lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mharw...@apache.org
Subject svn commit: r409994 - /lucene/java/trunk/contrib/highlighter/src/java/org/apache/lucene/search/highlight/QueryTermExtractor.java
Date Sun, 28 May 2006 20:24:43 GMT
Author: mharwood
Date: Sun May 28 13:24:42 2006
New Revision: 409994

URL: http://svn.apache.org/viewvc?rev=409994&view=rev
Log: (empty)

Modified:
    lucene/java/trunk/contrib/highlighter/src/java/org/apache/lucene/search/highlight/QueryTermExtractor.java

Modified: lucene/java/trunk/contrib/highlighter/src/java/org/apache/lucene/search/highlight/QueryTermExtractor.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/contrib/highlighter/src/java/org/apache/lucene/search/highlight/QueryTermExtractor.java?rev=409994&r1=409993&r2=409994&view=diff
==============================================================================
--- lucene/java/trunk/contrib/highlighter/src/java/org/apache/lucene/search/highlight/QueryTermExtractor.java
(original)
+++ lucene/java/trunk/contrib/highlighter/src/java/org/apache/lucene/search/highlight/QueryTermExtractor.java
Sun May 28 13:24:42 2006
@@ -21,6 +21,9 @@
 
 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 @@
 	{
        	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);		
+	}
+	
 }



Mime
View raw message