You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by mi...@apache.org on 2012/12/11 17:54:42 UTC

svn commit: r1420279 - 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: mikemccand
Date: Tue Dec 11 16:54:40 2012
New Revision: 1420279

URL: http://svn.apache.org/viewvc?rev=1420279&view=rev
Log:
LUCENE-4290: be sure to throw exc if app didn't index offsets

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=1420279&r1=1420278&r2=1420279&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 Tue Dec 11 16:54:40 2012
@@ -104,7 +104,7 @@ public final class PostingsHighlighter {
   }
   
   public PostingsHighlighter(String field, int maxLength) {
-    this(field, DEFAULT_MAX_LENGTH, BreakIterator.getSentenceInstance(Locale.ROOT), new PassageScorer(), new PassageFormatter());
+    this(field, maxLength, BreakIterator.getSentenceInstance(Locale.ROOT), new PassageScorer(), new PassageFormatter());
   }
   
   public PostingsHighlighter(String field, int maxLength, BreakIterator breakIterator, PassageScorer scorer, PassageFormatter formatter) {
@@ -201,7 +201,7 @@ public final class PostingsHighlighter {
       if (leaf != lastLeaf) {
         termsEnum = t.iterator(null);
         postings = new DocsAndPositionsEnum[terms.size()];
-      };
+      }
       Passage passages[] = highlightDoc(termTexts, termContexts, subContext.ord, weights, content.length(), bi, doc - subContext.docBase, termsEnum, postings, maxPassages);
       if (passages.length > 0) {
         // otherwise a null snippet
@@ -354,7 +354,7 @@ public final class PostingsHighlighter {
         if (off == otherOff) {
           return id - other.id;
         } else {
-          return off - otherOff;
+          return Long.signum(((long)off) - otherOff);
         }
       } catch (IOException e) {
         throw new RuntimeException(e);

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=1420279&r1=1420278&r2=1420279&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 Tue Dec 11 16:54:40 2012
@@ -146,4 +146,39 @@ public class TestPostingsHighlighter ext
     ir.close();
     dir.close();
   }
+
+  public void testUserFailedToIndexOffsets() throws Exception {
+    Directory dir = newDirectory();
+    IndexWriterConfig iwc = newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random(), MockTokenizer.SIMPLE, true));
+    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);
+    Document doc = new Document();
+    doc.add(body);
+    
+    body.setStringValue("This is a test. Just a test highlighting from postings. Feel free to ignore.");
+    iw.addDocument(doc);
+    body.setStringValue("This test is another test. Not a good sentence. Test test test test.");
+    iw.addDocument(doc);
+    
+    IndexReader ir = iw.getReader();
+    iw.close();
+    
+    IndexSearcher searcher = newSearcher(ir);
+    PostingsHighlighter highlighter = new PostingsHighlighter("body");
+    Query query = new TermQuery(new Term("body", "test"));
+    TopDocs topDocs = searcher.search(query, null, 10, Sort.INDEXORDER);
+    assertEquals(2, topDocs.totalHits);
+    try {
+      highlighter.highlight(query, searcher, topDocs, 2);
+      fail("did not hit expected exception");
+    } catch (IllegalArgumentException iae) {
+      // expected
+    }
+    ir.close();
+    dir.close();
+  }
 }