lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From markrmil...@apache.org
Subject svn commit: r770993 - in /lucene/java/trunk/contrib/highlighter/src: java/org/apache/lucene/search/highlight/WeightedSpanTermExtractor.java test/org/apache/lucene/search/highlight/HighlighterTest.java
Date Sat, 02 May 2009 23:36:53 GMT
Author: markrmiller
Date: Sat May  2 22:46:26 2009
New Revision: 770993

URL: http://svn.apache.org/viewvc?rev=770993&view=rev
Log:
fix for LUCENE-1425

Modified:
    lucene/java/trunk/contrib/highlighter/src/java/org/apache/lucene/search/highlight/WeightedSpanTermExtractor.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/WeightedSpanTermExtractor.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/contrib/highlighter/src/java/org/apache/lucene/search/highlight/WeightedSpanTermExtractor.java?rev=770993&r1=770992&r2=770993&view=diff
==============================================================================
--- lucene/java/trunk/contrib/highlighter/src/java/org/apache/lucene/search/highlight/WeightedSpanTermExtractor.java
(original)
+++ lucene/java/trunk/contrib/highlighter/src/java/org/apache/lucene/search/highlight/WeightedSpanTermExtractor.java
Sat May  2 22:46:26 2009
@@ -140,10 +140,17 @@
     } else if (query instanceof MultiTermQuery && (highlightCnstScrRngQuery || expandMultiTermQuery))
{
       MultiTermQuery mtq = ((MultiTermQuery)query);
       if(mtq.getConstantScoreRewrite()) {
-        query = copyMultiTermQuery(mtq);
+        mtq = copyMultiTermQuery(mtq);
         mtq.setConstantScoreRewrite(false);
+        query = mtq;
       }
-      IndexReader ir = getReaderForField(fieldName);
+      String field;
+      if(mtq instanceof RangeQuery) {
+        field = ((RangeQuery)mtq).getField();
+      } else {
+        field = mtq.getTerm().field();
+      }
+      IndexReader ir = getReaderForField(field);
       extract(query.rewrite(ir), terms);
     } else if (query instanceof MultiPhraseQuery) {
       final MultiPhraseQuery mpq = (MultiPhraseQuery) query;
@@ -464,17 +471,17 @@
     
   }
   
-  private Query copyMultiTermQuery(MultiTermQuery query) {
+  private MultiTermQuery copyMultiTermQuery(MultiTermQuery query) {
     if(query instanceof RangeQuery) {
       RangeQuery q = (RangeQuery)query;
       q.setBoost(query.getBoost());
       return new RangeQuery(q.getField(), q.getLowerTermText(), q.getUpperTermText(), q.includesLower(),
q.includesUpper());
     } else if(query instanceof WildcardQuery) {
-      Query q = new WildcardQuery(query.getTerm());
+      MultiTermQuery q = new WildcardQuery(query.getTerm());
       q.setBoost(query.getBoost());
       return q;
     } else if(query instanceof PrefixQuery) {
-      Query q = new PrefixQuery(query.getTerm());
+      MultiTermQuery q = new PrefixQuery(query.getTerm());
       q.setBoost(q.getBoost());
       return q;
     } else if(query instanceof FuzzyQuery) {

Modified: lucene/java/trunk/contrib/highlighter/src/test/org/apache/lucene/search/highlight/HighlighterTest.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/contrib/highlighter/src/test/org/apache/lucene/search/highlight/HighlighterTest.java?rev=770993&r1=770992&r2=770993&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
Sat May  2 22:46:26 2009
@@ -578,6 +578,68 @@
     }
     assertTrue("Failed to find correct number of highlights " + numHighlights + " found",
         numHighlights == 5);
+    
+    // try null field
+    
+    hits = searcher.search(query);
+    
+    numHighlights = 0;
+
+    for (int i = 0; i < hits.length(); i++) {
+      String text = hits.doc(i).get(HighlighterTest.FIELD_NAME);
+      int maxNumFragmentsRequired = 2;
+      String fragmentSeparator = "...";
+      SpanScorer scorer = null;
+      TokenStream tokenStream = null;
+
+      tokenStream = new CachingTokenFilter(analyzer.tokenStream(HighlighterTest.FIELD_NAME,
+          new StringReader(text)));
+      
+      scorer = new SpanScorer(query, null, (CachingTokenFilter) tokenStream, true);
+
+      Highlighter highlighter = new Highlighter(this, scorer);
+
+      ((CachingTokenFilter) tokenStream).reset();
+
+      highlighter.setTextFragmenter(new SimpleFragmenter(20));
+
+      String result = highlighter.getBestFragments(tokenStream, text, maxNumFragmentsRequired,
+          fragmentSeparator);
+      System.out.println("\t" + result);
+    }
+    assertTrue("Failed to find correct number of highlights " + numHighlights + " found",
+        numHighlights == 5);
+    
+    // try default field
+    
+    hits = searcher.search(query);
+    
+    numHighlights = 0;
+
+    for (int i = 0; i < hits.length(); i++) {
+      String text = hits.doc(i).get(HighlighterTest.FIELD_NAME);
+      int maxNumFragmentsRequired = 2;
+      String fragmentSeparator = "...";
+      SpanScorer scorer = null;
+      TokenStream tokenStream = null;
+
+      tokenStream = new CachingTokenFilter(analyzer.tokenStream(HighlighterTest.FIELD_NAME,
+          new StringReader(text)));
+      
+      scorer = new SpanScorer(query, "random_field", (CachingTokenFilter) tokenStream, HighlighterTest.FIELD_NAME,
true);
+
+      Highlighter highlighter = new Highlighter(this, scorer);
+
+      ((CachingTokenFilter) tokenStream).reset();
+
+      highlighter.setTextFragmenter(new SimpleFragmenter(20));
+
+      String result = highlighter.getBestFragments(tokenStream, text, maxNumFragmentsRequired,
+          fragmentSeparator);
+      System.out.println("\t" + result);
+    }
+    assertTrue("Failed to find correct number of highlights " + numHighlights + " found",
+        numHighlights == 5);
   }
 
   public void testGetBestFragmentsPhrase() throws Exception {



Mime
View raw message