lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mharw...@apache.org
Subject svn commit: r351504 - in /lucene/java/trunk/contrib/highlighter/src: java/org/apache/lucene/search/highlight/QueryScorer.java java/org/apache/lucene/search/highlight/QueryTermExtractor.java test/org/apache/lucene/search/highlight/HighlighterTest.java
Date Thu, 01 Dec 2005 22:18:42 GMT
Author: mharwood
Date: Thu Dec  1 14:18:33 2005
New Revision: 351504

URL: http://svn.apache.org/viewcvs?rev=351504&view=rev
Log:
Added support for field-specific highlighting which respects the fieldnames found in queries.
Pass a field name to the QueryScorer in order to only select those field's query terms for
highlighting. Updated JUnit tests too.

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

Modified: lucene/java/trunk/contrib/highlighter/src/java/org/apache/lucene/search/highlight/QueryScorer.java
URL: http://svn.apache.org/viewcvs/lucene/java/trunk/contrib/highlighter/src/java/org/apache/lucene/search/highlight/QueryScorer.java?rev=351504&r1=351503&r2=351504&view=diff
==============================================================================
--- lucene/java/trunk/contrib/highlighter/src/java/org/apache/lucene/search/highlight/QueryScorer.java
(original)
+++ lucene/java/trunk/contrib/highlighter/src/java/org/apache/lucene/search/highlight/QueryScorer.java
Thu Dec  1 14:18:33 2005
@@ -48,6 +48,17 @@
 	{
 		this(QueryTermExtractor.getTerms(query));
 	}
