From 9af140211d4f191d68eedcfd7fb3b8fe2e5700b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=BCscher?= Date: Thu, 19 Dec 2024 16:40:27 +0100 Subject: [PATCH] Restore WildcardFieldMapperTests#testBWCIndexVersion (#119093) This test has been removed with #112570 but with the coming read-only support for v7 we need it back. --- .../mapper/WildcardFieldMapperTests.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/x-pack/plugin/wildcard/src/test/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldMapperTests.java b/x-pack/plugin/wildcard/src/test/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldMapperTests.java index a1a01ebdcc590..0b31e96ece84a 100644 --- a/x-pack/plugin/wildcard/src/test/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldMapperTests.java +++ b/x-pack/plugin/wildcard/src/test/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldMapperTests.java @@ -51,6 +51,7 @@ import org.elasticsearch.index.Index; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.IndexVersion; +import org.elasticsearch.index.IndexVersions; import org.elasticsearch.index.cache.bitset.BitsetFilterCache; import org.elasticsearch.index.fielddata.FieldDataContext; import org.elasticsearch.index.fielddata.IndexFieldData; @@ -106,6 +107,7 @@ static SearchExecutionContext createMockSearchExecutionContext(boolean allowExpe static final int MAX_FIELD_LENGTH = 30; static WildcardFieldMapper wildcardFieldType; + static WildcardFieldMapper wildcardFieldType79; static KeywordFieldMapper keywordFieldType; private DirectoryReader rewriteReader; private BaseDirectoryWrapper rewriteDir; @@ -127,6 +129,9 @@ public void setUp() throws Exception { builder.ignoreAbove(MAX_FIELD_LENGTH); wildcardFieldType = builder.build(MapperBuilderContext.root(false, false)); + Builder builder79 = new WildcardFieldMapper.Builder(WILDCARD_FIELD_NAME, IndexVersions.V_7_9_0); + wildcardFieldType79 = builder79.build(MapperBuilderContext.root(false, false)); + org.elasticsearch.index.mapper.KeywordFieldMapper.Builder kwBuilder = new KeywordFieldMapper.Builder( KEYWORD_FIELD_NAME, IndexVersion.current() @@ -207,6 +212,37 @@ public void testIgnoreAbove() throws IOException { assertTrue(fields.stream().anyMatch(field -> "field".equals(field.stringValue()))); } + public void testBWCIndexVersion() throws IOException { + // Create old format index using wildcard ngram analyzer used in 7.9 launch + Directory dir = newDirectory(); + IndexWriterConfig iwc = newIndexWriterConfig(WildcardFieldMapper.WILDCARD_ANALYZER_7_9); + iwc.setMergePolicy(newTieredMergePolicy(random())); + RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwc); + + Document doc = new Document(); + LuceneDocument parseDoc = new LuceneDocument(); + addFields(parseDoc, doc, "a b"); + indexDoc(parseDoc, doc, iw); + + iw.forceMerge(1); + DirectoryReader reader = iw.getReader(); + IndexSearcher searcher = newSearcher(reader); + iw.close(); + + // Unnatural circumstance - testing we fail if we were to use the new analyzer on old index + Query oldWildcardFieldQuery = wildcardFieldType.fieldType().wildcardQuery("a b", null, null); + TopDocs oldWildcardFieldTopDocs = searcher.search(oldWildcardFieldQuery, 10, Sort.INDEXORDER); + assertThat(oldWildcardFieldTopDocs.totalHits.value(), equalTo(0L)); + + // Natural circumstance test we revert to the old analyzer for old indices + Query wildcardFieldQuery = wildcardFieldType79.fieldType().wildcardQuery("a b", null, null); + TopDocs wildcardFieldTopDocs = searcher.search(wildcardFieldQuery, 10, Sort.INDEXORDER); + assertThat(wildcardFieldTopDocs.totalHits.value(), equalTo(1L)); + + reader.close(); + dir.close(); + } + // Test long query strings don't cause exceptions public void testTooBigQueryField() throws IOException { Directory dir = newDirectory();