You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@jackrabbit.apache.org by GitBox <gi...@apache.org> on 2022/06/20 07:34:43 UTC

[GitHub] [jackrabbit-oak] fabriziofortino commented on a diff in pull request #578: Oak 9783

fabriziofortino commented on code in PR #578:
URL: https://github.com/apache/jackrabbit-oak/pull/578#discussion_r899198946


##########
oak-search/src/test/java/org/apache/jackrabbit/oak/plugins/index/ExcerptTest.java:
##########
@@ -0,0 +1,340 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.jackrabbit.oak.plugins.index;
+
+import com.google.common.base.Joiner;
+import org.apache.jackrabbit.JcrConstants;
+import org.apache.jackrabbit.oak.api.*;
+import org.apache.jackrabbit.oak.plugins.index.search.FulltextIndexConstants;
+import org.apache.jackrabbit.oak.plugins.index.search.IndexFormatVersion;
+import org.apache.jackrabbit.oak.plugins.memory.ArrayBasedBlob;
+import org.apache.jackrabbit.oak.query.AbstractQueryTest;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.text.ParseException;
+import java.util.Iterator;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import static com.google.common.collect.Lists.newArrayList;
+import static org.apache.jackrabbit.oak.api.QueryEngine.NO_BINDINGS;
+import static org.apache.jackrabbit.oak.api.Type.STRING;
+import static org.apache.jackrabbit.oak.plugins.index.IndexConstants.*;
+import static org.junit.Assert.*;
+
+public abstract class ExcerptTest extends AbstractQueryTest {
+
+    protected IndexOptions indexOptions;
+    protected TestRepository repositoryOptionsUtil;
+
+    protected void assertEventually(Runnable r) {
+        TestUtils.assertEventually(r,
+                ((repositoryOptionsUtil.isAsync() ? repositoryOptionsUtil.defaultAsyncIndexingTimeInSeconds : 0) + 3000) * 5);
+    }
+
+    @Before
+    public void setup() throws Exception { //named so that it gets called after super.before :-/
+        Tree rootTree = root.getTree("/");
+
+        Tree def = rootTree.addChild(INDEX_DEFINITIONS_NAME).addChild("testExcerpt");
+        def.setProperty(JcrConstants.JCR_PRIMARYTYPE, INDEX_DEFINITIONS_NODE_TYPE, Type.NAME);
+        def.setProperty(TYPE_PROPERTY_NAME, indexOptions.getIndexType());
+        def.setProperty(REINDEX_PROPERTY_NAME, true);
+        def.setProperty(FulltextIndexConstants.EVALUATE_PATH_RESTRICTION, true);
+        def.setProperty(FulltextIndexConstants.COMPAT_MODE, IndexFormatVersion.V2.getVersion());
+
+        Tree properties = def.addChild(FulltextIndexConstants.INDEX_RULES)
+                .addChild("nt:base")
+                .addChild(FulltextIndexConstants.PROP_NODE);
+
+        Tree notIndexedProp = properties.addChild("baz");
+        notIndexedProp.setProperty(FulltextIndexConstants.PROP_NODE_SCOPE_INDEX, true);
+
+        Tree relativeProp = properties.addChild("relative-baz");
+        relativeProp.setProperty(FulltextIndexConstants.PROP_ANALYZED, true);
+        relativeProp.setProperty(FulltextIndexConstants.PROP_USE_IN_EXCERPT, true);
+        relativeProp.setProperty(FulltextIndexConstants.PROP_NAME, "relative/baz");
+
+        Tree allProps = properties.addChild("allProps");
+        allProps.setProperty(FulltextIndexConstants.PROP_ANALYZED, true);
+        allProps.setProperty(FulltextIndexConstants.PROP_NODE_SCOPE_INDEX, true);
+        allProps.setProperty(FulltextIndexConstants.PROP_USE_IN_EXCERPT, true);
+        allProps.setProperty(FulltextIndexConstants.PROP_NAME, FulltextIndexConstants.REGEX_ALL_PROPS);
+        allProps.setProperty(FulltextIndexConstants.PROP_IS_REGEX, true);
+
+        root.commit();
+    }
+
+    @Test
+    public void getAllSelectedColumns() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "is fox ifoxing");
+        contentRoot.setProperty("bar", "ifoxing fox");
+        contentRoot.setProperty("baz", "fox ifoxing");
+        root.commit();
+
+        List<String> columns = newArrayList("rep:excerpt", "rep:excerpt(.)", "rep:excerpt(foo)", "rep:excerpt(bar)");
+        String selectColumns = Joiner.on(",").join(
+                columns.stream().map(col -> "[" + col + "]").collect(Collectors.toList())
+        );
+        assertEventually(() -> {
+            String query = "SELECT " + selectColumns + " FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue excerptValue;
+                String excerpt;
+
+                for (String col : columns) {
+                    excerptValue = firstRow.getValue(col);
+                    assertNotNull(col + " not evaluated", excerptValue);
+                    excerpt = excerptValue.getValue(STRING);
+                    assertFalse(col + " didn't evaluate correctly - got '" + excerpt + "'",
+                            excerpt.contains("i<strong>fox</foxing>ing"));
+                }
+            } catch (ParseException e) {
+                fail(e.getMessage());
+            }
+        });
+    }
+
+    @Test
+    public void nodeExcerpt() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "is fox ifoxing");
+        contentRoot.setProperty("bar", "ifoxing fox");
+        root.commit();
+
+        String query = "SELECT [rep:excerpt],[rep:excerpt(.)] FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+        assertEventually(() -> {
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue nodeExcerpt;
+                String excerpt1, excerpt2;
+
+
+                nodeExcerpt = firstRow.getValue("rep:excerpt");
+                assertNotNull("rep:excerpt not evaluated", nodeExcerpt);
+                excerpt1 = nodeExcerpt.getValue(STRING);
+                assertTrue("rep:excerpt didn't evaluate correctly - got '" + excerpt1 + "'",
+                        "is <strong>fox</strong> ifoxing".equals(excerpt1) || "ifoxing <strong>fox</strong>".equals(excerpt1));
+
+                nodeExcerpt = firstRow.getValue("rep:excerpt(.)");
+                assertNotNull("rep:excerpt(.) not evaluated", nodeExcerpt);
+                excerpt2 = nodeExcerpt.getValue(STRING);
+                assertEquals("excerpt extracted via rep:excerpt not same as rep:excerpt(.)", excerpt1, excerpt2);
+            } catch (ParseException e) {
+                fail(e.getMessage());
+            }
+        });
+    }
+
+    @Test
+    public void nonIndexedRequestedPropExcerpt() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "fox");
+        contentRoot.setProperty("baz", "is fox ifoxing");
+        root.commit();
+
+        assertEventually(() -> {
+            String query = "SELECT [rep:excerpt(baz)] FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue nodeExcerpt = firstRow.getValue("rep:excerpt(baz)");
+                assertNull("rep:excerpt(baz) if requested explicitly must be indexed to be evaluated", nodeExcerpt);
+            } catch (ParseException e) {
+                fail(e.getMessage());
+            }
+        });
+    }
+
+    @Test
+    public void propExcerpt() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "is fox ifoxing");
+        root.commit();
+
+        String query = "SELECT [rep:excerpt(foo)] FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+        assertEventually(() -> {
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue nodeExcerpt;
+                String excerpt;
+
+                nodeExcerpt = firstRow.getValue("rep:excerpt(foo)");
+                assertNotNull("rep:excerpt(foo) not evaluated", nodeExcerpt);
+                excerpt = nodeExcerpt.getValue(STRING);
+                assertTrue("rep:excerpt(foo) didn't evaluate correctly - got '" + excerpt + "'",
+                        "is <strong>fox</strong> ifoxing".equals(excerpt));
+            } catch (ParseException e) {
+                fail(e.getMessage());
+            }
+        });
+    }
+
+    @Test
+    public void indexedNonRequestedPropExcerpt() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "is fox ifoxing");
+        root.commit();
+
+        String query = "SELECT [rep:excerpt] FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+        assertEventually(() -> {
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue nodeExcerpt;
+                String excerpt;
+
+                nodeExcerpt = firstRow.getValue("rep:excerpt(foo)");
+                assertNotNull("rep:excerpt(foo) not evaluated", nodeExcerpt);
+                excerpt = nodeExcerpt.getValue(STRING);
+                assertTrue("rep:excerpt(foo) didn't evaluate correctly - got '" + excerpt + "'",
+                        excerpt.contains("<strong>fox</strong>"));
+
+                assertTrue("rep:excerpt(foo) highlighting inside words - got '" + excerpt + "'",
+                        !excerpt.contains("i<strong>fox</strong>ing"));
+            } catch (ParseException e) {
+                fail(e.getMessage());
+            }
+        });
+    }
+
+    @Test
+    public void nonIndexedNonRequestedPropExcerpt() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "fox");
+        contentRoot.setProperty("baz", "is fox ifoxing");
+        root.commit();
+
+        String query = "SELECT [rep:excerpt] FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+        assertEventually(() -> {
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue nodeExcerpt;
+                String excerpt;
+
+                nodeExcerpt = firstRow.getValue("rep:excerpt(baz)");
+                assertNotNull("rep:excerpt(baz) not evaluated", nodeExcerpt);
+                excerpt = nodeExcerpt.getValue(STRING);
+                assertTrue("rep:excerpt(foo) didn't evaluate correctly - got '" + excerpt + "'",
+                        excerpt.contains("<strong>fox</strong>"));
+
+                assertTrue("rep:excerpt(baz) highlighting inside words - got '" + excerpt + "'",
+                        !excerpt.contains("i<strong>fox</strong>ing"));
+            } catch (ParseException e) {
+                fail(e.getMessage());
+            }
+        });
+    }
+
+    //We 'intentionally' are indexing node names only on root state as we don't support indexing relative or
+    //regex for node name indexing. Comment taken from FultextDocumentMaker #148. Test skipped
+    //@Test

Review Comment:
   ```suggestion
       @Test
       @Ignore
   ```



