You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by rm...@apache.org on 2013/01/03 05:01:17 UTC

svn commit: r1428162 - in /lucene/dev/branches/branch_4x: ./ lucene/ lucene/sandbox/ lucene/sandbox/src/java/org/apache/lucene/sandbox/postingshighlight/ lucene/sandbox/src/test/org/apache/lucene/sandbox/postingshighlight/

Author: rmuir
Date: Thu Jan  3 04:01:16 2013
New Revision: 1428162

URL: http://svn.apache.org/viewvc?rev=1428162&view=rev
Log:
LUCENE-4290: also detect attempts to highlight fields w/o any prox

Modified:
    lucene/dev/branches/branch_4x/   (props changed)
    lucene/dev/branches/branch_4x/lucene/   (props changed)
    lucene/dev/branches/branch_4x/lucene/sandbox/   (props changed)
    lucene/dev/branches/branch_4x/lucene/sandbox/src/java/org/apache/lucene/sandbox/postingshighlight/PostingsHighlighter.java
    lucene/dev/branches/branch_4x/lucene/sandbox/src/test/org/apache/lucene/sandbox/postingshighlight/TestPostingsHighlighter.java

Modified: lucene/dev/branches/branch_4x/lucene/sandbox/src/java/org/apache/lucene/sandbox/postingshighlight/PostingsHighlighter.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/lucene/sandbox/src/java/org/apache/lucene/sandbox/postingshighlight/PostingsHighlighter.java?rev=1428162&r1=1428161&r2=1428162&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/lucene/sandbox/src/java/org/apache/lucene/sandbox/postingshighlight/PostingsHighlighter.java (original)
+++ lucene/dev/branches/branch_4x/lucene/sandbox/src/java/org/apache/lucene/sandbox/postingshighlight/PostingsHighlighter.java Thu Jan  3 04:01:16 2013
@@ -337,11 +337,10 @@ public final class PostingsHighlighter {
         if (!termsEnum.seekExact(terms[i].bytes(), true)) {
           continue; // term not found
         }
-        DocsAndPositionsEnum de2 = termsEnum.docsAndPositions(null, null, DocsAndPositionsEnum.FLAG_OFFSETS);
-        if (de2 == null) {
-          continue;
-        } else {
-          de = postings[i] = de2;
+        de = postings[i] = termsEnum.docsAndPositions(null, null, DocsAndPositionsEnum.FLAG_OFFSETS);
+        if (de == null) {
+          // no positions available
+          throw new IllegalArgumentException("field '" + field + "' was indexed without offsets, cannot highlight");
         }
         pDoc = de.advance(doc);
       } else {

Modified: lucene/dev/branches/branch_4x/lucene/sandbox/src/test/org/apache/lucene/sandbox/postingshighlight/TestPostingsHighlighter.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/lucene/sandbox/src/test/org/apache/lucene/sandbox/postingshighlight/TestPostingsHighlighter.java?rev=1428162&r1=1428161&r2=1428162&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/lucene/sandbox/src/test/org/apache/lucene/sandbox/postingshighlight/TestPostingsHighlighter.java (original)
+++ lucene/dev/branches/branch_4x/lucene/sandbox/src/test/org/apache/lucene/sandbox/postingshighlight/TestPostingsHighlighter.java Thu Jan  3 04:01:16 2013
@@ -24,6 +24,7 @@ import org.apache.lucene.analysis.MockTo
 import org.apache.lucene.document.Document;
 import org.apache.lucene.document.Field;
 import org.apache.lucene.document.FieldType;
+import org.apache.lucene.document.StringField;
 import org.apache.lucene.document.TextField;
 import org.apache.lucene.index.FieldInfo.IndexOptions;
 import org.apache.lucene.index.IndexReader;
@@ -232,15 +233,19 @@ public class TestPostingsHighlighter ext
     iwc.setMergePolicy(newLogMergePolicy());
     RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwc);
     
-    FieldType offsetsType = new FieldType(TextField.TYPE_STORED);
-    offsetsType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);
-    Field body = new Field("body", "", offsetsType);
+    FieldType positionsType = new FieldType(TextField.TYPE_STORED);
+    positionsType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);
+    Field body = new Field("body", "", positionsType);
+    Field title = new StringField("title", "", Field.Store.YES);
     Document doc = new Document();
     doc.add(body);
+    doc.add(title);
     
     body.setStringValue("This is a test. Just a test highlighting from postings. Feel free to ignore.");
+    title.setStringValue("test");
     iw.addDocument(doc);
     body.setStringValue("This test is another test. Not a good sentence. Test test test test.");
+    title.setStringValue("test");
     iw.addDocument(doc);
     
     IndexReader ir = iw.getReader();
@@ -257,6 +262,13 @@ public class TestPostingsHighlighter ext
     } catch (IllegalArgumentException iae) {
       // expected
     }
+    
+    try {
+      highlighter.highlight("title", new TermQuery(new Term("title", "test")), searcher, topDocs, 2);
+      fail("did not hit expected exception");
+    } catch (IllegalArgumentException iae) {
+      // expected
+    }
     ir.close();
     dir.close();
   }