Author: catholicon
Date: Thu Jan 18 16:14:07 2018
New Revision: 1821521

URL: http://svn.apache.org/viewvc?rev=1821521&view=rev
Log:
OAK-4401: Excerpt Highlighting for a property is not correct (backport 
r1821325,r1821516) from trunk)

This is just about ensuring that SimpleExcerptProvider would use non-letter or 
non-digit as delimeter marker before highlighting

Modified:
    jackrabbit/oak/branches/1.8/   (props changed)
    
jackrabbit/oak/branches/1.8/oak-core/src/main/java/org/apache/jackrabbit/oak/query/SimpleExcerptProvider.java
    
jackrabbit/oak/branches/1.8/oak-core/src/test/java/org/apache/jackrabbit/oak/query/SimpleExcerptProviderTest.java

Propchange: jackrabbit/oak/branches/1.8/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Thu Jan 18 16:14:07 2018
@@ -1,3 +1,3 @@
 /jackrabbit/oak/branches/1.0:1665962
-/jackrabbit/oak/trunk:1820660-1820661,1820859,1820861,1820878,1820888,1820947,1821130,1821140-1821141,1821240,1821258,1821358,1821362,1821370,1821375,1821477,1821487
+/jackrabbit/oak/trunk:1820660-1820661,1820859,1820861,1820878,1820888,1820947,1821130,1821140-1821141,1821240,1821258,1821325,1821358,1821362,1821370,1821375,1821477,1821487,1821516
 /jackrabbit/trunk:1345480

Modified: 
jackrabbit/oak/branches/1.8/oak-core/src/main/java/org/apache/jackrabbit/oak/query/SimpleExcerptProvider.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.8/oak-core/src/main/java/org/apache/jackrabbit/oak/query/SimpleExcerptProvider.java?rev=1821521&r1=1821520&r2=1821521&view=diff
==============================================================================
--- 
jackrabbit/oak/branches/1.8/oak-core/src/main/java/org/apache/jackrabbit/oak/query/SimpleExcerptProvider.java
 (original)
+++ 
jackrabbit/oak/branches/1.8/oak-core/src/main/java/org/apache/jackrabbit/oak/query/SimpleExcerptProvider.java
 Thu Jan 18 16:14:07 2018
@@ -34,6 +34,7 @@ import org.apache.jackrabbit.oak.query.a
 import org.apache.jackrabbit.oak.query.ast.OrImpl;
 import org.apache.jackrabbit.oak.plugins.memory.PropertyValues;
 
