You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by ng...@apache.org on 2020/03/26 11:02:42 UTC

svn commit: r1875701 [2/2] - in /jackrabbit/oak/trunk/oak-search-elastic: ./ src/main/java/org/apache/jackrabbit/oak/plugins/index/elasticsearch/ src/main/java/org/apache/jackrabbit/oak/plugins/index/elasticsearch/index/ src/main/java/org/apache/jackra...

Modified: jackrabbit/oak/trunk/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elasticsearch/query/ElasticsearchSearcher.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elasticsearch/query/ElasticsearchSearcher.java?rev=1875701&r1=1875700&r2=1875701&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elasticsearch/query/ElasticsearchSearcher.java (original)
+++ jackrabbit/oak/trunk/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elasticsearch/query/ElasticsearchSearcher.java Thu Mar 26 11:02:41 2020
@@ -16,14 +16,11 @@
  */
 package org.apache.jackrabbit.oak.plugins.index.elasticsearch.query;
 
-import org.apache.jackrabbit.oak.plugins.index.elasticsearch.ElasticsearchIndexCoordinateFactory;
-import org.apache.jackrabbit.oak.plugins.index.elasticsearch.ElasticsearchIndexCoordinate;
-import org.apache.jackrabbit.oak.plugins.index.elasticsearch.ElasticsearchIndexDefinition;
+import org.apache.jackrabbit.oak.plugins.index.elasticsearch.ElasticsearchIndexDescriptor;
 import org.apache.jackrabbit.oak.plugins.index.search.FieldNames;
 import org.elasticsearch.action.search.SearchRequest;
 import org.elasticsearch.action.search.SearchResponse;
 import org.elasticsearch.client.RequestOptions;
-import org.elasticsearch.client.RestHighLevelClient;
 import org.elasticsearch.index.query.QueryBuilder;
 import org.elasticsearch.search.builder.SearchSourceBuilder;
 import org.jetbrains.annotations.NotNull;