##########
oak-search/src/test/java/org/apache/jackrabbit/oak/plugins/index/ExcerptTest.java:
##########
@@ -0,0 +1,340 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.jackrabbit.oak.plugins.index;
+
+import com.google.common.base.Joiner;
+import org.apache.jackrabbit.JcrConstants;
+import org.apache.jackrabbit.oak.api.*;
+import org.apache.jackrabbit.oak.plugins.index.search.FulltextIndexConstants;
+import org.apache.jackrabbit.oak.plugins.index.search.IndexFormatVersion;
+import org.apache.jackrabbit.oak.plugins.memory.ArrayBasedBlob;
+import org.apache.jackrabbit.oak.query.AbstractQueryTest;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.text.ParseException;
+import java.util.Iterator;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import static com.google.common.collect.Lists.newArrayList;
+import static org.apache.jackrabbit.oak.api.QueryEngine.NO_BINDINGS;
+import static org.apache.jackrabbit.oak.api.Type.STRING;
+import static org.apache.jackrabbit.oak.plugins.index.IndexConstants.*;
+import static org.junit.Assert.*;
+
+public abstract class ExcerptTest extends AbstractQueryTest {
+
+    protected IndexOptions indexOptions;
+    protected TestRepository repositoryOptionsUtil;
+
+    protected void assertEventually(Runnable r) {
+        TestUtils.assertEventually(r,
+                ((repositoryOptionsUtil.isAsync() ? repositoryOptionsUtil.defaultAsyncIndexingTimeInSeconds : 0) + 3000) * 5);
+    }
+
+    @Before
+    public void setup() throws Exception { //named so that it gets called after super.before :-/
+        Tree rootTree = root.getTree("/");
+
+        Tree def = rootTree.addChild(INDEX_DEFINITIONS_NAME).addChild("testExcerpt");
+        def.setProperty(JcrConstants.JCR_PRIMARYTYPE, INDEX_DEFINITIONS_NODE_TYPE, Type.NAME);
+        def.setProperty(TYPE_PROPERTY_NAME, indexOptions.getIndexType());
+        def.setProperty(REINDEX_PROPERTY_NAME, true);
+        def.setProperty(FulltextIndexConstants.EVALUATE_PATH_RESTRICTION, true);
+        def.setProperty(FulltextIndexConstants.COMPAT_MODE, IndexFormatVersion.V2.getVersion());
+
+        Tree properties = def.addChild(FulltextIndexConstants.INDEX_RULES)
+                .addChild("nt:base")
+                .addChild(FulltextIndexConstants.PROP_NODE);
+
+        Tree notIndexedProp = properties.addChild("baz");
+        notIndexedProp.setProperty(FulltextIndexConstants.PROP_NODE_SCOPE_INDEX, true);
+
+        Tree relativeProp = properties.addChild("relative-baz");
+        relativeProp.setProperty(FulltextIndexConstants.PROP_ANALYZED, true);
+        relativeProp.setProperty(FulltextIndexConstants.PROP_USE_IN_EXCERPT, true);
+        relativeProp.setProperty(FulltextIndexConstants.PROP_NAME, "relative/baz");
+
+        Tree allProps = properties.addChild("allProps");
+        allProps.setProperty(FulltextIndexConstants.PROP_ANALYZED, true);
+        allProps.setProperty(FulltextIndexConstants.PROP_NODE_SCOPE_INDEX, true);
+        allProps.setProperty(FulltextIndexConstants.PROP_USE_IN_EXCERPT, true);
+        allProps.setProperty(FulltextIndexConstants.PROP_NAME, FulltextIndexConstants.REGEX_ALL_PROPS);
+        allProps.setProperty(FulltextIndexConstants.PROP_IS_REGEX, true);
+
+        root.commit();
+    }
+
+    @Test
+    public void getAllSelectedColumns() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "is fox ifoxing");
+        contentRoot.setProperty("bar", "ifoxing fox");
+        contentRoot.setProperty("baz", "fox ifoxing");
+        root.commit();
+
+        List<String> columns = newArrayList("rep:excerpt", "rep:excerpt(.)", "rep:excerpt(foo)", "rep:excerpt(bar)");
+        String selectColumns = Joiner.on(",").join(
+                columns.stream().map(col -> "[" + col + "]").collect(Collectors.toList())
+        );
+        assertEventually(() -> {
+            String query = "SELECT " + selectColumns + " FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue excerptValue;
+                String excerpt;
+
+                for (String col : columns) {
+                    excerptValue = firstRow.getValue(col);
+                    assertNotNull(col + " not evaluated", excerptValue);
+                    excerpt = excerptValue.getValue(STRING);
+                    assertFalse(col + " didn't evaluate correctly - got '" + excerpt + "'",
+                            excerpt.contains("i<strong>fox</foxing>ing"));
+                }
+            } catch (ParseException e) {
+                fail(e.getMessage());
+            }
+        });
+    }
+
+    @Test
+    public void nodeExcerpt() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "is fox ifoxing");
+        contentRoot.setProperty("bar", "ifoxing fox");
+        root.commit();
+
+        String query = "SELECT [rep:excerpt],[rep:excerpt(.)] FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+        assertEventually(() -> {
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue nodeExcerpt;
+                String excerpt1, excerpt2;
+
+
+                nodeExcerpt = firstRow.getValue("rep:excerpt");
+                assertNotNull("rep:excerpt not evaluated", nodeExcerpt);
+                excerpt1 = nodeExcerpt.getValue(STRING);
+                assertTrue("rep:excerpt didn't evaluate correctly - got '" + excerpt1 + "'",
+                        "is <strong>fox</strong> ifoxing".equals(excerpt1) || "ifoxing <strong>fox</strong>".equals(excerpt1));
+
+                nodeExcerpt = firstRow.getValue("rep:excerpt(.)");
+                assertNotNull("rep:excerpt(.) not evaluated", nodeExcerpt);
+                excerpt2 = nodeExcerpt.getValue(STRING);
+                assertEquals("excerpt extracted via rep:excerpt not same as rep:excerpt(.)", excerpt1, excerpt2);
+            } catch (ParseException e) {
+                fail(e.getMessage());
+            }
+        });
+    }
+
+    @Test
+    public void nonIndexedRequestedPropExcerpt() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "fox");
+        contentRoot.setProperty("baz", "is fox ifoxing");
+        root.commit();
+
+        assertEventually(() -> {
+            String query = "SELECT [rep:excerpt(baz)] FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+            Result result = null;

Review Comment:
   ```suggestion
               Result result;
   ```



##########
oak-search/src/test/java/org/apache/jackrabbit/oak/plugins/index/ExcerptTest.java:
##########
@@ -0,0 +1,340 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.jackrabbit.oak.plugins.index;
+
+import com.google.common.base.Joiner;
+import org.apache.jackrabbit.JcrConstants;
+import org.apache.jackrabbit.oak.api.*;
+import org.apache.jackrabbit.oak.plugins.index.search.FulltextIndexConstants;
+import org.apache.jackrabbit.oak.plugins.index.search.IndexFormatVersion;
+import org.apache.jackrabbit.oak.plugins.memory.ArrayBasedBlob;
+import org.apache.jackrabbit.oak.query.AbstractQueryTest;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.text.ParseException;
+import java.util.Iterator;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import static com.google.common.collect.Lists.newArrayList;
+import static org.apache.jackrabbit.oak.api.QueryEngine.NO_BINDINGS;
+import static org.apache.jackrabbit.oak.api.Type.STRING;
+import static org.apache.jackrabbit.oak.plugins.index.IndexConstants.*;
+import static org.junit.Assert.*;
+
+public abstract class ExcerptTest extends AbstractQueryTest {
+
+    protected IndexOptions indexOptions;
+    protected TestRepository repositoryOptionsUtil;
+
+    protected void assertEventually(Runnable r) {
+        TestUtils.assertEventually(r,
+                ((repositoryOptionsUtil.isAsync() ? repositoryOptionsUtil.defaultAsyncIndexingTimeInSeconds : 0) + 3000) * 5);
+    }
+
+    @Before
+    public void setup() throws Exception { //named so that it gets called after super.before :-/
+        Tree rootTree = root.getTree("/");
+
+        Tree def = rootTree.addChild(INDEX_DEFINITIONS_NAME).addChild("testExcerpt");
+        def.setProperty(JcrConstants.JCR_PRIMARYTYPE, INDEX_DEFINITIONS_NODE_TYPE, Type.NAME);
+        def.setProperty(TYPE_PROPERTY_NAME, indexOptions.getIndexType());
+        def.setProperty(REINDEX_PROPERTY_NAME, true);
+        def.setProperty(FulltextIndexConstants.EVALUATE_PATH_RESTRICTION, true);
+        def.setProperty(FulltextIndexConstants.COMPAT_MODE, IndexFormatVersion.V2.getVersion());
+
+        Tree properties = def.addChild(FulltextIndexConstants.INDEX_RULES)
+                .addChild("nt:base")
+                .addChild(FulltextIndexConstants.PROP_NODE);
+
+        Tree notIndexedProp = properties.addChild("baz");
+        notIndexedProp.setProperty(FulltextIndexConstants.PROP_NODE_SCOPE_INDEX, true);
+
+        Tree relativeProp = properties.addChild("relative-baz");
+        relativeProp.setProperty(FulltextIndexConstants.PROP_ANALYZED, true);
+        relativeProp.setProperty(FulltextIndexConstants.PROP_USE_IN_EXCERPT, true);
+        relativeProp.setProperty(FulltextIndexConstants.PROP_NAME, "relative/baz");
+
+        Tree allProps = properties.addChild("allProps");
+        allProps.setProperty(FulltextIndexConstants.PROP_ANALYZED, true);
+        allProps.setProperty(FulltextIndexConstants.PROP_NODE_SCOPE_INDEX, true);
+        allProps.setProperty(FulltextIndexConstants.PROP_USE_IN_EXCERPT, true);
+        allProps.setProperty(FulltextIndexConstants.PROP_NAME, FulltextIndexConstants.REGEX_ALL_PROPS);
+        allProps.setProperty(FulltextIndexConstants.PROP_IS_REGEX, true);
+
+        root.commit();
+    }
+
+    @Test
+    public void getAllSelectedColumns() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "is fox ifoxing");
+        contentRoot.setProperty("bar", "ifoxing fox");
+        contentRoot.setProperty("baz", "fox ifoxing");
+        root.commit();
+
+        List<String> columns = newArrayList("rep:excerpt", "rep:excerpt(.)", "rep:excerpt(foo)", "rep:excerpt(bar)");
+        String selectColumns = Joiner.on(",").join(
+                columns.stream().map(col -> "[" + col + "]").collect(Collectors.toList())
+        );
+        assertEventually(() -> {
+            String query = "SELECT " + selectColumns + " FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue excerptValue;
+                String excerpt;
+
+                for (String col : columns) {
+                    excerptValue = firstRow.getValue(col);
+                    assertNotNull(col + " not evaluated", excerptValue);
+                    excerpt = excerptValue.getValue(STRING);
+                    assertFalse(col + " didn't evaluate correctly - got '" + excerpt + "'",
+                            excerpt.contains("i<strong>fox</foxing>ing"));
+                }
+            } catch (ParseException e) {
+                fail(e.getMessage());
+            }
+        });
+    }
+
+    @Test
+    public void nodeExcerpt() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "is fox ifoxing");
+        contentRoot.setProperty("bar", "ifoxing fox");
+        root.commit();
+
+        String query = "SELECT [rep:excerpt],[rep:excerpt(.)] FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+        assertEventually(() -> {
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue nodeExcerpt;
+                String excerpt1, excerpt2;
+
+
+                nodeExcerpt = firstRow.getValue("rep:excerpt");
+                assertNotNull("rep:excerpt not evaluated", nodeExcerpt);
+                excerpt1 = nodeExcerpt.getValue(STRING);
+                assertTrue("rep:excerpt didn't evaluate correctly - got '" + excerpt1 + "'",
+                        "is <strong>fox</strong> ifoxing".equals(excerpt1) || "ifoxing <strong>fox</strong>".equals(excerpt1));
+
+                nodeExcerpt = firstRow.getValue("rep:excerpt(.)");
+                assertNotNull("rep:excerpt(.) not evaluated", nodeExcerpt);
+                excerpt2 = nodeExcerpt.getValue(STRING);
+                assertEquals("excerpt extracted via rep:excerpt not same as rep:excerpt(.)", excerpt1, excerpt2);
+            } catch (ParseException e) {
+                fail(e.getMessage());
+            }
+        });
+    }
+
+    @Test
+    public void nonIndexedRequestedPropExcerpt() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "fox");
+        contentRoot.setProperty("baz", "is fox ifoxing");
+        root.commit();
+
+        assertEventually(() -> {
+            String query = "SELECT [rep:excerpt(baz)] FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue nodeExcerpt = firstRow.getValue("rep:excerpt(baz)");
+                assertNull("rep:excerpt(baz) if requested explicitly must be indexed to be evaluated", nodeExcerpt);
+            } catch (ParseException e) {
+                fail(e.getMessage());
+            }
+        });
+    }
+
+    @Test
+    public void propExcerpt() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "is fox ifoxing");
+        root.commit();
+
+        String query = "SELECT [rep:excerpt(foo)] FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+        assertEventually(() -> {
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue nodeExcerpt;
+                String excerpt;
+
+                nodeExcerpt = firstRow.getValue("rep:excerpt(foo)");
+                assertNotNull("rep:excerpt(foo) not evaluated", nodeExcerpt);
+                excerpt = nodeExcerpt.getValue(STRING);
+                assertTrue("rep:excerpt(foo) didn't evaluate correctly - got '" + excerpt + "'",
+                        "is <strong>fox</strong> ifoxing".equals(excerpt));
+            } catch (ParseException e) {
+                fail(e.getMessage());
+            }
+        });
+    }
+
+    @Test
+    public void indexedNonRequestedPropExcerpt() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "is fox ifoxing");
+        root.commit();
+
+        String query = "SELECT [rep:excerpt] FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+        assertEventually(() -> {
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue nodeExcerpt;
+                String excerpt;
+
+                nodeExcerpt = firstRow.getValue("rep:excerpt(foo)");
+                assertNotNull("rep:excerpt(foo) not evaluated", nodeExcerpt);
+                excerpt = nodeExcerpt.getValue(STRING);
+                assertTrue("rep:excerpt(foo) didn't evaluate correctly - got '" + excerpt + "'",
+                        excerpt.contains("<strong>fox</strong>"));
+
+                assertTrue("rep:excerpt(foo) highlighting inside words - got '" + excerpt + "'",
+                        !excerpt.contains("i<strong>fox</strong>ing"));
+            } catch (ParseException e) {
+                fail(e.getMessage());
+            }
+        });
+    }
+
+    @Test
+    public void nonIndexedNonRequestedPropExcerpt() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "fox");
+        contentRoot.setProperty("baz", "is fox ifoxing");
+        root.commit();
+
+        String query = "SELECT [rep:excerpt] FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+        assertEventually(() -> {
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue nodeExcerpt;
+                String excerpt;
+
+                nodeExcerpt = firstRow.getValue("rep:excerpt(baz)");
+                assertNotNull("rep:excerpt(baz) not evaluated", nodeExcerpt);
+                excerpt = nodeExcerpt.getValue(STRING);
+                assertTrue("rep:excerpt(foo) didn't evaluate correctly - got '" + excerpt + "'",
+                        excerpt.contains("<strong>fox</strong>"));
+
+                assertTrue("rep:excerpt(baz) highlighting inside words - got '" + excerpt + "'",
+                        !excerpt.contains("i<strong>fox</strong>ing"));
+            } catch (ParseException e) {
+                fail(e.getMessage());
+            }
+        });
+    }
+
+    //We 'intentionally' are indexing node names only on root state as we don't support indexing relative or
+    //regex for node name indexing. Comment taken from FultextDocumentMaker #148. Test skipped

