Skip to content

Commit

Permalink
Fix flaky test due to random indexwriter
Browse files Browse the repository at this point in the history
Signed-off-by: Rishabh Maurya <[email protected]>
  • Loading branch information
rishabhmaurya committed Dec 20, 2023
1 parent c43d0ef commit a8b5280
Showing 1 changed file with 25 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@

package org.opensearch.index.query;

import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.opensearch.core.index.Index;
import org.opensearch.index.mapper.MapperService;
import org.opensearch.index.mapper.MapperServiceTestCase;
Expand Down Expand Up @@ -77,13 +82,14 @@ public void testAllPossibleScenarios() throws IOException {
queryShardContext.getFieldType("dessert"),
queryShardContext
);

withLuceneIndex(mapperService, iw -> {
for (ParsedDocument d : docs) {
iw.addDocument(d.rootDoc());
}
}, reader -> {
IndexSearcher searcher = newSearcher(reader);
Directory dir = newDirectory();
IndexWriter iw = new IndexWriter(dir, new IndexWriterConfig(mapperService.indexAnalyzer()));
for (ParsedDocument d : docs) {
iw.addDocument(d.rootDoc());
}
try (IndexReader reader = DirectoryReader.open(iw)) {
iw.close();
IndexSearcher searcher = new IndexSearcher(reader);
TopDocs topDocs = searcher.search(matchBoth, 10);
assertEquals(topDocs.totalHits.value, 1);
assertEquals(topDocs.scoreDocs[0].doc, 0);
Expand All @@ -103,7 +109,8 @@ public void testAllPossibleScenarios() throws IOException {
for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
assertEquals(scoreDoc.score, 1.0, 0.00000000001);
}
});
}
dir.close();
}

public void testSourceDisabled() throws IOException {
Expand Down Expand Up @@ -150,14 +157,17 @@ public void testMissingField() throws IOException {
queryShardContext.getFieldType("dessert"),
queryShardContext
);
withLuceneIndex(mapperService, iw -> {
for (ParsedDocument d : docs) {
iw.addDocument(d.rootDoc());
}
}, reader -> {
IndexSearcher searcher = newSearcher(reader);
Directory dir = newDirectory();
IndexWriter iw = new IndexWriter(dir, new IndexWriterConfig(mapperService.indexAnalyzer()));
for (ParsedDocument d : docs) {
iw.addDocument(d.rootDoc());
}
try (IndexReader reader = DirectoryReader.open(iw)) {
iw.close();
IndexSearcher searcher = new IndexSearcher(reader);
TopDocs topDocs = searcher.search(matchDelegate, 10);
assertEquals(topDocs.totalHits.value, 0);
});
}
dir.close();
}
}

0 comments on commit a8b5280

Please sign in to comment.