+	
+	/**
+	 * 
+	 * @param query a Lucene query (ideally rewritten using query.rewrite 
+	 * before being passed to this class and the searcher)
+	 * @param fieldName the Field name which is used to match Query terms
+	 */
+	public QueryScorer(Query query, String fieldName)
+	{
+		this(QueryTermExtractor.getTerms(query, false,fieldName));
+	}	
 
 	/**
 	 * 

Modified: lucene/java/trunk/contrib/highlighter/src/java/org/apache/lucene/search/highlight/QueryTermExtractor.java
URL: http://svn.apache.org/viewcvs/lucene/java/trunk/contrib/highlighter/src/java/org/apache/lucene/search/highlight/QueryTermExtractor.java?rev=351504&r1=351503&r2=351504&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
Thu Dec  1 14:18:33 2005
@@ -61,7 +61,7 @@
 	 */
 	public static final WeightedTerm[] getIdfWeightedTerms(Query query, IndexReader reader,
String fieldName) 
 	{
-	    WeightedTerm[] terms=getTerms(query,false);
+	    WeightedTerm[] terms=getTerms(query,false, fieldName);
 	    int totalNumDocs=reader.numDocs();
 	    for (int i = 0; i < terms.length; i++)
         {
@@ -85,31 +85,49 @@
 	 *
 	 * @param query      Query to extract term texts from
 	 * @param prohibited <code>true</code> to extract "prohibited" terms, too
+	 * @param fieldName  The fieldName used to filter query terms
    * @return an array of the terms used in a query, plus their weights.
    */
-	public static final WeightedTerm[] getTerms(Query query, boolean prohibited) 
+	public static final WeightedTerm[] getTerms(Query query, boolean prohibited, String fieldName)

 	{
 		HashSet terms=new HashSet();
-		getTerms(query,terms,prohibited);
+		if(fieldName!=null)
+		{
+		    fieldName=fieldName.intern();
+		}
+		getTerms(query,terms,prohibited,fieldName);
 		return (WeightedTerm[]) terms.toArray(new WeightedTerm[0]);
 	}
+	
+	/**
+	 * Extracts all terms texts of a given Query into an array of WeightedTerms
+	 *
+	 * @param query      Query to extract term texts from
+	 * @param prohibited <code>true</code> to extract "prohibited" terms, too
+   * @return an array of the terms used in a query, plus their weights.
+   */
+	public static final WeightedTerm[] getTerms(Query query, boolean prohibited) 
+	{
+	    return getTerms(query,prohibited,null);
+	}	
 
-	private static final void getTerms(Query query, HashSet terms,boolean prohibited) 
+	//fieldname MUST be interned prior to this call
+	private static final void getTerms(Query query, HashSet terms,boolean prohibited, String
fieldName) 
 	{
 		if (query instanceof BooleanQuery)
-			getTermsFromBooleanQuery((BooleanQuery) query, terms, prohibited);
+			getTermsFromBooleanQuery((BooleanQuery) query, terms, prohibited, fieldName);
 		else
 			if (query instanceof PhraseQuery)
-				getTermsFromPhraseQuery((PhraseQuery) query, terms);
+				getTermsFromPhraseQuery((PhraseQuery) query, terms, fieldName);
 			else
 				if (query instanceof TermQuery)
-					getTermsFromTermQuery((TermQuery) query, terms);
+					getTermsFromTermQuery((TermQuery) query, terms, fieldName);
 				else
 		        if(query instanceof SpanNearQuery)
-		            getTermsFromSpanNearQuery((SpanNearQuery) query, terms);
+		            getTermsFromSpanNearQuery((SpanNearQuery) query, terms, fieldName);
 	}
 
-	private static final void getTermsFromBooleanQuery(BooleanQuery query, HashSet terms, boolean
prohibited)
+	private static final void getTermsFromBooleanQuery(BooleanQuery query, HashSet terms, boolean
prohibited, String fieldName)
 	{
 		BooleanClause[] queryClauses = query.getClauses();
 		int i;
@@ -117,27 +135,33 @@
 		for (i = 0; i < queryClauses.length; i++)
 		{
 			if (prohibited || !queryClauses[i].prohibited)
-				getTerms(queryClauses[i].query, terms, prohibited);
+				getTerms(queryClauses[i].query, terms, prohibited, fieldName);
 		}
 	}
 
-	private static final void getTermsFromPhraseQuery(PhraseQuery query, HashSet terms)
+	private static final void getTermsFromPhraseQuery(PhraseQuery query, HashSet terms, String
fieldName)
 	{
 		Term[] queryTerms = query.getTerms();
 		int i;
 
 		for (i = 0; i < queryTerms.length; i++)
 		{
-			terms.add(new WeightedTerm(query.getBoost(),queryTerms[i].text()));
+		    if((fieldName==null)||(queryTerms[i].field()==fieldName))
+		    {
+		        terms.add(new WeightedTerm(query.getBoost(),queryTerms[i].text()));
+		    }
 		}
 	}
 
-	private static final void getTermsFromTermQuery(TermQuery query, HashSet terms)
+	private static final void getTermsFromTermQuery(TermQuery query, HashSet terms, String fieldName)
 	{
-		terms.add(new WeightedTerm(query.getBoost(),query.getTerm().text()));
+	    if((fieldName==null)||(query.getTerm().field()==fieldName))
+	    {
+	        terms.add(new WeightedTerm(query.getBoost(),query.getTerm().text()));
+	    }
 	}
 
-    private static final void getTermsFromSpanNearQuery(SpanNearQuery query, HashSet terms){
+    private static final void getTermsFromSpanNearQuery(SpanNearQuery query, HashSet terms,
String fieldName){
 
         Collection queryTerms = query.getTerms();
 
@@ -149,10 +173,10 @@
 
             String text = term.text();
 
-            terms.add(new WeightedTerm(query.getBoost(), text));
-
- 
-
+    	    if((fieldName==null)||(term.field()==fieldName))
+    	    {
+    	        terms.add(new WeightedTerm(query.getBoost(), text));
+    	    }
         }
 
     }

Modified: lucene/java/trunk/contrib/highlighter/src/test/org/apache/lucene/search/highlight/HighlighterTest.java
URL: http://svn.apache.org/viewcvs/lucene/java/trunk/contrib/highlighter/src/test/org/apache/lucene/search/highlight/HighlighterTest.java?rev=351504&r1=351503&r2=351504&view=diff
==============================================================================
--- lucene/java/trunk/contrib/highlighter/src/test/org/apache/lucene/search/highlight/HighlighterTest.java
(original)
+++ lucene/java/trunk/contrib/highlighter/src/test/org/apache/lucene/search/highlight/HighlighterTest.java
Thu Dec  1 14:18:33 2005
@@ -455,9 +455,36 @@
 		}
 		assertTrue("Failed to find correct number of highlights " + numHighlights + " found", numHighlights
== 2);
 
-
-
 	}
+	
+	public void testFieldSpecificHighlighting() throws IOException, ParseException
+	{
+		String docMainText="fred is one of the people";
+		QueryParser parser=new QueryParser(FIELD_NAME,analyzer);
+		Query query=parser.parse("fred category:people");
+		
+		//highlighting respects fieldnames used in query
+		QueryScorer fieldSpecificScorer=new QueryScorer(query, "contents");
+		Highlighter fieldSpecificHighlighter =
+			new Highlighter(new SimpleHTMLFormatter(),fieldSpecificScorer);
+		fieldSpecificHighlighter.setTextFragmenter(new NullFragmenter());
+		String result=fieldSpecificHighlighter.getBestFragment(analyzer,FIELD_NAME,docMainText);
+		assertEquals("Should match",result,"<B>fred</B> is one of the people");
+		
+		//highlighting does not respect fieldnames used in query
+		QueryScorer fieldInSpecificScorer=new QueryScorer(query);
+		Highlighter fieldInSpecificHighlighter =
+			new Highlighter(new SimpleHTMLFormatter(),fieldInSpecificScorer);
+		fieldInSpecificHighlighter.setTextFragmenter(new NullFragmenter());
+		result=fieldInSpecificHighlighter.getBestFragment(analyzer,FIELD_NAME,docMainText);
+		assertEquals("Should match",result,"<B>fred</B> is one of the <B>people</B>");
+		
+		
+		reader.close();
+		
+	}
+	
+	
 
 /*
 



Mime
View raw message