Review Comment:
   typo
   ```suggestion
       //regex for node name indexing. Comment taken from FulltextDocumentMaker #148. Test skipped
   ```



##########
oak-search/src/test/java/org/apache/jackrabbit/oak/plugins/index/ExcerptTest.java:
##########
@@ -0,0 +1,340 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.jackrabbit.oak.plugins.index;
+
+import com.google.common.base.Joiner;
+import org.apache.jackrabbit.JcrConstants;
+import org.apache.jackrabbit.oak.api.*;
+import org.apache.jackrabbit.oak.plugins.index.search.FulltextIndexConstants;
+import org.apache.jackrabbit.oak.plugins.index.search.IndexFormatVersion;
+import org.apache.jackrabbit.oak.plugins.memory.ArrayBasedBlob;
+import org.apache.jackrabbit.oak.query.AbstractQueryTest;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.text.ParseException;
+import java.util.Iterator;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import static com.google.common.collect.Lists.newArrayList;
+import static org.apache.jackrabbit.oak.api.QueryEngine.NO_BINDINGS;
+import static org.apache.jackrabbit.oak.api.Type.STRING;
+import static org.apache.jackrabbit.oak.plugins.index.IndexConstants.*;
+import static org.junit.Assert.*;
+
+public abstract class ExcerptTest extends AbstractQueryTest {
+
+    protected IndexOptions indexOptions;
+    protected TestRepository repositoryOptionsUtil;
+
+    protected void assertEventually(Runnable r) {
+        TestUtils.assertEventually(r,
+                ((repositoryOptionsUtil.isAsync() ? repositoryOptionsUtil.defaultAsyncIndexingTimeInSeconds : 0) + 3000) * 5);
+    }
+
+    @Before
+    public void setup() throws Exception { //named so that it gets called after super.before :-/
+        Tree rootTree = root.getTree("/");
+
+        Tree def = rootTree.addChild(INDEX_DEFINITIONS_NAME).addChild("testExcerpt");
+        def.setProperty(JcrConstants.JCR_PRIMARYTYPE, INDEX_DEFINITIONS_NODE_TYPE, Type.NAME);
+        def.setProperty(TYPE_PROPERTY_NAME, indexOptions.getIndexType());
+        def.setProperty(REINDEX_PROPERTY_NAME, true);
+        def.setProperty(FulltextIndexConstants.EVALUATE_PATH_RESTRICTION, true);
+        def.setProperty(FulltextIndexConstants.COMPAT_MODE, IndexFormatVersion.V2.getVersion());
+
+        Tree properties = def.addChild(FulltextIndexConstants.INDEX_RULES)
+                .addChild("nt:base")
+                .addChild(FulltextIndexConstants.PROP_NODE);
+
+        Tree notIndexedProp = properties.addChild("baz");
+        notIndexedProp.setProperty(FulltextIndexConstants.PROP_NODE_SCOPE_INDEX, true);
+
+        Tree relativeProp = properties.addChild("relative-baz");
+        relativeProp.setProperty(FulltextIndexConstants.PROP_ANALYZED, true);
+        relativeProp.setProperty(FulltextIndexConstants.PROP_USE_IN_EXCERPT, true);
+        relativeProp.setProperty(FulltextIndexConstants.PROP_NAME, "relative/baz");
+
+        Tree allProps = properties.addChild("allProps");
+        allProps.setProperty(FulltextIndexConstants.PROP_ANALYZED, true);
+        allProps.setProperty(FulltextIndexConstants.PROP_NODE_SCOPE_INDEX, true);
+        allProps.setProperty(FulltextIndexConstants.PROP_USE_IN_EXCERPT, true);
+        allProps.setProperty(FulltextIndexConstants.PROP_NAME, FulltextIndexConstants.REGEX_ALL_PROPS);
+        allProps.setProperty(FulltextIndexConstants.PROP_IS_REGEX, true);
+
+        root.commit();
+    }
+
+    @Test
+    public void getAllSelectedColumns() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "is fox ifoxing");
+        contentRoot.setProperty("bar", "ifoxing fox");
+        contentRoot.setProperty("baz", "fox ifoxing");
+        root.commit();
+
+        List<String> columns = newArrayList("rep:excerpt", "rep:excerpt(.)", "rep:excerpt(foo)", "rep:excerpt(bar)");
+        String selectColumns = Joiner.on(",").join(
+                columns.stream().map(col -> "[" + col + "]").collect(Collectors.toList())
+        );
+        assertEventually(() -> {
+            String query = "SELECT " + selectColumns + " FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue excerptValue;
+                String excerpt;
+
+                for (String col : columns) {
+                    excerptValue = firstRow.getValue(col);
+                    assertNotNull(col + " not evaluated", excerptValue);
+                    excerpt = excerptValue.getValue(STRING);
+                    assertFalse(col + " didn't evaluate correctly - got '" + excerpt + "'",
+                            excerpt.contains("i<strong>fox</foxing>ing"));
+                }
+            } catch (ParseException e) {
+                fail(e.getMessage());
+            }
+        });
+    }
+
+    @Test
+    public void nodeExcerpt() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "is fox ifoxing");
+        contentRoot.setProperty("bar", "ifoxing fox");
+        root.commit();
+
+        String query = "SELECT [rep:excerpt],[rep:excerpt(.)] FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+        assertEventually(() -> {
+            Result result = null;

Review Comment:
   ```suggestion
               Result result;
   ```



