From f22631a71ff020a0d3a1787b56b4a151ef13da6b Mon Sep 17 00:00:00 2001 From: Jim Ferenczi Date: Fri, 10 Jan 2020 17:51:56 +0100 Subject: [PATCH] Fix upgrade of custom similarity (#50851) This change fixes the upgrade of index metadata that contain a custom similarity with options that are not compatible with BM25. The upgrade doesn't need a real similarity service so we fake one that resolves all custom similarity to BM25 but this logic fails because the BM25 provider checks that all options are compatible. This commit removes the verification step as it is not needed during the upgrade (the verification is done when the index is restored/opened). Closes #50763 --- .../metadata/MetaDataIndexUpgradeService.java | 3 ++- .../metadata/MetaDataIndexUpgradeServiceTests.java | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/MetaDataIndexUpgradeService.java b/server/src/main/java/org/elasticsearch/cluster/metadata/MetaDataIndexUpgradeService.java index 52ee0be49ef6..9c4696bfa70f 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/MetaDataIndexUpgradeService.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/MetaDataIndexUpgradeService.java @@ -22,6 +22,7 @@ import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.ParameterizedMessage; import org.apache.lucene.analysis.Analyzer; +import org.apache.lucene.search.similarities.BM25Similarity; import org.apache.lucene.search.similarities.Similarity; import org.elasticsearch.Version; import org.elasticsearch.common.TriFunction; @@ -145,7 +146,7 @@ public boolean containsKey(Object key) { @Override public TriFunction get(Object key) { assert key instanceof String : "key must be a string but was: " + key.getClass(); - return SimilarityService.BUILT_IN.get(SimilarityService.DEFAULT_SIMILARITY); + return (settings, version, scriptService) -> new BM25Similarity(); } // this entrySet impl isn't fully correct but necessary as SimilarityService will iterate diff --git a/server/src/test/java/org/elasticsearch/cluster/metadata/MetaDataIndexUpgradeServiceTests.java b/server/src/test/java/org/elasticsearch/cluster/metadata/MetaDataIndexUpgradeServiceTests.java index 32e7a3bcbd87..20deb39c7232 100644 --- a/server/src/test/java/org/elasticsearch/cluster/metadata/MetaDataIndexUpgradeServiceTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/metadata/MetaDataIndexUpgradeServiceTests.java @@ -82,6 +82,18 @@ public void testUpgrade() { assertSame(src, service.upgradeIndexMetaData(src, Version.CURRENT.minimumIndexCompatibilityVersion())); // no double upgrade } + public void testUpgradeCustomSimilarity() { + MetaDataIndexUpgradeService service = getMetaDataIndexUpgradeService(); + IndexMetaData src = newIndexMeta("foo", + Settings.builder() + .put("index.similarity.my_similarity.type", "DFR") + .put("index.similarity.my_similarity.after_effect", "l") + .build()); + assertFalse(service.isUpgraded(src)); + src = service.upgradeIndexMetaData(src, Version.CURRENT.minimumIndexCompatibilityVersion()); + assertTrue(service.isUpgraded(src)); + } + public void testIsUpgraded() { MetaDataIndexUpgradeService service = getMetaDataIndexUpgradeService(); IndexMetaData src = newIndexMeta("foo", Settings.builder().put("index.refresh_interval", "-200").build());