+import static java.lang.Character.isLetterOrDigit;
 import static org.apache.jackrabbit.util.Text.encodeIllegalXMLCharacters;
 
 /**
@@ -239,19 +240,38 @@ class SimpleExcerptProvider {
             }
             int endIndex = index + token.length();
             if (isLike) {
-                int nextSpace = text.indexOf(" ", endIndex);
-                if (nextSpace != -1) {
+                int nextSpace = endIndex;
+
+                while (nextSpace < text.length() && 
!isDelimeter(text.codePointAt(nextSpace))) {
+                    nextSpace++;
+                }
+
+                if (nextSpace != text.length()) {
                     endIndex = nextSpace;
                 } else {
                     endIndex = text.length();
                 }
             }
-            while (index < endIndex) {
-                highlightBits.set(index++);
+
+            boolean isStartOk = (index == 0) || //allow for highlighting for 
token at the beginning
+                    isDelimeter(text.codePointAt(index - 1)); //else token 
must follow a delimeter
+            boolean isEndOk = (endIndex == text.length()) || //token is at the 
end of string
+                    isDelimeter(text.codePointAt(endIndex)); //else token must 
precede a delimeter
+
+            if (isStartOk && isEndOk) {
+                while (index < endIndex) {
+                    highlightBits.set(index++);
+                }
+            } else {
+                index = endIndex;
             }
         }
     }
 
+    static boolean isDelimeter(int codePoint) {
+        return !isLetterOrDigit(codePoint);
+    }
+
     static PropertyValue getExcerpt(PropertyValue value) {
         Splitter listSplitter = 
Splitter.on(',').trimResults().omitEmptyStrings();
         StringBuilder excerpt = new StringBuilder(EXCERPT_BEGIN);

Modified: 
jackrabbit/oak/branches/1.8/oak-core/src/test/java/org/apache/jackrabbit/oak/query/SimpleExcerptProviderTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/branches/1.8/oak-core/src/test/java/org/apache/jackrabbit/oak/query/SimpleExcerptProviderTest.java?rev=1821521&r1=1821520&r2=1821521&view=diff
==============================================================================
--- 
jackrabbit/oak/branches/1.8/oak-core/src/test/java/org/apache/jackrabbit/oak/query/SimpleExcerptProviderTest.java
 (original)
+++ 
jackrabbit/oak/branches/1.8/oak-core/src/test/java/org/apache/jackrabbit/oak/query/SimpleExcerptProviderTest.java
 Thu Jan 18 16:14:07 2018
@@ -23,8 +23,10 @@ import static com.google.common.collect.
 import static org.apache.jackrabbit.oak.query.SimpleExcerptProvider.highlight;
 import static org.junit.Assert.assertEquals;
 
+import java.util.Map;
 import java.util.Random;
 
+import com.google.common.collect.Maps;
 import org.junit.Test;
 
 public class SimpleExcerptProviderTest {
@@ -59,6 +61,61 @@ public class SimpleExcerptProviderTest {
         }
     }
 
+    @Test
+    public void hightlightCompleteWordOnly() {
+        // using 2 non-simple spaces as mentioned in 
http://jkorpela.fi/chars/spaces.html
+        String[] delimiters = new String[] {" ", "\t", "\n", ":", "\u1680", 
"\u00A0"};
+        Map<String, String> simpleCheck = Maps.newHashMap(); // highlight "of"
+
+        // simple ones
+        simpleCheck.put("official conflict of interest",
+                "<div><span>official conflict <strong>of</strong> 
interest</span></div>");
+        simpleCheck.put("of to new city",
+                "<div><span><strong>of</strong> to new city</span></div>");
+        simpleCheck.put("out of the roof",
+                "<div><span>out <strong>of</strong> the roof</span></div>");
+        simpleCheck.put("well this is of",
+                "<div><span>well this is <strong>of</strong></span></div>");
+
+        for (Map.Entry<String, String> simple : simpleCheck.entrySet()) {
+            for (String delimiter : delimiters) {
+                String text = simple.getKey().replaceAll(" ", delimiter);
+                String expect = simple.getValue().replaceAll(" ", delimiter);
+                assertEquals("highlighting '" + text + "' for 'of' (delimiter 
- '" + delimiter + "')",
+                        expect, highlight(sb(text), of("of")));
+            }
+        }
+
+        Map<String, String> wildcardCheck = Maps.newHashMap(); // highlight 
"of*"
+        wildcardCheck.put("office room",
+                "<div><span><strong>office</strong> room</span></div>");
+        wildcardCheck.put("office room off",
+                "<div><span><strong>office</strong> room 
<strong>off</strong></span></div>");
+        wildcardCheck.put("big office room",
+                "<div><span>big <strong>office</strong> room</span></div>");
+
+        for (Map.Entry<String, String> wildcard : wildcardCheck.entrySet()) {
+            for (String delimiter : delimiters) {
+                String text = wildcard.getKey().replaceAll(" ", delimiter);
+                String expect = wildcard.getValue().replaceAll(" ", delimiter);
+                assertEquals("highlighting '" + text + "' for 'of*' (delimiter 
- '" + delimiter + "')",
+                        expect, highlight(sb(text), of("of*")));
+            }
+        }
+    }
+
+    @Test
+    public void multipleSearchTokens() {
+        String text = "To be, or not to be. That is the question!";
+        String expected = "<div><span>To <strong>be</strong>, " +
+                "or not to <strong>be</strong>. " +
+                "That is the <strong>question</strong>!</span></div>";
+
+        assertEquals(expected, highlight(sb(text), of("question", "be")));
+        assertEquals(expected, highlight(sb(text), of("quest*", "be")));
+        assertEquals(expected, highlight(sb(text), of("quest*", "b*")));
+    }
+
     private static String randomString(Random r, String set) {
         int len = r.nextInt(10);
         StringBuilder buff = new StringBuilder();


Reply via email to