This is an automated email from the ASF dual-hosted git repository. fortino pushed a commit to branch trunk in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git
The following commit(s) were added to refs/heads/trunk by this push: new e44e9c62fd OAK-10365: introduce mapping version in elastic index definition (#1035) e44e9c62fd is described below commit e44e9c62fd4b2a2c624c72c210f7b2b4bce51c46 Author: Fabrizio Fortino <fabrizio.fort...@gmail.com> AuthorDate: Tue Jul 25 10:13:56 2023 +0200 OAK-10365: introduce mapping version in elastic index definition (#1035) * OAK-10365: introduce mapping version in elastic index definition * OAK-10365: minor improvements --- .../index/elastic/ElasticIndexDefinition.java | 13 +++++++ .../index/elastic/index/ElasticIndexHelper.java | 12 ++++++- .../index/elastic/index/ElasticIndexWriter.java | 2 ++ .../index/elastic/ElasticAbstractQueryTest.java | 6 ++-- .../index/elastic/index/ElasticIndexTest.java | 40 ++++++++++++++++++++++ 5 files changed, 69 insertions(+), 4 deletions(-) diff --git a/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticIndexDefinition.java b/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticIndexDefinition.java index 31b82d29e7..61656c4296 100644 --- a/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticIndexDefinition.java +++ b/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticIndexDefinition.java @@ -68,6 +68,11 @@ public class ElasticIndexDefinition extends IndexDefinition { public static final String TRACK_TOTAL_HITS = "trackTotalHits"; public static final Integer TRACK_TOTAL_HITS_DEFAULT = 10000; + /** + * Hidden property for storing the index mapping version. + */ + public static final String PROP_INDEX_MAPPING_VERSION = ":mappingVersion"; + public static final String DYNAMIC_MAPPING = "dynamicMapping"; // possible values are: true, false, runtime, strict. See https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic.html public static final String DYNAMIC_MAPPING_DEFAULT = "true"; @@ -269,6 +274,14 @@ public class ElasticIndexDefinition extends IndexDefinition { return getOptionalValue(analyzersTree, SPLIT_ON_NUMERICS, false); } + /** + * Returns the mapping version for this index definition. + * If the version is not specified, the default value is {@code 1.0.0}. + */ + public String getMappingVersion() { + return getOptionalValue(definition, PROP_INDEX_MAPPING_VERSION, "1.0.0"); + } + @Override protected PropertyDefinition createPropertyDefinition(IndexDefinition.IndexingRule rule, String name, NodeState nodeState) { return new ElasticPropertyDefinition(rule, name, nodeState); diff --git a/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/index/ElasticIndexHelper.java b/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/index/ElasticIndexHelper.java index 63afc7c9df..187f49beee 100644 --- a/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/index/ElasticIndexHelper.java +++ b/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/index/ElasticIndexHelper.java @@ -46,6 +46,16 @@ import java.util.stream.Collectors; */ class ElasticIndexHelper { + /** + * Mapping version that uses <a href="https://semver.org/">SemVer Specification</a> to allow changes without + * breaking existing queries. + * Changes breaking compatibility should increment the major version (indicating that a reindex is mandatory). + * Changes not breaking compatibility should increment the minor version (old queries still work, but they might not + * use the new feature). + * Changes that do not affect queries should increment the patch version (eg: bug fixes). + */ + protected static final String MAPPING_VERSION = "1.0.0"; + // Unset the refresh interval and disable replicas at index creation to optimize for initial loads // https://www.elastic.co/guide/en/elasticsearch/reference/current/tune-for-indexing-speed.html private static final Time INITIAL_REFRESH_INTERVAL = Time.of(b -> b.time("-1")); @@ -271,7 +281,7 @@ class ElasticIndexHelper { " \"similarity\": \"" + pd.getSimilaritySearchParameters().getIndexTimeSimilarityFunction() + "\"," + " \"L\": " + pd.getSimilaritySearchParameters().getL() + "," + " \"k\": " + pd.getSimilaritySearchParameters().getK() + "," + - " \"w\": " + pd.getSimilaritySearchParameters().getW() + "" + + " \"w\": " + pd.getSimilaritySearchParameters().getW() + " }" + "}"); diff --git a/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/index/ElasticIndexWriter.java b/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/index/ElasticIndexWriter.java index 9cdf8d2b59..3ae8ec3e3d 100644 --- a/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/index/ElasticIndexWriter.java +++ b/oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/index/ElasticIndexWriter.java @@ -90,6 +90,8 @@ class ElasticIndexWriter implements FulltextIndexWriter<ElasticDocument> { long seed = UUID.randomUUID().getMostSignificantBits(); // merge gets called on node store later in the indexing flow definitionBuilder.setProperty(ElasticIndexDefinition.PROP_INDEX_NAME_SEED, seed); + // let's store the current mapping version in the index definition + definitionBuilder.setProperty(ElasticIndexDefinition.PROP_INDEX_MAPPING_VERSION, ElasticIndexHelper.MAPPING_VERSION); indexName = ElasticIndexNameHelper. getRemoteIndexName(elasticConnection.getIndexPrefix(), indexDefinition.getIndexPath(), seed); diff --git a/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticAbstractQueryTest.java b/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticAbstractQueryTest.java index 57fd31b002..976ae264dd 100644 --- a/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticAbstractQueryTest.java +++ b/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticAbstractQueryTest.java @@ -94,8 +94,8 @@ public abstract class ElasticAbstractQueryTest extends AbstractQueryTest { } /* - Override this to create some other repo initializer if needed - // Make sure to call super.initialize(builder) + * Override this to create some other repo initializer if needed + * Make sure to call super.initialize(builder) */ protected InitialContent getInitialContent() { return new InitialContent() { @@ -255,7 +255,7 @@ public abstract class ElasticAbstractQueryTest extends AbstractQueryTest { } } - private ElasticIndexDefinition getElasticIndexDefinition(Tree index) { + protected ElasticIndexDefinition getElasticIndexDefinition(Tree index) { return new ElasticIndexDefinition( nodeStore.getRoot(), nodeStore.getRoot().getChildNode(INDEX_DEFINITIONS_NAME).getChildNode(index.getName()), diff --git a/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elastic/index/ElasticIndexTest.java b/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elastic/index/ElasticIndexTest.java new file mode 100644 index 0000000000..92309e7dd6 --- /dev/null +++ b/oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elastic/index/ElasticIndexTest.java @@ -0,0 +1,40 @@ +/* + * 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.elastic.index; + +import org.apache.jackrabbit.oak.api.Tree; +import org.apache.jackrabbit.oak.plugins.index.elastic.ElasticAbstractQueryTest; +import org.apache.jackrabbit.oak.plugins.index.search.util.IndexDefinitionBuilder; +import org.junit.Test; + +import java.util.UUID; + +import static org.junit.Assert.assertEquals; + +public class ElasticIndexTest extends ElasticAbstractQueryTest { + + @Test + public void indexStoresMappingVersion() throws Exception { + IndexDefinitionBuilder builder = createIndex("a").noAsync(); + builder.indexRule("nt:base").property("a").propertyIndex(); + Tree index = setIndex(UUID.randomUUID().toString(), builder); + root.commit(); + + assertEventually(() -> assertEquals(ElasticIndexHelper.MAPPING_VERSION, + getElasticIndexDefinition(index).getMappingVersion())); + } +}