|
@@ -8,6 +8,7 @@
|
|
|
|
|
|
package org.elasticsearch.action.admin.cluster.stats;
|
|
|
|
|
|
+import org.elasticsearch.Version;
|
|
|
import org.elasticsearch.cluster.metadata.IndexMetadata;
|
|
|
import org.elasticsearch.cluster.metadata.Metadata;
|
|
|
import org.elasticsearch.common.io.stream.Writeable.Reader;
|
|
@@ -15,13 +16,19 @@ import org.elasticsearch.common.settings.Settings;
|
|
|
import org.elasticsearch.index.IndexVersion;
|
|
|
import org.elasticsearch.tasks.TaskCancelledException;
|
|
|
import org.elasticsearch.test.AbstractWireSerializingTestCase;
|
|
|
+import org.elasticsearch.xcontent.XContentType;
|
|
|
|
|
|
import java.util.Collections;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.HashSet;
|
|
|
+import java.util.Map;
|
|
|
import java.util.Set;
|
|
|
+import java.util.TreeMap;
|
|
|
|
|
|
public class AnalysisStatsTests extends AbstractWireSerializingTestCase<AnalysisStats> {
|
|
|
|
|
|
+ private static final String[] SYNONYM_RULES_TYPES = { "synonyms", "synonyms_set", "synonyms_path" };
|
|
|
+
|
|
|
@Override
|
|
|
protected Reader<AnalysisStats> instanceReader() {
|
|
|
return AnalysisStats::new;
|
|
@@ -34,6 +41,20 @@ public class AnalysisStatsTests extends AbstractWireSerializingTestCase<Analysis
|
|
|
return stats;
|
|
|
}
|
|
|
|
|
|
+ private static Map<String, SynonymsStats> randomSynonymsStats() {
|
|
|
+ Map<String, SynonymsStats> result = new HashMap<>();
|
|
|
+ for (String ruleType : SYNONYM_RULES_TYPES) {
|
|
|
+ if (randomBoolean()) {
|
|
|
+ SynonymsStats stats = new SynonymsStats();
|
|
|
+ stats.indexCount = randomIntBetween(1, 10);
|
|
|
+ stats.count = randomIntBetween(stats.indexCount, 100);
|
|
|
+
|
|
|
+ result.put(ruleType, stats);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
protected AnalysisStats createTestInstance() {
|
|
|
Set<IndexFeatureStats> charFilters = new HashSet<>();
|
|
@@ -75,6 +96,8 @@ public class AnalysisStatsTests extends AbstractWireSerializingTestCase<Analysis
|
|
|
if (randomBoolean()) {
|
|
|
builtInAnalyzers.add(randomStats("french"));
|
|
|
}
|
|
|
+ Map<String, SynonymsStats> synonymsStats = randomSynonymsStats();
|
|
|
+
|
|
|
return new AnalysisStats(
|
|
|
charFilters,
|
|
|
tokenizers,
|
|
@@ -83,13 +106,14 @@ public class AnalysisStatsTests extends AbstractWireSerializingTestCase<Analysis
|
|
|
builtInCharFilters,
|
|
|
builtInTokenizers,
|
|
|
builtInTokenFilters,
|
|
|
- builtInAnalyzers
|
|
|
+ builtInAnalyzers,
|
|
|
+ synonymsStats
|
|
|
);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected AnalysisStats mutateInstance(AnalysisStats instance) {
|
|
|
- switch (randomInt(7)) {
|
|
|
+ switch (randomInt(8)) {
|
|
|
case 0 -> {
|
|
|
Set<IndexFeatureStats> charFilters = new HashSet<>(instance.getUsedCharFilterTypes());
|
|
|
if (charFilters.removeIf(s -> s.getName().equals("pattern_replace")) == false) {
|
|
@@ -103,7 +127,8 @@ public class AnalysisStatsTests extends AbstractWireSerializingTestCase<Analysis
|
|
|
instance.getUsedBuiltInCharFilters(),
|
|
|
instance.getUsedBuiltInTokenizers(),
|
|
|
instance.getUsedBuiltInTokenFilters(),
|
|
|
- instance.getUsedBuiltInAnalyzers()
|
|
|
+ instance.getUsedBuiltInAnalyzers(),
|
|
|
+ instance.getUsedSynonyms()
|
|
|
);
|
|
|
}
|
|
|
case 1 -> {
|
|
@@ -119,7 +144,8 @@ public class AnalysisStatsTests extends AbstractWireSerializingTestCase<Analysis
|
|
|
instance.getUsedBuiltInCharFilters(),
|
|
|
instance.getUsedBuiltInTokenizers(),
|
|
|
instance.getUsedBuiltInTokenFilters(),
|
|
|
- instance.getUsedBuiltInAnalyzers()
|
|
|
+ instance.getUsedBuiltInAnalyzers(),
|
|
|
+ instance.getUsedSynonyms()
|
|
|
);
|
|
|
}
|
|
|
case 2 -> {
|
|
@@ -135,7 +161,8 @@ public class AnalysisStatsTests extends AbstractWireSerializingTestCase<Analysis
|
|
|
instance.getUsedBuiltInCharFilters(),
|
|
|
instance.getUsedBuiltInTokenizers(),
|
|
|
instance.getUsedBuiltInTokenFilters(),
|
|
|
- instance.getUsedBuiltInAnalyzers()
|
|
|
+ instance.getUsedBuiltInAnalyzers(),
|
|
|
+ instance.getUsedSynonyms()
|
|
|
);
|
|
|
}
|
|
|
case 3 -> {
|
|
@@ -151,7 +178,8 @@ public class AnalysisStatsTests extends AbstractWireSerializingTestCase<Analysis
|
|
|
instance.getUsedBuiltInCharFilters(),
|
|
|
instance.getUsedBuiltInTokenizers(),
|
|
|
instance.getUsedBuiltInTokenFilters(),
|
|
|
- instance.getUsedBuiltInAnalyzers()
|
|
|
+ instance.getUsedBuiltInAnalyzers(),
|
|
|
+ instance.getUsedSynonyms()
|
|
|
);
|
|
|
}
|
|
|
case 4 -> {
|
|
@@ -167,7 +195,8 @@ public class AnalysisStatsTests extends AbstractWireSerializingTestCase<Analysis
|
|
|
builtInCharFilters,
|
|
|
instance.getUsedBuiltInTokenizers(),
|
|
|
instance.getUsedBuiltInTokenFilters(),
|
|
|
- instance.getUsedBuiltInAnalyzers()
|
|
|
+ instance.getUsedBuiltInAnalyzers(),
|
|
|
+ instance.getUsedSynonyms()
|
|
|
);
|
|
|
}
|
|
|
case 5 -> {
|
|
@@ -183,7 +212,8 @@ public class AnalysisStatsTests extends AbstractWireSerializingTestCase<Analysis
|
|
|
instance.getUsedBuiltInCharFilters(),
|
|
|
builtInTokenizers,
|
|
|
instance.getUsedBuiltInTokenFilters(),
|
|
|
- instance.getUsedBuiltInAnalyzers()
|
|
|
+ instance.getUsedBuiltInAnalyzers(),
|
|
|
+ instance.getUsedSynonyms()
|
|
|
);
|
|
|
}
|
|
|
case 6 -> {
|
|
@@ -199,7 +229,8 @@ public class AnalysisStatsTests extends AbstractWireSerializingTestCase<Analysis
|
|
|
instance.getUsedBuiltInCharFilters(),
|
|
|
instance.getUsedBuiltInTokenizers(),
|
|
|
builtInTokenFilters,
|
|
|
- instance.getUsedBuiltInAnalyzers()
|
|
|
+ instance.getUsedBuiltInAnalyzers(),
|
|
|
+ instance.getUsedSynonyms()
|
|
|
);
|
|
|
}
|
|
|
case 7 -> {
|
|
@@ -215,7 +246,21 @@ public class AnalysisStatsTests extends AbstractWireSerializingTestCase<Analysis
|
|
|
instance.getUsedBuiltInCharFilters(),
|
|
|
instance.getUsedBuiltInTokenizers(),
|
|
|
instance.getUsedBuiltInTokenFilters(),
|
|
|
- builtInAnalyzers
|
|
|
+ builtInAnalyzers,
|
|
|
+ instance.getUsedSynonyms()
|
|
|
+ );
|
|
|
+ }
|
|
|
+ case 8 -> {
|
|
|
+ return new AnalysisStats(
|
|
|
+ instance.getUsedCharFilterTypes(),
|
|
|
+ instance.getUsedTokenizerTypes(),
|
|
|
+ instance.getUsedTokenFilterTypes(),
|
|
|
+ instance.getUsedAnalyzerTypes(),
|
|
|
+ instance.getUsedBuiltInCharFilters(),
|
|
|
+ instance.getUsedBuiltInTokenizers(),
|
|
|
+ instance.getUsedBuiltInTokenFilters(),
|
|
|
+ instance.getUsedBuiltInAnalyzers(),
|
|
|
+ randomValueOtherThan(instance.getUsedSynonyms(), AnalysisStatsTests::randomSynonymsStats)
|
|
|
);
|
|
|
}
|
|
|
default -> throw new AssertionError();
|
|
@@ -280,4 +325,125 @@ public class AnalysisStatsTests extends AbstractWireSerializingTestCase<Analysis
|
|
|
() -> AnalysisStats.of(metadata, () -> { throw new TaskCancelledException("task cancelled"); })
|
|
|
);
|
|
|
}
|
|
|
+
|
|
|
+ public void testSynonymsStats() {
|
|
|
+ final String settingsSourceIndex1 = """
|
|
|
+ {
|
|
|
+ "index": {
|
|
|
+ "analysis": {
|
|
|
+ "filter": {
|
|
|
+ "bigram_max_size": {
|
|
|
+ "type": "length",
|
|
|
+ "max": "16",
|
|
|
+ "min": "0"
|
|
|
+ },
|
|
|
+ "synonyms_inline_filter": {
|
|
|
+ "type": "synonym",
|
|
|
+ "synonyms": ["foo, bar", "bar => baz"]
|
|
|
+ },
|
|
|
+ "other_inline_filter": {
|
|
|
+ "type": "synonym",
|
|
|
+ "synonyms": ["foo, bar, baz"]
|
|
|
+ },
|
|
|
+ "synonyms_path_filter": {
|
|
|
+ "type": "synonym",
|
|
|
+ "synonyms_path": "/a/reused/path"
|
|
|
+ },
|
|
|
+ "other_synonyms_path_filter": {
|
|
|
+ "type": "synonym_graph",
|
|
|
+ "synonyms_path": "/a/different/path"
|
|
|
+ },
|
|
|
+ "another_synonyms_path_filter": {
|
|
|
+ "type": "synonym_graph",
|
|
|
+ "synonyms_path": "/another/different/path"
|
|
|
+ },
|
|
|
+ "synonyms_set_filter": {
|
|
|
+ "type": "synonym_graph",
|
|
|
+ "synonyms_set": "reused-synonym-set"
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ """;
|
|
|
+ Settings settings = indexSettings(Version.CURRENT, 4, 1).loadFromSource(settingsSourceIndex1, XContentType.JSON).build();
|
|
|
+ IndexMetadata indexMetadata1 = new IndexMetadata.Builder("foo").settings(settings).build();
|
|
|
+
|
|
|
+ final String settingsSourceIndex2 = """
|
|
|
+ {
|
|
|
+ "index": {
|
|
|
+ "analysis": {
|
|
|
+ "filter": {
|
|
|
+ "en-stem-filter": {
|
|
|
+ "name": "light_english",
|
|
|
+ "type": "stemmer",
|
|
|
+ "language": "light_english"
|
|
|
+ },
|
|
|
+ "other_synonyms_filter": {
|
|
|
+ "type": "synonym",
|
|
|
+ "synonyms_set": "another-synonym-set"
|
|
|
+ },
|
|
|
+ "a_repeated_synonyms_set_filter": {
|
|
|
+ "type": "synonym",
|
|
|
+ "synonyms_set": "reused-synonym-set"
|
|
|
+ },
|
|
|
+ "repeated_inline_filter": {
|
|
|
+ "type": "synonym",
|
|
|
+ "synonyms": ["foo, bar", "bar => baz"]
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ """;
|
|
|
+ Settings settings2 = indexSettings(Version.CURRENT, 4, 1).loadFromSource(settingsSourceIndex2, XContentType.JSON).build();
|
|
|
+ IndexMetadata indexMetadata2 = new IndexMetadata.Builder("bar").settings(settings2).build();
|
|
|
+
|
|
|
+ final String settingsSourceIndex3 = """
|
|
|
+ {
|
|
|
+ "index": {
|
|
|
+ "analysis": {
|
|
|
+ "filter": {
|
|
|
+ "other_synonyms_filter": {
|
|
|
+ "type": "synonym",
|
|
|
+ "synonyms_set": "a-different-synonym-set"
|
|
|
+ },
|
|
|
+ "a_repeated_synonyms_set_filter": {
|
|
|
+ "type": "synonym",
|
|
|
+ "synonyms_set": "reused-synonym-set"
|
|
|
+ },
|
|
|
+ "more_inline_filter": {
|
|
|
+ "type": "synonym",
|
|
|
+ "synonyms": ["foo, bar", "bar => baz"]
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ """;
|
|
|
+ Settings settings3 = indexSettings(Version.CURRENT, 4, 1).loadFromSource(settingsSourceIndex3, XContentType.JSON).build();
|
|
|
+ IndexMetadata indexMetadata3 = new IndexMetadata.Builder("baz").settings(settings3).build();
|
|
|
+
|
|
|
+ Metadata metadata = new Metadata.Builder().build()
|
|
|
+ .withAddedIndex(indexMetadata1)
|
|
|
+ .withAddedIndex(indexMetadata2)
|
|
|
+ .withAddedIndex(indexMetadata3);
|
|
|
+ AnalysisStats analysisStats = AnalysisStats.of(metadata, () -> {});
|
|
|
+ SynonymsStats expectedSynonymSetStats = new SynonymsStats();
|
|
|
+ expectedSynonymSetStats.count = 3;
|
|
|
+ expectedSynonymSetStats.indexCount = 3;
|
|
|
+ SynonymsStats expectedSynonymPathStats = new SynonymsStats();
|
|
|
+ expectedSynonymPathStats.count = 3;
|
|
|
+ expectedSynonymPathStats.indexCount = 1;
|
|
|
+ SynonymsStats expectedSynonymInlineStats = new SynonymsStats();
|
|
|
+ expectedSynonymInlineStats.count = 4;
|
|
|
+ expectedSynonymInlineStats.indexCount = 3;
|
|
|
+
|
|
|
+ Map<String, SynonymsStats> expectedSynonymStats = new TreeMap<>();
|
|
|
+ expectedSynonymStats.put("synonyms_set", expectedSynonymSetStats);
|
|
|
+ expectedSynonymStats.put("synonyms_path", expectedSynonymPathStats);
|
|
|
+ expectedSynonymStats.put("synonyms", expectedSynonymInlineStats);
|
|
|
+
|
|
|
+ assertEquals(expectedSynonymStats, analysisStats.getUsedSynonyms());
|
|
|
+ }
|
|
|
}
|