##########
oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneDocumentMaker.java:
##########
@@ -88,7 +88,7 @@ public LuceneDocumentMaker(@Nullable FulltextBinaryTextExtractor textExtractor,
     @Override
     protected void indexAnalyzedProperty(Document doc, String pname, String value, PropertyDefinition pd) {
         String analyzedPropName = constructAnalyzedPropertyName(pname);
-        doc.add(newPropertyField(analyzedPropName, value, !pd.skipTokenization(pname), pd.stored));
+        doc.add(newPropertyField(analyzedPropName, value, !pd.skipTokenization(pname), pd.useInExcerpt));

Review Comment:
   not sure about this change



##########
oak-search/src/test/java/org/apache/jackrabbit/oak/plugins/index/ExcerptTest.java:
##########
@@ -0,0 +1,340 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.jackrabbit.oak.plugins.index;
+
+import com.google.common.base.Joiner;
+import org.apache.jackrabbit.JcrConstants;
+import org.apache.jackrabbit.oak.api.*;
+import org.apache.jackrabbit.oak.plugins.index.search.FulltextIndexConstants;
+import org.apache.jackrabbit.oak.plugins.index.search.IndexFormatVersion;
+import org.apache.jackrabbit.oak.plugins.memory.ArrayBasedBlob;
+import org.apache.jackrabbit.oak.query.AbstractQueryTest;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.text.ParseException;
+import java.util.Iterator;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import static com.google.common.collect.Lists.newArrayList;
+import static org.apache.jackrabbit.oak.api.QueryEngine.NO_BINDINGS;
+import static org.apache.jackrabbit.oak.api.Type.STRING;
+import static org.apache.jackrabbit.oak.plugins.index.IndexConstants.*;
+import static org.junit.Assert.*;
+
+public abstract class ExcerptTest extends AbstractQueryTest {
+
+    protected IndexOptions indexOptions;
+    protected TestRepository repositoryOptionsUtil;
+
+    protected void assertEventually(Runnable r) {
+        TestUtils.assertEventually(r,
+                ((repositoryOptionsUtil.isAsync() ? repositoryOptionsUtil.defaultAsyncIndexingTimeInSeconds : 0) + 3000) * 5);
+    }
+
+    @Before
+    public void setup() throws Exception { //named so that it gets called after super.before :-/
+        Tree rootTree = root.getTree("/");
+
+        Tree def = rootTree.addChild(INDEX_DEFINITIONS_NAME).addChild("testExcerpt");
+        def.setProperty(JcrConstants.JCR_PRIMARYTYPE, INDEX_DEFINITIONS_NODE_TYPE, Type.NAME);
+        def.setProperty(TYPE_PROPERTY_NAME, indexOptions.getIndexType());
+        def.setProperty(REINDEX_PROPERTY_NAME, true);
+        def.setProperty(FulltextIndexConstants.EVALUATE_PATH_RESTRICTION, true);
+        def.setProperty(FulltextIndexConstants.COMPAT_MODE, IndexFormatVersion.V2.getVersion());
+
+        Tree properties = def.addChild(FulltextIndexConstants.INDEX_RULES)
+                .addChild("nt:base")
+                .addChild(FulltextIndexConstants.PROP_NODE);
+
+        Tree notIndexedProp = properties.addChild("baz");
+        notIndexedProp.setProperty(FulltextIndexConstants.PROP_NODE_SCOPE_INDEX, true);
+
+        Tree relativeProp = properties.addChild("relative-baz");
+        relativeProp.setProperty(FulltextIndexConstants.PROP_ANALYZED, true);
+        relativeProp.setProperty(FulltextIndexConstants.PROP_USE_IN_EXCERPT, true);
+        relativeProp.setProperty(FulltextIndexConstants.PROP_NAME, "relative/baz");
+
+        Tree allProps = properties.addChild("allProps");
+        allProps.setProperty(FulltextIndexConstants.PROP_ANALYZED, true);
+        allProps.setProperty(FulltextIndexConstants.PROP_NODE_SCOPE_INDEX, true);
+        allProps.setProperty(FulltextIndexConstants.PROP_USE_IN_EXCERPT, true);
+        allProps.setProperty(FulltextIndexConstants.PROP_NAME, FulltextIndexConstants.REGEX_ALL_PROPS);
+        allProps.setProperty(FulltextIndexConstants.PROP_IS_REGEX, true);
+
+        root.commit();
+    }
+
+    @Test
+    public void getAllSelectedColumns() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "is fox ifoxing");
+        contentRoot.setProperty("bar", "ifoxing fox");
+        contentRoot.setProperty("baz", "fox ifoxing");
+        root.commit();
+
+        List<String> columns = newArrayList("rep:excerpt", "rep:excerpt(.)", "rep:excerpt(foo)", "rep:excerpt(bar)");
+        String selectColumns = Joiner.on(",").join(
+                columns.stream().map(col -> "[" + col + "]").collect(Collectors.toList())
+        );
+        assertEventually(() -> {
+            String query = "SELECT " + selectColumns + " FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue excerptValue;
+                String excerpt;
+
+                for (String col : columns) {
+                    excerptValue = firstRow.getValue(col);
+                    assertNotNull(col + " not evaluated", excerptValue);
+                    excerpt = excerptValue.getValue(STRING);
+                    assertFalse(col + " didn't evaluate correctly - got '" + excerpt + "'",
+                            excerpt.contains("i<strong>fox</foxing>ing"));
+                }
+            } catch (ParseException e) {
+                fail(e.getMessage());
+            }
+        });
+    }
+
+    @Test
+    public void nodeExcerpt() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "is fox ifoxing");
+        contentRoot.setProperty("bar", "ifoxing fox");
+        root.commit();
+
+        String query = "SELECT [rep:excerpt],[rep:excerpt(.)] FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+        assertEventually(() -> {
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue nodeExcerpt;
+                String excerpt1, excerpt2;
+
+
+                nodeExcerpt = firstRow.getValue("rep:excerpt");
+                assertNotNull("rep:excerpt not evaluated", nodeExcerpt);
+                excerpt1 = nodeExcerpt.getValue(STRING);
+                assertTrue("rep:excerpt didn't evaluate correctly - got '" + excerpt1 + "'",
+                        "is <strong>fox</strong> ifoxing".equals(excerpt1) || "ifoxing <strong>fox</strong>".equals(excerpt1));
+
+                nodeExcerpt = firstRow.getValue("rep:excerpt(.)");
+                assertNotNull("rep:excerpt(.) not evaluated", nodeExcerpt);
+                excerpt2 = nodeExcerpt.getValue(STRING);
+                assertEquals("excerpt extracted via rep:excerpt not same as rep:excerpt(.)", excerpt1, excerpt2);
+            } catch (ParseException e) {
+                fail(e.getMessage());
+            }
+        });
+    }
+
+    @Test
+    public void nonIndexedRequestedPropExcerpt() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "fox");
+        contentRoot.setProperty("baz", "is fox ifoxing");
+        root.commit();
+
+        assertEventually(() -> {
+            String query = "SELECT [rep:excerpt(baz)] FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue nodeExcerpt = firstRow.getValue("rep:excerpt(baz)");
+                assertNull("rep:excerpt(baz) if requested explicitly must be indexed to be evaluated", nodeExcerpt);
+            } catch (ParseException e) {
+                fail(e.getMessage());
+            }
+        });
+    }
+
+    @Test
+    public void propExcerpt() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "is fox ifoxing");
+        root.commit();
+
+        String query = "SELECT [rep:excerpt(foo)] FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+        assertEventually(() -> {
+            Result result = null;

Review Comment:
   ```suggestion
               Result result;
   ```



##########
oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/query/async/ElasticResultRowAsyncIterator.java:
##########
@@ -138,13 +140,38 @@ public void on(Hit<ObjectNode> searchHit) {
             }
             try {
                 queue.put(new FulltextResultRow(path, searchHit.score() != null ? searchHit.score() : 0.0,
-                        null, elasticFacetProvider, null));
+                        readExcerptsFromResponse(searchHit), elasticFacetProvider, null));
             } catch (InterruptedException e) {
                 throw new IllegalStateException("Error producing results into the iterator queue", e);
             }
         }
     }
 
+    /**
+     * Reads excerpts from elasticsearch response.
+     * rep:excerpt and rep:excerpt(.) keys are used for :fulltext
+     * rep:excerpt(PROPERTY) for other fields.
+     * Note: properties to get excerpt from must be included in the _source, which means ingested,
+     * not necessarily Elasticsearch indexed, neither included in the mapping properties.
+     *
+     * @param searchHit
+     * @return a map with the excerpts
+     * @throws IOException
+     */
+    private Map<String, String> readExcerptsFromResponse(Hit<ObjectNode> searchHit) {
+        Map<String, String> excerpts = Maps.newHashMap();

Review Comment:
   Guava does not bring any value here. We are trying to minimize its use. Please remove it
   ```suggestion
           Map<String, String> excerpts = new HashMap<>();
   ```



##########
oak-search/src/test/java/org/apache/jackrabbit/oak/plugins/index/ExcerptTest.java:
##########
@@ -0,0 +1,340 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.jackrabbit.oak.plugins.index;
+
+import com.google.common.base.Joiner;
+import org.apache.jackrabbit.JcrConstants;
+import org.apache.jackrabbit.oak.api.*;
+import org.apache.jackrabbit.oak.plugins.index.search.FulltextIndexConstants;
+import org.apache.jackrabbit.oak.plugins.index.search.IndexFormatVersion;
+import org.apache.jackrabbit.oak.plugins.memory.ArrayBasedBlob;
+import org.apache.jackrabbit.oak.query.AbstractQueryTest;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.text.ParseException;
+import java.util.Iterator;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import static com.google.common.collect.Lists.newArrayList;
+import static org.apache.jackrabbit.oak.api.QueryEngine.NO_BINDINGS;
+import static org.apache.jackrabbit.oak.api.Type.STRING;
+import static org.apache.jackrabbit.oak.plugins.index.IndexConstants.*;
+import static org.junit.Assert.*;
+
+public abstract class ExcerptTest extends AbstractQueryTest {
+
+    protected IndexOptions indexOptions;
+    protected TestRepository repositoryOptionsUtil;
+
+    protected void assertEventually(Runnable r) {
+        TestUtils.assertEventually(r,
+                ((repositoryOptionsUtil.isAsync() ? repositoryOptionsUtil.defaultAsyncIndexingTimeInSeconds : 0) + 3000) * 5);
+    }
+
+    @Before
+    public void setup() throws Exception { //named so that it gets called after super.before :-/
+        Tree rootTree = root.getTree("/");
+
+        Tree def = rootTree.addChild(INDEX_DEFINITIONS_NAME).addChild("testExcerpt");
+        def.setProperty(JcrConstants.JCR_PRIMARYTYPE, INDEX_DEFINITIONS_NODE_TYPE, Type.NAME);
+        def.setProperty(TYPE_PROPERTY_NAME, indexOptions.getIndexType());
+        def.setProperty(REINDEX_PROPERTY_NAME, true);
+        def.setProperty(FulltextIndexConstants.EVALUATE_PATH_RESTRICTION, true);
+        def.setProperty(FulltextIndexConstants.COMPAT_MODE, IndexFormatVersion.V2.getVersion());
+
+        Tree properties = def.addChild(FulltextIndexConstants.INDEX_RULES)
+                .addChild("nt:base")
+                .addChild(FulltextIndexConstants.PROP_NODE);
+
+        Tree notIndexedProp = properties.addChild("baz");
+        notIndexedProp.setProperty(FulltextIndexConstants.PROP_NODE_SCOPE_INDEX, true);
+
+        Tree relativeProp = properties.addChild("relative-baz");
+        relativeProp.setProperty(FulltextIndexConstants.PROP_ANALYZED, true);
+        relativeProp.setProperty(FulltextIndexConstants.PROP_USE_IN_EXCERPT, true);
+        relativeProp.setProperty(FulltextIndexConstants.PROP_NAME, "relative/baz");
+
+        Tree allProps = properties.addChild("allProps");
+        allProps.setProperty(FulltextIndexConstants.PROP_ANALYZED, true);
+        allProps.setProperty(FulltextIndexConstants.PROP_NODE_SCOPE_INDEX, true);
+        allProps.setProperty(FulltextIndexConstants.PROP_USE_IN_EXCERPT, true);
+        allProps.setProperty(FulltextIndexConstants.PROP_NAME, FulltextIndexConstants.REGEX_ALL_PROPS);
+        allProps.setProperty(FulltextIndexConstants.PROP_IS_REGEX, true);
+
+        root.commit();
+    }
+
+    @Test
+    public void getAllSelectedColumns() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "is fox ifoxing");
+        contentRoot.setProperty("bar", "ifoxing fox");
+        contentRoot.setProperty("baz", "fox ifoxing");
+        root.commit();
+
+        List<String> columns = newArrayList("rep:excerpt", "rep:excerpt(.)", "rep:excerpt(foo)", "rep:excerpt(bar)");
+        String selectColumns = Joiner.on(",").join(
+                columns.stream().map(col -> "[" + col + "]").collect(Collectors.toList())
+        );
+        assertEventually(() -> {
+            String query = "SELECT " + selectColumns + " FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue excerptValue;
+                String excerpt;
+
+                for (String col : columns) {
+                    excerptValue = firstRow.getValue(col);
+                    assertNotNull(col + " not evaluated", excerptValue);
+                    excerpt = excerptValue.getValue(STRING);
+                    assertFalse(col + " didn't evaluate correctly - got '" + excerpt + "'",
+                            excerpt.contains("i<strong>fox</foxing>ing"));
+                }
+            } catch (ParseException e) {
+                fail(e.getMessage());
+            }
+        });
+    }
+
+    @Test
+    public void nodeExcerpt() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "is fox ifoxing");
+        contentRoot.setProperty("bar", "ifoxing fox");
+        root.commit();
+
+        String query = "SELECT [rep:excerpt],[rep:excerpt(.)] FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+        assertEventually(() -> {
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue nodeExcerpt;
+                String excerpt1, excerpt2;
+
+
+                nodeExcerpt = firstRow.getValue("rep:excerpt");
+                assertNotNull("rep:excerpt not evaluated", nodeExcerpt);
+                excerpt1 = nodeExcerpt.getValue(STRING);
+                assertTrue("rep:excerpt didn't evaluate correctly - got '" + excerpt1 + "'",
+                        "is <strong>fox</strong> ifoxing".equals(excerpt1) || "ifoxing <strong>fox</strong>".equals(excerpt1));
+
+                nodeExcerpt = firstRow.getValue("rep:excerpt(.)");
+                assertNotNull("rep:excerpt(.) not evaluated", nodeExcerpt);
+                excerpt2 = nodeExcerpt.getValue(STRING);
+                assertEquals("excerpt extracted via rep:excerpt not same as rep:excerpt(.)", excerpt1, excerpt2);
+            } catch (ParseException e) {
+                fail(e.getMessage());
+            }
+        });
+    }
+
+    @Test
+    public void nonIndexedRequestedPropExcerpt() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "fox");
+        contentRoot.setProperty("baz", "is fox ifoxing");
+        root.commit();
+
+        assertEventually(() -> {
+            String query = "SELECT [rep:excerpt(baz)] FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue nodeExcerpt = firstRow.getValue("rep:excerpt(baz)");
+                assertNull("rep:excerpt(baz) if requested explicitly must be indexed to be evaluated", nodeExcerpt);
+            } catch (ParseException e) {
+                fail(e.getMessage());
+            }
+        });
+    }
+
+    @Test
+    public void propExcerpt() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "is fox ifoxing");
+        root.commit();
+
+        String query = "SELECT [rep:excerpt(foo)] FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+        assertEventually(() -> {
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue nodeExcerpt;
+                String excerpt;
+
+                nodeExcerpt = firstRow.getValue("rep:excerpt(foo)");
+                assertNotNull("rep:excerpt(foo) not evaluated", nodeExcerpt);
+                excerpt = nodeExcerpt.getValue(STRING);
+                assertTrue("rep:excerpt(foo) didn't evaluate correctly - got '" + excerpt + "'",
+                        "is <strong>fox</strong> ifoxing".equals(excerpt));
+            } catch (ParseException e) {
+                fail(e.getMessage());
+            }
+        });
+    }
+
+    @Test
+    public void indexedNonRequestedPropExcerpt() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "is fox ifoxing");
+        root.commit();
+
+        String query = "SELECT [rep:excerpt] FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+        assertEventually(() -> {
+            Result result = null;

