Skip to content

Commit

Permalink
Restore WildcardFieldMapperTests#testBWCIndexVersion (#119093)
Browse files Browse the repository at this point in the history
This test has been removed with #112570 but with
the coming read-only support for v7 we need it back.
  • Loading branch information
cbuescher authored Dec 19, 2024
1 parent e504cec commit 9af1402
Showing 1 changed file with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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()
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 9af1402

Please sign in to comment.