You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ds...@apache.org on 2017/07/18 15:00:40 UTC

lucene-solr:branch_7x: LUCENE-7901

Repository: lucene-solr
Updated Branches:
  refs/heads/branch_7x e66ef6b99 -> d1995a61d


LUCENE-7901

(cherry picked from commit 0971fe6)


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/d1995a61
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/d1995a61
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/d1995a61

Branch: refs/heads/branch_7x
Commit: d1995a61d5d21539f2c2a2714758ec2f78210c29
Parents: e66ef6b
Author: David Smiley <ds...@apache.org>
Authored: Tue Jul 18 10:58:08 2017 -0400
Committer: David Smiley <ds...@apache.org>
Committed: Tue Jul 18 11:00:34 2017 -0400

----------------------------------------------------------------------
 lucene/CHANGES.txt                              |  3 +
 .../lucene/search/highlight/Highlighter.java    | 86 ++++++++++++--------
 2 files changed, 54 insertions(+), 35 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/d1995a61/lucene/CHANGES.txt
----------------------------------------------------------------------
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 4abb85c..90a3ed5 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -136,6 +136,9 @@ Improvements
 * LUCENE-7730: More accurate encoding of the length normalization factor
   thanks to the removal of index-time boosts. (Adrien Grand)
 
+* LUCENE-7901: Original Highlighter now eagerly throws an exception if you
+  provide components that are null. (Jason Gerlowski, David Smiley)
+
 Optimizations
 
 * LUCENE-7416: BooleanQuery optimizes queries that have queries that occur both

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/d1995a61/lucene/highlighter/src/java/org/apache/lucene/search/highlight/Highlighter.java
----------------------------------------------------------------------
diff --git a/lucene/highlighter/src/java/org/apache/lucene/search/highlight/Highlighter.java b/lucene/highlighter/src/java/org/apache/lucene/search/highlight/Highlighter.java
index 85e73df..6f3ea78 100644
--- a/lucene/highlighter/src/java/org/apache/lucene/search/highlight/Highlighter.java
+++ b/lucene/highlighter/src/java/org/apache/lucene/search/highlight/Highlighter.java
@@ -15,9 +15,11 @@
  * limitations under the License.
  */
 package org.apache.lucene.search.highlight;
+
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Iterator;
+import java.util.Objects;
 
 import org.apache.lucene.analysis.Analyzer;
 import org.apache.lucene.analysis.TokenStream;
@@ -26,38 +28,42 @@ import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
 import org.apache.lucene.util.PriorityQueue;
 
 /**
- * Class used to markup highlighted terms found in the best sections of a
+ * Marks up highlighted terms found in the best sections of
  * text, using configurable {@link Fragmenter}, {@link Scorer}, {@link Formatter},
  * {@link Encoder} and tokenizers.
+ *
+ * This is Lucene's original Highlighter; there are others.
  */
 public class Highlighter
 {
   public static final int DEFAULT_MAX_CHARS_TO_ANALYZE = 50*1024;
 
-  private int maxDocCharsToAnalyze = DEFAULT_MAX_CHARS_TO_ANALYZE;
   private Formatter formatter;
   private Encoder encoder;
-  private Fragmenter textFragmenter=new SimpleFragmenter();
-  private Scorer fragmentScorer=null;
+  private Scorer fragmentScorer;
+  private int maxDocCharsToAnalyze = DEFAULT_MAX_CHARS_TO_ANALYZE;
+  private Fragmenter textFragmenter = new SimpleFragmenter();
 
   public Highlighter(Scorer fragmentScorer)
   {
     this(new SimpleHTMLFormatter(),fragmentScorer);
   }
 
-
-   public Highlighter(Formatter formatter, Scorer fragmentScorer)
-   {
+  public Highlighter(Formatter formatter, Scorer fragmentScorer)
+  {
     this(formatter,new DefaultEncoder(),fragmentScorer);
   }
 
-
   public Highlighter(Formatter formatter, Encoder encoder, Scorer fragmentScorer)
   {
-     this.formatter = formatter;
+    ensureArgumentNotNull(formatter, "'formatter' must not be null");
+    ensureArgumentNotNull(encoder, "'encoder' must not be null");
+    ensureArgumentNotNull(fragmentScorer, "'fragmentScorer' must not be null");
+
+    this.formatter = formatter;
     this.encoder = encoder;
-     this.fragmentScorer = fragmentScorer;
-   }
+    this.fragmentScorer = fragmentScorer;
+  }
 
   /**
    * Highlights chosen terms in a text, extracting the most relevant section.
@@ -191,7 +197,7 @@ public class Highlighter
     if (fragmentScorer instanceof QueryScorer) {
       ((QueryScorer) fragmentScorer).setMaxDocCharsToAnalyze(maxDocCharsToAnalyze);
     }
-    
+
     TokenStream newStream = fragmentScorer.init(tokenStream);
     if(newStream != null) {
       tokenStream = newStream;
@@ -477,7 +483,6 @@ public class Highlighter
     this.maxDocCharsToAnalyze = maxDocCharsToAnalyze;
   }
 
-  
   public Fragmenter getTextFragmenter()
   {
     return textFragmenter;
@@ -485,7 +490,7 @@ public class Highlighter
 
   public void setTextFragmenter(Fragmenter fragmenter)
   {
-    textFragmenter = fragmenter;
+    textFragmenter = Objects.requireNonNull(fragmenter);
   }
 
   /**
@@ -496,34 +501,45 @@ public class Highlighter
     return fragmentScorer;
   }
 
-
   public void setFragmentScorer(Scorer scorer)
   {
-    fragmentScorer = scorer;
+    fragmentScorer = Objects.requireNonNull(scorer);
   }
 
-    public Encoder getEncoder()
-    {
-        return encoder;
-    }
-    public void setEncoder(Encoder encoder)
-    {
-        this.encoder = encoder;
+  public Encoder getEncoder() {
+    return encoder;
+  }
+
+  public void setEncoder(Encoder encoder) {
+    this.encoder = Objects.requireNonNull(encoder);
+  }
+
+  /**
+   * Throws an IllegalArgumentException with the provided message if 'argument' is null.
+   *
+   * @param argument the argument to be null-checked
+   * @param message  the message of the exception thrown if argument == null
+   */
+  private static void ensureArgumentNotNull(Object argument, String message) {
+    if (argument == null) {
+      throw new IllegalArgumentException(message);
     }
-}
-class FragmentQueue extends PriorityQueue<TextFragment>
-{
-  public FragmentQueue(int size)
-  {
-    super(size);
   }
 
-  @Override
-  public final boolean lessThan(TextFragment fragA, TextFragment fragB)
+  static class FragmentQueue extends PriorityQueue<TextFragment>
   {
-    if (fragA.getScore() == fragB.getScore())
-      return fragA.fragNum > fragB.fragNum;
-    else
-      return fragA.getScore() < fragB.getScore();
+    FragmentQueue(int size)
+    {
+      super(size);
+    }
+
+    @Override
+    public final boolean lessThan(TextFragment fragA, TextFragment fragB)
+    {
+      if (fragA.getScore() == fragB.getScore())
+        return fragA.fragNum > fragB.fragNum;
+      else
+        return fragA.getScore() < fragB.getScore();
+    }
   }
 }