Review Comment:
   ```suggestion
               Result result;
   ```



##########
oak-search/src/test/java/org/apache/jackrabbit/oak/plugins/index/ExcerptTest.java:
##########
@@ -0,0 +1,340 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.jackrabbit.oak.plugins.index;
+
+import com.google.common.base.Joiner;
+import org.apache.jackrabbit.JcrConstants;
+import org.apache.jackrabbit.oak.api.*;
+import org.apache.jackrabbit.oak.plugins.index.search.FulltextIndexConstants;
+import org.apache.jackrabbit.oak.plugins.index.search.IndexFormatVersion;
+import org.apache.jackrabbit.oak.plugins.memory.ArrayBasedBlob;
+import org.apache.jackrabbit.oak.query.AbstractQueryTest;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.text.ParseException;
+import java.util.Iterator;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import static com.google.common.collect.Lists.newArrayList;
+import static org.apache.jackrabbit.oak.api.QueryEngine.NO_BINDINGS;
+import static org.apache.jackrabbit.oak.api.Type.STRING;
+import static org.apache.jackrabbit.oak.plugins.index.IndexConstants.*;
+import static org.junit.Assert.*;
+
+public abstract class ExcerptTest extends AbstractQueryTest {
+
+    protected IndexOptions indexOptions;
+    protected TestRepository repositoryOptionsUtil;
+
+    protected void assertEventually(Runnable r) {
+        TestUtils.assertEventually(r,
+                ((repositoryOptionsUtil.isAsync() ? repositoryOptionsUtil.defaultAsyncIndexingTimeInSeconds : 0) + 3000) * 5);
+    }
+
+    @Before
+    public void setup() throws Exception { //named so that it gets called after super.before :-/
+        Tree rootTree = root.getTree("/");
+
+        Tree def = rootTree.addChild(INDEX_DEFINITIONS_NAME).addChild("testExcerpt");
+        def.setProperty(JcrConstants.JCR_PRIMARYTYPE, INDEX_DEFINITIONS_NODE_TYPE, Type.NAME);
+        def.setProperty(TYPE_PROPERTY_NAME, indexOptions.getIndexType());
+        def.setProperty(REINDEX_PROPERTY_NAME, true);
+        def.setProperty(FulltextIndexConstants.EVALUATE_PATH_RESTRICTION, true);
+        def.setProperty(FulltextIndexConstants.COMPAT_MODE, IndexFormatVersion.V2.getVersion());
+
+        Tree properties = def.addChild(FulltextIndexConstants.INDEX_RULES)
+                .addChild("nt:base")
+                .addChild(FulltextIndexConstants.PROP_NODE);
+
+        Tree notIndexedProp = properties.addChild("baz");
+        notIndexedProp.setProperty(FulltextIndexConstants.PROP_NODE_SCOPE_INDEX, true);
+
+        Tree relativeProp = properties.addChild("relative-baz");
+        relativeProp.setProperty(FulltextIndexConstants.PROP_ANALYZED, true);
+        relativeProp.setProperty(FulltextIndexConstants.PROP_USE_IN_EXCERPT, true);
+        relativeProp.setProperty(FulltextIndexConstants.PROP_NAME, "relative/baz");
+
+        Tree allProps = properties.addChild("allProps");
+        allProps.setProperty(FulltextIndexConstants.PROP_ANALYZED, true);
+        allProps.setProperty(FulltextIndexConstants.PROP_NODE_SCOPE_INDEX, true);
+        allProps.setProperty(FulltextIndexConstants.PROP_USE_IN_EXCERPT, true);
+        allProps.setProperty(FulltextIndexConstants.PROP_NAME, FulltextIndexConstants.REGEX_ALL_PROPS);
+        allProps.setProperty(FulltextIndexConstants.PROP_IS_REGEX, true);
+
+        root.commit();
+    }
+
+    @Test
+    public void getAllSelectedColumns() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "is fox ifoxing");
+        contentRoot.setProperty("bar", "ifoxing fox");
+        contentRoot.setProperty("baz", "fox ifoxing");
+        root.commit();
+
+        List<String> columns = newArrayList("rep:excerpt", "rep:excerpt(.)", "rep:excerpt(foo)", "rep:excerpt(bar)");
+        String selectColumns = Joiner.on(",").join(
+                columns.stream().map(col -> "[" + col + "]").collect(Collectors.toList())
+        );
+        assertEventually(() -> {
+            String query = "SELECT " + selectColumns + " FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue excerptValue;
+                String excerpt;
+
+                for (String col : columns) {
+                    excerptValue = firstRow.getValue(col);
+                    assertNotNull(col + " not evaluated", excerptValue);
+                    excerpt = excerptValue.getValue(STRING);
+                    assertFalse(col + " didn't evaluate correctly - got '" + excerpt + "'",
+                            excerpt.contains("i<strong>fox</foxing>ing"));
+                }
+            } catch (ParseException e) {
+                fail(e.getMessage());
+            }
+        });
+    }
+
+    @Test
+    public void nodeExcerpt() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "is fox ifoxing");
+        contentRoot.setProperty("bar", "ifoxing fox");
+        root.commit();
+
+        String query = "SELECT [rep:excerpt],[rep:excerpt(.)] FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+        assertEventually(() -> {
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue nodeExcerpt;
+                String excerpt1, excerpt2;
+
+
+                nodeExcerpt = firstRow.getValue("rep:excerpt");
+                assertNotNull("rep:excerpt not evaluated", nodeExcerpt);
+                excerpt1 = nodeExcerpt.getValue(STRING);
+                assertTrue("rep:excerpt didn't evaluate correctly - got '" + excerpt1 + "'",
+                        "is <strong>fox</strong> ifoxing".equals(excerpt1) || "ifoxing <strong>fox</strong>".equals(excerpt1));
+
+                nodeExcerpt = firstRow.getValue("rep:excerpt(.)");
+                assertNotNull("rep:excerpt(.) not evaluated", nodeExcerpt);
+                excerpt2 = nodeExcerpt.getValue(STRING);
+                assertEquals("excerpt extracted via rep:excerpt not same as rep:excerpt(.)", excerpt1, excerpt2);
+            } catch (ParseException e) {
+                fail(e.getMessage());
+            }
+        });
+    }
+
+    @Test
+    public void nonIndexedRequestedPropExcerpt() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "fox");
+        contentRoot.setProperty("baz", "is fox ifoxing");
+        root.commit();
+
+        assertEventually(() -> {
+            String query = "SELECT [rep:excerpt(baz)] FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue nodeExcerpt = firstRow.getValue("rep:excerpt(baz)");
+                assertNull("rep:excerpt(baz) if requested explicitly must be indexed to be evaluated", nodeExcerpt);
+            } catch (ParseException e) {
+                fail(e.getMessage());
+            }
+        });
+    }
+
+    @Test
+    public void propExcerpt() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "is fox ifoxing");
+        root.commit();
+
+        String query = "SELECT [rep:excerpt(foo)] FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+        assertEventually(() -> {
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue nodeExcerpt;
+                String excerpt;
+
+                nodeExcerpt = firstRow.getValue("rep:excerpt(foo)");
+                assertNotNull("rep:excerpt(foo) not evaluated", nodeExcerpt);
+                excerpt = nodeExcerpt.getValue(STRING);
+                assertTrue("rep:excerpt(foo) didn't evaluate correctly - got '" + excerpt + "'",
+                        "is <strong>fox</strong> ifoxing".equals(excerpt));
+            } catch (ParseException e) {
+                fail(e.getMessage());
+            }
+        });
+    }
+
+    @Test
+    public void indexedNonRequestedPropExcerpt() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "is fox ifoxing");
+        root.commit();
+
+        String query = "SELECT [rep:excerpt] FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+        assertEventually(() -> {
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue nodeExcerpt;
+                String excerpt;
+
+                nodeExcerpt = firstRow.getValue("rep:excerpt(foo)");
+                assertNotNull("rep:excerpt(foo) not evaluated", nodeExcerpt);
+                excerpt = nodeExcerpt.getValue(STRING);
+                assertTrue("rep:excerpt(foo) didn't evaluate correctly - got '" + excerpt + "'",
+                        excerpt.contains("<strong>fox</strong>"));
+
+                assertTrue("rep:excerpt(foo) highlighting inside words - got '" + excerpt + "'",
+                        !excerpt.contains("i<strong>fox</strong>ing"));
+            } catch (ParseException e) {
+                fail(e.getMessage());
+            }
+        });
+    }
+
+    @Test
+    public void nonIndexedNonRequestedPropExcerpt() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "fox");
+        contentRoot.setProperty("baz", "is fox ifoxing");
+        root.commit();
+
+        String query = "SELECT [rep:excerpt] FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+        assertEventually(() -> {
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue nodeExcerpt;
+                String excerpt;
+
+                nodeExcerpt = firstRow.getValue("rep:excerpt(baz)");
+                assertNotNull("rep:excerpt(baz) not evaluated", nodeExcerpt);
+                excerpt = nodeExcerpt.getValue(STRING);
+                assertTrue("rep:excerpt(foo) didn't evaluate correctly - got '" + excerpt + "'",
+                        excerpt.contains("<strong>fox</strong>"));
+
+                assertTrue("rep:excerpt(baz) highlighting inside words - got '" + excerpt + "'",
+                        !excerpt.contains("i<strong>fox</strong>ing"));
+            } catch (ParseException e) {
+                fail(e.getMessage());
+            }
+        });
+    }
+
+    //We 'intentionally' are indexing node names only on root state as we don't support indexing relative or
+    //regex for node name indexing. Comment taken from FultextDocumentMaker #148. Test skipped
+    //@Test
+    public void relativePropExcerpt() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.addChild("relative").setProperty("baz", "is fox ifoxing");
+        root.commit();
+
+        String query = "SELECT [rep:excerpt(relative/baz)] FROM [nt:base] WHERE CONTAINS([relative/baz], 'fox')";
+        assertEventually(() -> {
+            Result result = null;

Review Comment:
   ```suggestion
               Result result;
   ```



##########
oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/query/async/ElasticResultRowAsyncIterator.java:
##########
@@ -138,13 +140,38 @@ public void on(Hit<ObjectNode> searchHit) {
             }
             try {
                 queue.put(new FulltextResultRow(path, searchHit.score() != null ? searchHit.score() : 0.0,
-                        null, elasticFacetProvider, null));
+                        readExcerptsFromResponse(searchHit), elasticFacetProvider, null));
             } catch (InterruptedException e) {
                 throw new IllegalStateException("Error producing results into the iterator queue", e);
             }
         }
     }
 