@@ -31,14 +28,10 @@ import org.jetbrains.annotations.NotNull
 import java.io.IOException;
 
 public class ElasticsearchSearcher {
-    private final ElasticsearchIndexCoordinate esIndexCoord;
-    private final RestHighLevelClient client;
+    private final ElasticsearchIndexDescriptor indexDescriptor;
 
-    ElasticsearchSearcher(@NotNull ElasticsearchIndexCoordinateFactory esIndexCoordFactory,
-                          @NotNull ElasticsearchIndexNode indexNode) {
-        ElasticsearchIndexDefinition defn = indexNode.getDefinition();
-        esIndexCoord = esIndexCoordFactory.getElasticsearchIndexCoordinate(defn);
-        client = esIndexCoord.getClient();
+    ElasticsearchSearcher(@NotNull ElasticsearchIndexNode indexNode) {
+        indexDescriptor = indexNode.getIndexDescriptor();
     }
 
     public SearchResponse search(QueryBuilder query, int batchSize) throws IOException {
@@ -46,12 +39,11 @@ public class ElasticsearchSearcher {
                 .query(query)
                 .fetchSource(false)
                 .storedField(FieldNames.PATH)
-                .size(batchSize)
-                ;
+                .size(batchSize);
 
-        SearchRequest request = new SearchRequest(esIndexCoord.getEsIndexName())
+        SearchRequest request = new SearchRequest(indexDescriptor.getIndexName())
                 .source(searchSourceBuilder);
 
-        return client.search(request, RequestOptions.DEFAULT);
+        return indexDescriptor.getClient().search(request, RequestOptions.DEFAULT);
     }
 }

Modified: jackrabbit/oak/trunk/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elasticsearch/util/TermQueryBuilderFactory.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elasticsearch/util/TermQueryBuilderFactory.java?rev=1875701&r1=1875700&r2=1875701&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elasticsearch/util/TermQueryBuilderFactory.java (original)
+++ jackrabbit/oak/trunk/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elasticsearch/util/TermQueryBuilderFactory.java Thu Mar 26 11:02:41 2020
@@ -17,16 +17,20 @@
 package org.apache.jackrabbit.oak.plugins.index.elasticsearch.util;
 
 import org.apache.jackrabbit.oak.api.PropertyValue;
-import org.apache.jackrabbit.oak.api.Type;
 import org.apache.jackrabbit.oak.commons.PathUtils;
 import org.apache.jackrabbit.oak.plugins.index.search.FieldNames;
-import org.apache.jackrabbit.oak.plugins.index.search.PropertyDefinition;
 import org.apache.jackrabbit.oak.plugins.index.search.spi.query.FulltextIndexPlanner;
 import org.apache.jackrabbit.oak.spi.query.Filter;
-import org.elasticsearch.index.query.*;
+import org.elasticsearch.index.query.BoolQueryBuilder;
+import org.elasticsearch.index.query.ExistsQueryBuilder;
+import org.elasticsearch.index.query.PrefixQueryBuilder;
+import org.elasticsearch.index.query.QueryBuilder;
+import org.elasticsearch.index.query.QueryBuilders;
+import org.elasticsearch.index.query.RangeQueryBuilder;
+import org.elasticsearch.index.query.TermQueryBuilder;
+import org.elasticsearch.index.query.WildcardQueryBuilder;
 import org.jetbrains.annotations.NotNull;
 
-import javax.jcr.PropertyType;
 import java.util.List;
 import java.util.function.Function;
 import java.util.stream.Collectors;
@@ -35,9 +39,11 @@ import static org.apache.jackrabbit.JcrC
 import static org.apache.jackrabbit.JcrConstants.JCR_PRIMARYTYPE;
 import static org.apache.jackrabbit.oak.plugins.index.search.FieldNames.PATH;
 import static org.apache.jackrabbit.oak.plugins.index.search.FieldNames.PATH_DEPTH;
-import static org.elasticsearch.index.query.QueryBuilders.*;
+import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
+import static org.elasticsearch.index.query.QueryBuilders.prefixQuery;
+import static org.elasticsearch.index.query.QueryBuilders.termQuery;
+import static org.elasticsearch.index.query.QueryBuilders.wildcardQuery;
 
-@SuppressWarnings("SpellCheckingInspection")
 public class TermQueryBuilderFactory {
     /**
      * Private constructor.
@@ -80,7 +86,7 @@ public class TermQueryBuilderFactory {
         return wildcardQuery(PATH, value);
     }
 
-    public static TermQueryBuilder newAncestorQuery(String path){
+    public static TermQueryBuilder newAncestorQuery(String path) {
         return termQuery(FieldNames.ANCESTORS, preparePath(path));
     }
 
@@ -144,7 +150,7 @@ public class TermQueryBuilderFactory {
             return newRangeQuery(propertyName, null, last, true, pr.lastIncluding);
         } else if (pr.list != null) {
             return newInQuery(propertyName, pr.list.stream()
-                    .map(propToObj::apply)
+                    .map(propToObj)
                     .collect(Collectors.toList()));
         } else if (pr.isNotNullRestriction()) {
             // not null. For date lower bound of zero can be used

Added: jackrabbit/oak/trunk/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elasticsearch/ElasticsearchConnectionTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elasticsearch/ElasticsearchConnectionTest.java?rev=1875701&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elasticsearch/ElasticsearchConnectionTest.java (added)
+++ jackrabbit/oak/trunk/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elasticsearch/ElasticsearchConnectionTest.java Thu Mar 26 11:02:41 2020
@@ -0,0 +1,31 @@
+package org.apache.jackrabbit.oak.plugins.index.elasticsearch;
+
+import org.elasticsearch.client.RestHighLevelClient;
+import org.junit.Test;
+
+import java.io.IOException;
+
+import static org.junit.Assert.assertEquals;
+
+public class ElasticsearchConnectionTest {
+
+    @Test
+    public void uniqueClient() throws IOException {
+        ElasticsearchConnection connection = ElasticsearchConnection.defaultConnection.get();
+
+        RestHighLevelClient client1 = connection.getClient();
+        RestHighLevelClient client2 = connection.getClient();
+
+        assertEquals(client1, client2);
+
+        connection.close();
+    }
+
+    @Test(expected = IllegalStateException.class)
+    public void alreadyClosedConnection() throws IOException {
+        ElasticsearchConnection connection = ElasticsearchConnection.defaultConnection.get();
+        connection.close();
+
+        connection.getClient();
+    }
+}

Added: jackrabbit/oak/trunk/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elasticsearch/ElasticsearchIndexProviderServiceTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elasticsearch/ElasticsearchIndexProviderServiceTest.java?rev=1875701&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elasticsearch/ElasticsearchIndexProviderServiceTest.java (added)
+++ jackrabbit/oak/trunk/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elasticsearch/ElasticsearchIndexProviderServiceTest.java Thu Mar 26 11:02:41 2020
@@ -0,0 +1,62 @@
+package org.apache.jackrabbit.oak.plugins.index.elasticsearch;
+
+import org.apache.jackrabbit.oak.osgi.OsgiWhiteboard;
+import org.apache.jackrabbit.oak.plugins.index.IndexEditorProvider;
+import org.apache.jackrabbit.oak.plugins.memory.MemoryNodeStore;
+import org.apache.jackrabbit.oak.spi.mount.MountInfoProvider;
+import org.apache.jackrabbit.oak.spi.mount.Mounts;
+import org.apache.jackrabbit.oak.spi.query.QueryIndexProvider;
+import org.apache.jackrabbit.oak.spi.state.NodeStore;
+import org.apache.jackrabbit.oak.spi.whiteboard.Whiteboard;
+import org.apache.jackrabbit.oak.spi.whiteboard.WhiteboardUtils;
+import org.apache.jackrabbit.oak.stats.StatisticsProvider;
+import org.apache.sling.testing.mock.osgi.MockOsgi;
+import org.apache.sling.testing.mock.osgi.junit.OsgiContext;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import java.io.File;
+import java.util.Collections;
+
+import static org.junit.Assert.assertNotNull;
+
+public class ElasticsearchIndexProviderServiceTest {
+
+    @Rule
+    public final TemporaryFolder folder = new TemporaryFolder(new File("target"));
+
+    @Rule
+    public final OsgiContext context = new OsgiContext();
+
+    private final ElasticsearchIndexProviderService service = new ElasticsearchIndexProviderService();
+
+    private Whiteboard wb;
+
+    @Before
+    public void setUp() {
+        MountInfoProvider mip = Mounts.newBuilder().build();
+        context.registerService(MountInfoProvider.class, mip);
+        context.registerService(NodeStore.class, new MemoryNodeStore());
+        context.registerService(StatisticsProvider.class, StatisticsProvider.NOOP);
+
+        wb = new OsgiWhiteboard(context.bundleContext());
+        MockOsgi.injectServices(service, context.bundleContext());
+    }
+
+    @Test
+    public void defaultSetup() throws Exception {
+        MockOsgi.activate(service, context.bundleContext(),
+                Collections.singletonMap("localTextExtractionDir", folder.newFolder("localTextExtractionDir").getAbsolutePath())
+        );
+
+        assertNotNull(context.getService(QueryIndexProvider.class));
+        assertNotNull(context.getService(IndexEditorProvider.class));
+
+        assertNotNull(WhiteboardUtils.getServices(wb, Runnable.class));
+
+        MockOsgi.deactivate(service, context.bundleContext());
+    }
+
+}

Modified: jackrabbit/oak/trunk/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elasticsearch/ElasticsearchPropertyIndexTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elasticsearch/ElasticsearchPropertyIndexTest.java?rev=1875701&r1=1875700&r2=1875701&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elasticsearch/ElasticsearchPropertyIndexTest.java (original)
+++ jackrabbit/oak/trunk/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elasticsearch/ElasticsearchPropertyIndexTest.java Thu Mar 26 11:02:41 2020
@@ -33,30 +33,36 @@ import org.apache.jackrabbit.oak.query.A
 import org.apache.jackrabbit.oak.spi.security.OpenSecurityProvider;
 import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
 import org.apache.jackrabbit.oak.spi.state.NodeStore;
+import org.elasticsearch.Version;
 import org.junit.Assert;
-import org.junit.Rule;
+import org.junit.ClassRule;
 import org.junit.Test;
+import org.testcontainers.elasticsearch.ElasticsearchContainer;
 
 import java.util.Arrays;
-import java.util.Collections;
-import java.util.Set;
 
-import static com.google.common.collect.ImmutableSet.of;
-import static org.apache.derby.vti.XmlVTI.asList;
+import static java.util.Collections.singletonList;
 import static org.apache.jackrabbit.oak.plugins.index.IndexConstants.INDEX_DEFINITIONS_NAME;
 import static org.apache.jackrabbit.oak.plugins.index.search.FulltextIndexConstants.PROPDEF_PROP_NODE_NAME;
 import static org.hamcrest.CoreMatchers.containsString;
 import static org.hamcrest.MatcherAssert.assertThat;
 
 public class ElasticsearchPropertyIndexTest extends AbstractQueryTest {
-    @Rule
-    public ElasticsearchManagementRule esMgmt = new ElasticsearchManagementRule();
+
+    @ClassRule
+    public static final ElasticsearchContainer ELASTIC =
+            new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch:" + Version.CURRENT);
 
     @Override
     protected ContentRepository createRepository() {
-        ElasticsearchIndexEditorProvider editorProvider = new ElasticsearchIndexEditorProvider(esMgmt,
-                new ExtractedTextCache(10* FileUtils.ONE_MB, 100));
-        ElasticsearchIndexProvider indexProvider = new ElasticsearchIndexProvider(esMgmt);
+        ElasticsearchConnection coordinate = new ElasticsearchConnection(
+                ElasticsearchConnection.DEFAULT_SCHEME,
+                ELASTIC.getContainerIpAddress(),
+                ELASTIC.getMappedPort(ElasticsearchConnection.DEFAULT_PORT)
+        );
+        ElasticsearchIndexEditorProvider editorProvider = new ElasticsearchIndexEditorProvider(coordinate,
+                new ExtractedTextCache(10 * FileUtils.ONE_MB, 100));
+        ElasticsearchIndexProvider indexProvider = new ElasticsearchIndexProvider(coordinate);
 
         // remove all indexes to avoid cost competition (essentially a TODO for fixing cost ES cost estimation)
         NodeBuilder builder = InitialContentHelper.INITIAL_CONTENT.builder();
@@ -81,8 +87,8 @@ public class ElasticsearchPropertyIndexT
 
     @Test
     public void indexSelection() throws Exception {
-        setIndex("test1", createIndex(of("propa", "propb")));
-        setIndex("test2", createIndex(of("propc")));
+        setIndex("test1", createIndex("propa", "propb"));
+        setIndex("test2", createIndex("propc"));
 
         Tree test = root.getTree("/").addChild("test");
         test.addChild("a").setProperty("propa", "foo");
@@ -96,16 +102,16 @@ public class ElasticsearchPropertyIndexT
         assertThat(explain(propaQuery), containsString("elasticsearch:test1"));
         assertThat(explain("select [jcr:path] from [nt:base] where [propc] = 'foo'"), containsString("elasticsearch:test2"));
 
-        assertQuery(propaQuery, asList("/test/a", "/test/b"));
-        assertQuery("select [jcr:path] from [nt:base] where [propa] = 'foo2'", asList("/test/c"));
-        assertQuery("select [jcr:path] from [nt:base] where [propc] = 'foo'", asList("/test/d"));
+        assertQuery(propaQuery, Arrays.asList("/test/a", "/test/b"));
+        assertQuery("select [jcr:path] from [nt:base] where [propa] = 'foo2'", singletonList("/test/c"));
+        assertQuery("select [jcr:path] from [nt:base] where [propc] = 'foo'", singletonList("/test/d"));
     }
 
     //OAK-3825
     @Test
-    public void nodeNameViaPropDefinition() throws Exception{
+    public void nodeNameViaPropDefinition() throws Exception {
         //make index
-        IndexDefinitionBuilder builder = createIndex(Collections.EMPTY_SET);
+        IndexDefinitionBuilder builder = createIndex();
         builder.includedPaths("/test")
                 .evaluatePathRestrictions()
                 .indexRule("nt:base")
@@ -126,19 +132,19 @@ public class ElasticsearchPropertyIndexT
         String explanation = explain(propabQuery);
         Assert.assertThat(explanation, containsString("elasticsearch:test1(/oak:index/test1) "));
         Assert.assertThat(explanation, containsString("{\"term\":{\":nodeName\":{\"value\":\"foo\","));
-        assertQuery(propabQuery, Arrays.asList("/test/foo"));
-        assertQuery(queryPrefix + "LOCALNAME() = 'bar'", Arrays.asList("/test/sc/bar"));
-        assertQuery(queryPrefix + "LOCALNAME() LIKE 'foo'", Arrays.asList("/test/foo"));
-        assertQuery(queryPrefix + "LOCALNAME() LIKE 'camel%'", Arrays.asList("/test/camelCase"));
-
-        assertQuery(queryPrefix + "NAME() = 'bar'", Arrays.asList("/test/sc/bar"));
-        assertQuery(queryPrefix + "NAME() LIKE 'foo'", Arrays.asList("/test/foo"));
-        assertQuery(queryPrefix + "NAME() LIKE 'camel%'", Arrays.asList("/test/camelCase"));
+        assertQuery(propabQuery, singletonList("/test/foo"));
+        assertQuery(queryPrefix + "LOCALNAME() = 'bar'", singletonList("/test/sc/bar"));
+        assertQuery(queryPrefix + "LOCALNAME() LIKE 'foo'", singletonList("/test/foo"));
+        assertQuery(queryPrefix + "LOCALNAME() LIKE 'camel%'", singletonList("/test/camelCase"));
+
+        assertQuery(queryPrefix + "NAME() = 'bar'", singletonList("/test/sc/bar"));
+        assertQuery(queryPrefix + "NAME() LIKE 'foo'", singletonList("/test/foo"));
+        assertQuery(queryPrefix + "NAME() LIKE 'camel%'", singletonList("/test/camelCase"));
     }
 
     @Test
-    public void emptyIndex() throws Exception{
-        setIndex("test1", createIndex(of("propa", "propb")));
+    public void emptyIndex() throws Exception {
+        setIndex("test1", createIndex("propa", "propb"));
         root.commit();
 
         Tree test = root.getTree("/").addChild("test");
@@ -151,7 +157,7 @@ public class ElasticsearchPropertyIndexT
 
     @Test
     public void propertyExistenceQuery() throws Exception {
-        setIndex("test1", createIndex(of("propa", "propb")));
+        setIndex("test1", createIndex("propa", "propb"));
 
         Tree test = root.getTree("/").addChild("test");
         test.addChild("a").setProperty("propa", "a");
@@ -162,11 +168,12 @@ public class ElasticsearchPropertyIndexT
         assertQuery("select [jcr:path] from [nt:base] where propa is not null", Arrays.asList("/test/a", "/test/b"));
     }
 
-    private static IndexDefinitionBuilder createIndex(Set<String> propNames) {
+    private static IndexDefinitionBuilder createIndex(String... propNames) {
         IndexDefinitionBuilder builder = new ElasticsearchIndexDefinitionBuilder().noAsync();
         IndexDefinitionBuilder.IndexRule indexRule = builder.indexRule("nt:base");
-        propNames.forEach(propName -> indexRule.property(propName).propertyIndex());
-
+        for (String propName : propNames) {
+            indexRule.property(propName).propertyIndex();
+        }
         return builder;
     }
 
@@ -174,7 +181,7 @@ public class ElasticsearchPropertyIndexT
         builder.build(root.getTree("/").addChild(INDEX_DEFINITIONS_NAME).addChild(idxName));
     }
 
-    private String explain(String query){
+    private String explain(String query) {
         String explain = "explain " + query;
         return executeQuery(explain, "JCR-SQL2").get(0);
     }