+    /**
+     * Reads excerpts from elasticsearch response.
+     * rep:excerpt and rep:excerpt(.) keys are used for :fulltext
+     * rep:excerpt(PROPERTY) for other fields.
+     * Note: properties to get excerpt from must be included in the _source, which means ingested,
+     * not necessarily Elasticsearch indexed, neither included in the mapping properties.
+     *
+     * @param searchHit
+     * @return a map with the excerpts
+     * @throws IOException

Review Comment:
   the method does not throw any IOException and the other stuff does not bring additional info. I would remove it
   ```suggestion
   ```



##########
oak-search/src/test/java/org/apache/jackrabbit/oak/plugins/index/ExcerptTest.java:
##########
@@ -0,0 +1,340 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.jackrabbit.oak.plugins.index;
+
+import com.google.common.base.Joiner;
+import org.apache.jackrabbit.JcrConstants;
+import org.apache.jackrabbit.oak.api.*;
+import org.apache.jackrabbit.oak.plugins.index.search.FulltextIndexConstants;
+import org.apache.jackrabbit.oak.plugins.index.search.IndexFormatVersion;
+import org.apache.jackrabbit.oak.plugins.memory.ArrayBasedBlob;
+import org.apache.jackrabbit.oak.query.AbstractQueryTest;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.text.ParseException;
+import java.util.Iterator;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import static com.google.common.collect.Lists.newArrayList;
+import static org.apache.jackrabbit.oak.api.QueryEngine.NO_BINDINGS;
+import static org.apache.jackrabbit.oak.api.Type.STRING;
+import static org.apache.jackrabbit.oak.plugins.index.IndexConstants.*;
+import static org.junit.Assert.*;
+
+public abstract class ExcerptTest extends AbstractQueryTest {
+
+    protected IndexOptions indexOptions;
+    protected TestRepository repositoryOptionsUtil;
+
+    protected void assertEventually(Runnable r) {
+        TestUtils.assertEventually(r,
+                ((repositoryOptionsUtil.isAsync() ? repositoryOptionsUtil.defaultAsyncIndexingTimeInSeconds : 0) + 3000) * 5);
+    }
+
+    @Before
+    public void setup() throws Exception { //named so that it gets called after super.before :-/
+        Tree rootTree = root.getTree("/");
+
+        Tree def = rootTree.addChild(INDEX_DEFINITIONS_NAME).addChild("testExcerpt");
+        def.setProperty(JcrConstants.JCR_PRIMARYTYPE, INDEX_DEFINITIONS_NODE_TYPE, Type.NAME);
+        def.setProperty(TYPE_PROPERTY_NAME, indexOptions.getIndexType());
+        def.setProperty(REINDEX_PROPERTY_NAME, true);
+        def.setProperty(FulltextIndexConstants.EVALUATE_PATH_RESTRICTION, true);
+        def.setProperty(FulltextIndexConstants.COMPAT_MODE, IndexFormatVersion.V2.getVersion());
+
+        Tree properties = def.addChild(FulltextIndexConstants.INDEX_RULES)
+                .addChild("nt:base")
+                .addChild(FulltextIndexConstants.PROP_NODE);
+
+        Tree notIndexedProp = properties.addChild("baz");
+        notIndexedProp.setProperty(FulltextIndexConstants.PROP_NODE_SCOPE_INDEX, true);
+
+        Tree relativeProp = properties.addChild("relative-baz");
+        relativeProp.setProperty(FulltextIndexConstants.PROP_ANALYZED, true);
+        relativeProp.setProperty(FulltextIndexConstants.PROP_USE_IN_EXCERPT, true);
+        relativeProp.setProperty(FulltextIndexConstants.PROP_NAME, "relative/baz");
+
+        Tree allProps = properties.addChild("allProps");
+        allProps.setProperty(FulltextIndexConstants.PROP_ANALYZED, true);
+        allProps.setProperty(FulltextIndexConstants.PROP_NODE_SCOPE_INDEX, true);
+        allProps.setProperty(FulltextIndexConstants.PROP_USE_IN_EXCERPT, true);
+        allProps.setProperty(FulltextIndexConstants.PROP_NAME, FulltextIndexConstants.REGEX_ALL_PROPS);
+        allProps.setProperty(FulltextIndexConstants.PROP_IS_REGEX, true);
+
+        root.commit();
+    }
+
+    @Test
+    public void getAllSelectedColumns() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "is fox ifoxing");
+        contentRoot.setProperty("bar", "ifoxing fox");
+        contentRoot.setProperty("baz", "fox ifoxing");
+        root.commit();
+
+        List<String> columns = newArrayList("rep:excerpt", "rep:excerpt(.)", "rep:excerpt(foo)", "rep:excerpt(bar)");
+        String selectColumns = Joiner.on(",").join(
+                columns.stream().map(col -> "[" + col + "]").collect(Collectors.toList())
+        );
+        assertEventually(() -> {
+            String query = "SELECT " + selectColumns + " FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue excerptValue;
+                String excerpt;
+
+                for (String col : columns) {
+                    excerptValue = firstRow.getValue(col);
+                    assertNotNull(col + " not evaluated", excerptValue);
+                    excerpt = excerptValue.getValue(STRING);
+                    assertFalse(col + " didn't evaluate correctly - got '" + excerpt + "'",
+                            excerpt.contains("i<strong>fox</foxing>ing"));
+                }
+            } catch (ParseException e) {
+                fail(e.getMessage());
+            }
+        });
+    }
+
+    @Test
+    public void nodeExcerpt() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "is fox ifoxing");
+        contentRoot.setProperty("bar", "ifoxing fox");
+        root.commit();
+
+        String query = "SELECT [rep:excerpt],[rep:excerpt(.)] FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+        assertEventually(() -> {
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue nodeExcerpt;
+                String excerpt1, excerpt2;
+
+
+                nodeExcerpt = firstRow.getValue("rep:excerpt");
+                assertNotNull("rep:excerpt not evaluated", nodeExcerpt);
+                excerpt1 = nodeExcerpt.getValue(STRING);
+                assertTrue("rep:excerpt didn't evaluate correctly - got '" + excerpt1 + "'",
+                        "is <strong>fox</strong> ifoxing".equals(excerpt1) || "ifoxing <strong>fox</strong>".equals(excerpt1));
+
+                nodeExcerpt = firstRow.getValue("rep:excerpt(.)");
+                assertNotNull("rep:excerpt(.) not evaluated", nodeExcerpt);
+                excerpt2 = nodeExcerpt.getValue(STRING);
+                assertEquals("excerpt extracted via rep:excerpt not same as rep:excerpt(.)", excerpt1, excerpt2);
+            } catch (ParseException e) {
+                fail(e.getMessage());
+            }
+        });
+    }
+
+    @Test
+    public void nonIndexedRequestedPropExcerpt() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "fox");
+        contentRoot.setProperty("baz", "is fox ifoxing");
+        root.commit();
+
+        assertEventually(() -> {
+            String query = "SELECT [rep:excerpt(baz)] FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue nodeExcerpt = firstRow.getValue("rep:excerpt(baz)");
+                assertNull("rep:excerpt(baz) if requested explicitly must be indexed to be evaluated", nodeExcerpt);
+            } catch (ParseException e) {
+                fail(e.getMessage());
+            }
+        });
+    }
+
+    @Test
+    public void propExcerpt() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "is fox ifoxing");
+        root.commit();
+
+        String query = "SELECT [rep:excerpt(foo)] FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+        assertEventually(() -> {
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue nodeExcerpt;
+                String excerpt;
+
+                nodeExcerpt = firstRow.getValue("rep:excerpt(foo)");
+                assertNotNull("rep:excerpt(foo) not evaluated", nodeExcerpt);
+                excerpt = nodeExcerpt.getValue(STRING);
+                assertTrue("rep:excerpt(foo) didn't evaluate correctly - got '" + excerpt + "'",
+                        "is <strong>fox</strong> ifoxing".equals(excerpt));
+            } catch (ParseException e) {
+                fail(e.getMessage());
+            }
+        });
+    }
+
+    @Test
+    public void indexedNonRequestedPropExcerpt() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "is fox ifoxing");
+        root.commit();
+
+        String query = "SELECT [rep:excerpt] FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+        assertEventually(() -> {
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue nodeExcerpt;
+                String excerpt;
+
+                nodeExcerpt = firstRow.getValue("rep:excerpt(foo)");
+                assertNotNull("rep:excerpt(foo) not evaluated", nodeExcerpt);
+                excerpt = nodeExcerpt.getValue(STRING);
+                assertTrue("rep:excerpt(foo) didn't evaluate correctly - got '" + excerpt + "'",
+                        excerpt.contains("<strong>fox</strong>"));
+
+                assertTrue("rep:excerpt(foo) highlighting inside words - got '" + excerpt + "'",
+                        !excerpt.contains("i<strong>fox</strong>ing"));
+            } catch (ParseException e) {
+                fail(e.getMessage());
+            }
+        });
+    }
+
+    @Test
+    public void nonIndexedNonRequestedPropExcerpt() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "fox");
+        contentRoot.setProperty("baz", "is fox ifoxing");
+        root.commit();
+
+        String query = "SELECT [rep:excerpt] FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+        assertEventually(() -> {
+            Result result = null;

Review Comment:
   ```suggestion
               Result result;
   ```



##########
oak-search/src/test/java/org/apache/jackrabbit/oak/plugins/index/ExcerptTest.java:
##########
@@ -0,0 +1,340 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.jackrabbit.oak.plugins.index;
+
+import com.google.common.base.Joiner;
+import org.apache.jackrabbit.JcrConstants;
+import org.apache.jackrabbit.oak.api.*;
+import org.apache.jackrabbit.oak.plugins.index.search.FulltextIndexConstants;
+import org.apache.jackrabbit.oak.plugins.index.search.IndexFormatVersion;
+import org.apache.jackrabbit.oak.plugins.memory.ArrayBasedBlob;
+import org.apache.jackrabbit.oak.query.AbstractQueryTest;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.text.ParseException;
+import java.util.Iterator;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import static com.google.common.collect.Lists.newArrayList;
+import static org.apache.jackrabbit.oak.api.QueryEngine.NO_BINDINGS;
+import static org.apache.jackrabbit.oak.api.Type.STRING;
+import static org.apache.jackrabbit.oak.plugins.index.IndexConstants.*;
+import static org.junit.Assert.*;
+
+public abstract class ExcerptTest extends AbstractQueryTest {
+
+    protected IndexOptions indexOptions;
+    protected TestRepository repositoryOptionsUtil;
+
+    protected void assertEventually(Runnable r) {
+        TestUtils.assertEventually(r,
+                ((repositoryOptionsUtil.isAsync() ? repositoryOptionsUtil.defaultAsyncIndexingTimeInSeconds : 0) + 3000) * 5);
+    }
+
+    @Before
+    public void setup() throws Exception { //named so that it gets called after super.before :-/
+        Tree rootTree = root.getTree("/");
+
+        Tree def = rootTree.addChild(INDEX_DEFINITIONS_NAME).addChild("testExcerpt");
+        def.setProperty(JcrConstants.JCR_PRIMARYTYPE, INDEX_DEFINITIONS_NODE_TYPE, Type.NAME);
+        def.setProperty(TYPE_PROPERTY_NAME, indexOptions.getIndexType());
+        def.setProperty(REINDEX_PROPERTY_NAME, true);
+        def.setProperty(FulltextIndexConstants.EVALUATE_PATH_RESTRICTION, true);
+        def.setProperty(FulltextIndexConstants.COMPAT_MODE, IndexFormatVersion.V2.getVersion());
+
+        Tree properties = def.addChild(FulltextIndexConstants.INDEX_RULES)
+                .addChild("nt:base")
+                .addChild(FulltextIndexConstants.PROP_NODE);
+
+        Tree notIndexedProp = properties.addChild("baz");
+        notIndexedProp.setProperty(FulltextIndexConstants.PROP_NODE_SCOPE_INDEX, true);
+
+        Tree relativeProp = properties.addChild("relative-baz");
+        relativeProp.setProperty(FulltextIndexConstants.PROP_ANALYZED, true);
+        relativeProp.setProperty(FulltextIndexConstants.PROP_USE_IN_EXCERPT, true);
+        relativeProp.setProperty(FulltextIndexConstants.PROP_NAME, "relative/baz");
+
+        Tree allProps = properties.addChild("allProps");
+        allProps.setProperty(FulltextIndexConstants.PROP_ANALYZED, true);
+        allProps.setProperty(FulltextIndexConstants.PROP_NODE_SCOPE_INDEX, true);
+        allProps.setProperty(FulltextIndexConstants.PROP_USE_IN_EXCERPT, true);
+        allProps.setProperty(FulltextIndexConstants.PROP_NAME, FulltextIndexConstants.REGEX_ALL_PROPS);
+        allProps.setProperty(FulltextIndexConstants.PROP_IS_REGEX, true);
+
+        root.commit();
+    }
+
+    @Test
+    public void getAllSelectedColumns() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "is fox ifoxing");
+        contentRoot.setProperty("bar", "ifoxing fox");
+        contentRoot.setProperty("baz", "fox ifoxing");
+        root.commit();
+
+        List<String> columns = newArrayList("rep:excerpt", "rep:excerpt(.)", "rep:excerpt(foo)", "rep:excerpt(bar)");
+        String selectColumns = Joiner.on(",").join(
+                columns.stream().map(col -> "[" + col + "]").collect(Collectors.toList())
+        );
+        assertEventually(() -> {
+            String query = "SELECT " + selectColumns + " FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue excerptValue;
+                String excerpt;
+
+                for (String col : columns) {
+                    excerptValue = firstRow.getValue(col);
+                    assertNotNull(col + " not evaluated", excerptValue);
+                    excerpt = excerptValue.getValue(STRING);
+                    assertFalse(col + " didn't evaluate correctly - got '" + excerpt + "'",
+                            excerpt.contains("i<strong>fox</foxing>ing"));
+                }
+            } catch (ParseException e) {
+                fail(e.getMessage());
+            }
+        });
+    }
+
+    @Test
+    public void nodeExcerpt() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "is fox ifoxing");
+        contentRoot.setProperty("bar", "ifoxing fox");
+        root.commit();
+
+        String query = "SELECT [rep:excerpt],[rep:excerpt(.)] FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+        assertEventually(() -> {
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue nodeExcerpt;
+                String excerpt1, excerpt2;
+
+
+                nodeExcerpt = firstRow.getValue("rep:excerpt");
+                assertNotNull("rep:excerpt not evaluated", nodeExcerpt);
+                excerpt1 = nodeExcerpt.getValue(STRING);
+                assertTrue("rep:excerpt didn't evaluate correctly - got '" + excerpt1 + "'",
+                        "is <strong>fox</strong> ifoxing".equals(excerpt1) || "ifoxing <strong>fox</strong>".equals(excerpt1));
+
+                nodeExcerpt = firstRow.getValue("rep:excerpt(.)");
+                assertNotNull("rep:excerpt(.) not evaluated", nodeExcerpt);
+                excerpt2 = nodeExcerpt.getValue(STRING);
+                assertEquals("excerpt extracted via rep:excerpt not same as rep:excerpt(.)", excerpt1, excerpt2);
+            } catch (ParseException e) {
+                fail(e.getMessage());
+            }
+        });
+    }
+
+    @Test
+    public void nonIndexedRequestedPropExcerpt() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "fox");
+        contentRoot.setProperty("baz", "is fox ifoxing");
+        root.commit();
+
+        assertEventually(() -> {
+            String query = "SELECT [rep:excerpt(baz)] FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue nodeExcerpt = firstRow.getValue("rep:excerpt(baz)");
+                assertNull("rep:excerpt(baz) if requested explicitly must be indexed to be evaluated", nodeExcerpt);
+            } catch (ParseException e) {
+                fail(e.getMessage());
+            }
+        });
+    }
+
+    @Test
+    public void propExcerpt() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "is fox ifoxing");
+        root.commit();
+
+        String query = "SELECT [rep:excerpt(foo)] FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+        assertEventually(() -> {
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue nodeExcerpt;
+                String excerpt;
+
+                nodeExcerpt = firstRow.getValue("rep:excerpt(foo)");
+                assertNotNull("rep:excerpt(foo) not evaluated", nodeExcerpt);
+                excerpt = nodeExcerpt.getValue(STRING);
+                assertTrue("rep:excerpt(foo) didn't evaluate correctly - got '" + excerpt + "'",
+                        "is <strong>fox</strong> ifoxing".equals(excerpt));
+            } catch (ParseException e) {
+                fail(e.getMessage());
+            }
+        });
+    }
+
+    @Test
+    public void indexedNonRequestedPropExcerpt() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "is fox ifoxing");
+        root.commit();
+
+        String query = "SELECT [rep:excerpt] FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+        assertEventually(() -> {
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue nodeExcerpt;
+                String excerpt;
+
+                nodeExcerpt = firstRow.getValue("rep:excerpt(foo)");
+                assertNotNull("rep:excerpt(foo) not evaluated", nodeExcerpt);
+                excerpt = nodeExcerpt.getValue(STRING);
+                assertTrue("rep:excerpt(foo) didn't evaluate correctly - got '" + excerpt + "'",
+                        excerpt.contains("<strong>fox</strong>"));
+
+                assertTrue("rep:excerpt(foo) highlighting inside words - got '" + excerpt + "'",
+                        !excerpt.contains("i<strong>fox</strong>ing"));
+            } catch (ParseException e) {
+                fail(e.getMessage());
+            }
+        });
+    }
+
+    @Test
+    public void nonIndexedNonRequestedPropExcerpt() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "fox");
+        contentRoot.setProperty("baz", "is fox ifoxing");
+        root.commit();
+
+        String query = "SELECT [rep:excerpt] FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+        assertEventually(() -> {
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue nodeExcerpt;
+                String excerpt;
+
+                nodeExcerpt = firstRow.getValue("rep:excerpt(baz)");
+                assertNotNull("rep:excerpt(baz) not evaluated", nodeExcerpt);
+                excerpt = nodeExcerpt.getValue(STRING);
+                assertTrue("rep:excerpt(foo) didn't evaluate correctly - got '" + excerpt + "'",
+                        excerpt.contains("<strong>fox</strong>"));
+
+                assertTrue("rep:excerpt(baz) highlighting inside words - got '" + excerpt + "'",
+                        !excerpt.contains("i<strong>fox</strong>ing"));
+            } catch (ParseException e) {
+                fail(e.getMessage());
+            }
+        });
+    }
+
+    //We 'intentionally' are indexing node names only on root state as we don't support indexing relative or
+    //regex for node name indexing. Comment taken from FultextDocumentMaker #148. Test skipped
+    //@Test
+    public void relativePropExcerpt() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.addChild("relative").setProperty("baz", "is fox ifoxing");
+        root.commit();
+
+        String query = "SELECT [rep:excerpt(relative/baz)] FROM [nt:base] WHERE CONTAINS([relative/baz], 'fox')";
+        assertEventually(() -> {
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue nodeExcerpt;
+                String excerpt;
+
+                nodeExcerpt = firstRow.getValue("rep:excerpt(relative/baz)");
+                assertNotNull("rep:excerpt(relative/baz) not evaluated", nodeExcerpt);
+                excerpt = nodeExcerpt.getValue(STRING);
+                assertTrue("rep:excerpt(relative/baz) didn't evaluate correctly - got '" + excerpt + "'",
+                        "is <strong>fox</strong> ifoxing".equals(excerpt));
+            } catch (ParseException e) {
+                fail(e.getMessage());
+            }
+        });
+    }
+
+    @Test
+    public void binaryExcerpt() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+
+        String binaryText = "is fox foxing as a fox cub";
+        Blob blob = new ArrayBasedBlob(binaryText.getBytes());
+        TestUtil.createFileNode(contentRoot, "binaryNode", blob, "text/plain");
+        root.commit();
+
+        String query = "SELECT [rep:excerpt] FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+        assertEventually(() -> {
+            Result result = null;

Review Comment:
   ```suggestion
               Result result;
   ```



##########
oak-search/src/test/java/org/apache/jackrabbit/oak/plugins/index/ExcerptTest.java:
##########
@@ -0,0 +1,340 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.jackrabbit.oak.plugins.index;
+
+import com.google.common.base.Joiner;
+import org.apache.jackrabbit.JcrConstants;
+import org.apache.jackrabbit.oak.api.*;
+import org.apache.jackrabbit.oak.plugins.index.search.FulltextIndexConstants;
+import org.apache.jackrabbit.oak.plugins.index.search.IndexFormatVersion;
+import org.apache.jackrabbit.oak.plugins.memory.ArrayBasedBlob;
+import org.apache.jackrabbit.oak.query.AbstractQueryTest;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.text.ParseException;
+import java.util.Iterator;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import static com.google.common.collect.Lists.newArrayList;
+import static org.apache.jackrabbit.oak.api.QueryEngine.NO_BINDINGS;
+import static org.apache.jackrabbit.oak.api.Type.STRING;
+import static org.apache.jackrabbit.oak.plugins.index.IndexConstants.*;
+import static org.junit.Assert.*;
+
+public abstract class ExcerptTest extends AbstractQueryTest {
+
+    protected IndexOptions indexOptions;
+    protected TestRepository repositoryOptionsUtil;
+
+    protected void assertEventually(Runnable r) {
+        TestUtils.assertEventually(r,
+                ((repositoryOptionsUtil.isAsync() ? repositoryOptionsUtil.defaultAsyncIndexingTimeInSeconds : 0) + 3000) * 5);
+    }
+
+    @Before
+    public void setup() throws Exception { //named so that it gets called after super.before :-/
+        Tree rootTree = root.getTree("/");
+
+        Tree def = rootTree.addChild(INDEX_DEFINITIONS_NAME).addChild("testExcerpt");
+        def.setProperty(JcrConstants.JCR_PRIMARYTYPE, INDEX_DEFINITIONS_NODE_TYPE, Type.NAME);
+        def.setProperty(TYPE_PROPERTY_NAME, indexOptions.getIndexType());
+        def.setProperty(REINDEX_PROPERTY_NAME, true);
+        def.setProperty(FulltextIndexConstants.EVALUATE_PATH_RESTRICTION, true);
+        def.setProperty(FulltextIndexConstants.COMPAT_MODE, IndexFormatVersion.V2.getVersion());
+
+        Tree properties = def.addChild(FulltextIndexConstants.INDEX_RULES)
+                .addChild("nt:base")
+                .addChild(FulltextIndexConstants.PROP_NODE);
+
+        Tree notIndexedProp = properties.addChild("baz");
+        notIndexedProp.setProperty(FulltextIndexConstants.PROP_NODE_SCOPE_INDEX, true);
+
+        Tree relativeProp = properties.addChild("relative-baz");
+        relativeProp.setProperty(FulltextIndexConstants.PROP_ANALYZED, true);
+        relativeProp.setProperty(FulltextIndexConstants.PROP_USE_IN_EXCERPT, true);
+        relativeProp.setProperty(FulltextIndexConstants.PROP_NAME, "relative/baz");
+
+        Tree allProps = properties.addChild("allProps");
+        allProps.setProperty(FulltextIndexConstants.PROP_ANALYZED, true);
+        allProps.setProperty(FulltextIndexConstants.PROP_NODE_SCOPE_INDEX, true);
+        allProps.setProperty(FulltextIndexConstants.PROP_USE_IN_EXCERPT, true);
+        allProps.setProperty(FulltextIndexConstants.PROP_NAME, FulltextIndexConstants.REGEX_ALL_PROPS);
+        allProps.setProperty(FulltextIndexConstants.PROP_IS_REGEX, true);
+
+        root.commit();
+    }
+
+    @Test
+    public void getAllSelectedColumns() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "is fox ifoxing");
+        contentRoot.setProperty("bar", "ifoxing fox");
+        contentRoot.setProperty("baz", "fox ifoxing");
+        root.commit();
+
+        List<String> columns = newArrayList("rep:excerpt", "rep:excerpt(.)", "rep:excerpt(foo)", "rep:excerpt(bar)");
+        String selectColumns = Joiner.on(",").join(
+                columns.stream().map(col -> "[" + col + "]").collect(Collectors.toList())
+        );
+        assertEventually(() -> {
+            String query = "SELECT " + selectColumns + " FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+            Result result = null;

Review Comment:
   null init is redundant
   
   ```suggestion
               Result result;
   ```



##########
oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/index/ElasticDocument.java:
##########
@@ -147,13 +155,18 @@ public String build() {
                 if (!similarityTags.isEmpty()) {
                     builder.field(ElasticIndexDefinition.SIMILARITY_TAGS, similarityTags);
                 }
+                for (Map.Entry<String, Set<Object>> prop : toStoreInSource.entrySet()) {
+                    if (!properties.containsKey(prop.getKey())) {
+                        builder.field(prop.getKey(), prop.getValue());
+                    }
+                }

Review Comment:
   I guess this was made to support regex properties. Although it would work, we should refrain to do that because it will go against the strict mapping we need to soon support.
   
   This is not just a problem related to the excerpt feature but a more general one (eg: regex properties, function properties).
   
   My proposal is to simplify this PR by adding excerpt support for specific properties only (we need to document the difference) and have a separate PR to cover the dynamic properties and aim for a NON-dynamic mapping.



##########
oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/index/ElasticDocument.java:
##########
@@ -28,12 +28,7 @@
 import org.slf4j.LoggerFactory;
 
 import java.io.IOException;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.LinkedHashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;

Review Comment:
   do not use * imports



##########
oak-search/src/test/java/org/apache/jackrabbit/oak/plugins/index/ExcerptTest.java:
##########
@@ -0,0 +1,340 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.jackrabbit.oak.plugins.index;
+
+import com.google.common.base.Joiner;
+import org.apache.jackrabbit.JcrConstants;
+import org.apache.jackrabbit.oak.api.*;
+import org.apache.jackrabbit.oak.plugins.index.search.FulltextIndexConstants;
+import org.apache.jackrabbit.oak.plugins.index.search.IndexFormatVersion;
+import org.apache.jackrabbit.oak.plugins.memory.ArrayBasedBlob;
+import org.apache.jackrabbit.oak.query.AbstractQueryTest;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.text.ParseException;
+import java.util.Iterator;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import static com.google.common.collect.Lists.newArrayList;
+import static org.apache.jackrabbit.oak.api.QueryEngine.NO_BINDINGS;
+import static org.apache.jackrabbit.oak.api.Type.STRING;
+import static org.apache.jackrabbit.oak.plugins.index.IndexConstants.*;
+import static org.junit.Assert.*;
+
+public abstract class ExcerptTest extends AbstractQueryTest {
+
+    protected IndexOptions indexOptions;
+    protected TestRepository repositoryOptionsUtil;
+
+    protected void assertEventually(Runnable r) {
+        TestUtils.assertEventually(r,
+                ((repositoryOptionsUtil.isAsync() ? repositoryOptionsUtil.defaultAsyncIndexingTimeInSeconds : 0) + 3000) * 5);
+    }
+
+    @Before
+    public void setup() throws Exception { //named so that it gets called after super.before :-/
+        Tree rootTree = root.getTree("/");
+
+        Tree def = rootTree.addChild(INDEX_DEFINITIONS_NAME).addChild("testExcerpt");
+        def.setProperty(JcrConstants.JCR_PRIMARYTYPE, INDEX_DEFINITIONS_NODE_TYPE, Type.NAME);
+        def.setProperty(TYPE_PROPERTY_NAME, indexOptions.getIndexType());
+        def.setProperty(REINDEX_PROPERTY_NAME, true);
+        def.setProperty(FulltextIndexConstants.EVALUATE_PATH_RESTRICTION, true);
+        def.setProperty(FulltextIndexConstants.COMPAT_MODE, IndexFormatVersion.V2.getVersion());
+
+        Tree properties = def.addChild(FulltextIndexConstants.INDEX_RULES)
+                .addChild("nt:base")
+                .addChild(FulltextIndexConstants.PROP_NODE);
+
+        Tree notIndexedProp = properties.addChild("baz");
+        notIndexedProp.setProperty(FulltextIndexConstants.PROP_NODE_SCOPE_INDEX, true);
+
+        Tree relativeProp = properties.addChild("relative-baz");
+        relativeProp.setProperty(FulltextIndexConstants.PROP_ANALYZED, true);
+        relativeProp.setProperty(FulltextIndexConstants.PROP_USE_IN_EXCERPT, true);
+        relativeProp.setProperty(FulltextIndexConstants.PROP_NAME, "relative/baz");
+
+        Tree allProps = properties.addChild("allProps");
+        allProps.setProperty(FulltextIndexConstants.PROP_ANALYZED, true);
+        allProps.setProperty(FulltextIndexConstants.PROP_NODE_SCOPE_INDEX, true);
+        allProps.setProperty(FulltextIndexConstants.PROP_USE_IN_EXCERPT, true);
+        allProps.setProperty(FulltextIndexConstants.PROP_NAME, FulltextIndexConstants.REGEX_ALL_PROPS);
+        allProps.setProperty(FulltextIndexConstants.PROP_IS_REGEX, true);
+
+        root.commit();
+    }
+
+    @Test
+    public void getAllSelectedColumns() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "is fox ifoxing");
+        contentRoot.setProperty("bar", "ifoxing fox");
+        contentRoot.setProperty("baz", "fox ifoxing");
+        root.commit();
+
+        List<String> columns = newArrayList("rep:excerpt", "rep:excerpt(.)", "rep:excerpt(foo)", "rep:excerpt(bar)");
+        String selectColumns = Joiner.on(",").join(
+                columns.stream().map(col -> "[" + col + "]").collect(Collectors.toList())
+        );
+        assertEventually(() -> {
+            String query = "SELECT " + selectColumns + " FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue excerptValue;
+                String excerpt;
+
+                for (String col : columns) {
+                    excerptValue = firstRow.getValue(col);
+                    assertNotNull(col + " not evaluated", excerptValue);
+                    excerpt = excerptValue.getValue(STRING);
+                    assertFalse(col + " didn't evaluate correctly - got '" + excerpt + "'",
+                            excerpt.contains("i<strong>fox</foxing>ing"));
+                }
+            } catch (ParseException e) {
+                fail(e.getMessage());
+            }
+        });
+    }
+
+    @Test
+    public void nodeExcerpt() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "is fox ifoxing");
+        contentRoot.setProperty("bar", "ifoxing fox");
+        root.commit();
+
+        String query = "SELECT [rep:excerpt],[rep:excerpt(.)] FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+        assertEventually(() -> {
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue nodeExcerpt;
+                String excerpt1, excerpt2;
+
+
+                nodeExcerpt = firstRow.getValue("rep:excerpt");
+                assertNotNull("rep:excerpt not evaluated", nodeExcerpt);
+                excerpt1 = nodeExcerpt.getValue(STRING);
+                assertTrue("rep:excerpt didn't evaluate correctly - got '" + excerpt1 + "'",
+                        "is <strong>fox</strong> ifoxing".equals(excerpt1) || "ifoxing <strong>fox</strong>".equals(excerpt1));
+
+                nodeExcerpt = firstRow.getValue("rep:excerpt(.)");
+                assertNotNull("rep:excerpt(.) not evaluated", nodeExcerpt);
+                excerpt2 = nodeExcerpt.getValue(STRING);
+                assertEquals("excerpt extracted via rep:excerpt not same as rep:excerpt(.)", excerpt1, excerpt2);
+            } catch (ParseException e) {
+                fail(e.getMessage());
+            }
+        });
+    }
+
+    @Test
+    public void nonIndexedRequestedPropExcerpt() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "fox");
+        contentRoot.setProperty("baz", "is fox ifoxing");
+        root.commit();
+
+        assertEventually(() -> {
+            String query = "SELECT [rep:excerpt(baz)] FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue nodeExcerpt = firstRow.getValue("rep:excerpt(baz)");
+                assertNull("rep:excerpt(baz) if requested explicitly must be indexed to be evaluated", nodeExcerpt);
+            } catch (ParseException e) {
+                fail(e.getMessage());
+            }
+        });
+    }
+
+    @Test
+    public void propExcerpt() throws Exception {
+        Tree contentRoot = root.getTree("/").addChild("testRoot");
+        contentRoot.setProperty("foo", "is fox ifoxing");
+        root.commit();
+
+        String query = "SELECT [rep:excerpt(foo)] FROM [nt:base] WHERE CONTAINS(*, 'fox')";
+        assertEventually(() -> {
+            Result result = null;
+            try {
+                result = executeQuery(query, SQL2, NO_BINDINGS);
+                Iterator<? extends ResultRow> resultIter = result.getRows().iterator();
+                assertTrue(resultIter.hasNext());
+                ResultRow firstRow = resultIter.next();
+
+                PropertyValue nodeExcerpt;
+                String excerpt;
+
+                nodeExcerpt = firstRow.getValue("rep:excerpt(foo)");
+                assertNotNull("rep:excerpt(foo) not evaluated", nodeExcerpt);
+                excerpt = nodeExcerpt.getValue(STRING);
+                assertTrue("rep:excerpt(foo) didn't evaluate correctly - got '" + excerpt + "'",
+                        "is <strong>fox</strong> ifoxing".equals(excerpt));

Review Comment:
   this can be simplified 
   
   ```suggestion
                   assertEquals("rep:excerpt(foo) didn't evaluate correctly - got '" + excerpt + "'", 
                           "is <strong>fox</strong> ifoxing", excerpt);
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@jackrabbit.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org