use of org.apache.lucene.codecs.lucene87.Lucene87Codec in project OpenSearch by opensearch-project.
the class CompletionStatsCacheTests method testCompletionStatsCache.
public void testCompletionStatsCache() throws IOException, InterruptedException {
final IndexWriterConfig indexWriterConfig = newIndexWriterConfig();
final PostingsFormat postingsFormat = new Completion84PostingsFormat();
indexWriterConfig.setCodec(new Lucene87Codec() {
@Override
public PostingsFormat getPostingsFormatForField(String field) {
// all fields are suggest fields
return postingsFormat;
}
});
final QueryCachingPolicy queryCachingPolicy = new QueryCachingPolicy() {
@Override
public void onUse(Query query) {
}
@Override
public boolean shouldCache(Query query) {
return false;
}
};
try (Directory directory = newDirectory();
IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig)) {
final Document document = new Document();
document.add(new SuggestField("suggest1", "val", 1));
document.add(new SuggestField("suggest2", "val", 1));
document.add(new SuggestField("suggest2", "anotherval", 1));
document.add(new SuggestField("otherfield", "val", 1));
document.add(new SuggestField("otherfield", "anotherval", 1));
document.add(new SuggestField("otherfield", "yetmoreval", 1));
indexWriter.addDocument(document);
final OpenCloseCounter openCloseCounter = new OpenCloseCounter();
final CompletionStatsCache completionStatsCache = new CompletionStatsCache(() -> {
openCloseCounter.countOpened();
try {
final DirectoryReader directoryReader = DirectoryReader.open(indexWriter);
return new Engine.Searcher("test", directoryReader, null, null, queryCachingPolicy, () -> {
openCloseCounter.countClosed();
IOUtils.close(directoryReader);
});
} catch (IOException e) {
throw new AssertionError(e);
}
});
final int threadCount = 6;
final TestHarness testHarness = new TestHarness(completionStatsCache, threadCount);
final Thread[] threads = new Thread[threadCount];
threads[0] = new Thread(() -> testHarness.getStats(0, "*"));
threads[1] = new Thread(() -> testHarness.getStats(1, "suggest1", "suggest2"));
threads[2] = new Thread(() -> testHarness.getStats(2, "sug*"));
threads[3] = new Thread(() -> testHarness.getStats(3, "no match*"));
threads[4] = new Thread(() -> testHarness.getStats(4));
threads[5] = new Thread(() -> testHarness.getStats(5, (String[]) null));
for (Thread thread : threads) {
thread.start();
}
testHarness.start();
for (Thread thread : threads) {
thread.join();
}
// 0: "*" should match all fields:
final long suggest1Size = testHarness.getResult(0).getFields().get("suggest1");
final long suggest2Size = testHarness.getResult(0).getFields().get("suggest2");
final long otherFieldSize = testHarness.getResult(0).getFields().get("otherfield");
final long totalSizeInBytes = testHarness.getResult(0).getSizeInBytes();
assertThat(suggest1Size, greaterThan(0L));
assertThat(suggest2Size, greaterThan(0L));
assertThat(otherFieldSize, greaterThan(0L));
assertThat(totalSizeInBytes, equalTo(suggest1Size + suggest2Size + otherFieldSize));
// 1: enumerating fields omits the other ones
assertThat(testHarness.getResult(1).getSizeInBytes(), equalTo(totalSizeInBytes));
assertThat(testHarness.getResult(1).getFields().get("suggest1"), equalTo(suggest1Size));
assertThat(testHarness.getResult(1).getFields().get("suggest2"), equalTo(suggest2Size));
assertFalse(testHarness.getResult(1).getFields().containsField("otherfield"));
// 2: wildcards also exclude some fields
assertThat(testHarness.getResult(2).getSizeInBytes(), equalTo(totalSizeInBytes));
assertThat(testHarness.getResult(2).getFields().get("suggest1"), equalTo(suggest1Size));
assertThat(testHarness.getResult(2).getFields().get("suggest2"), equalTo(suggest2Size));
assertFalse(testHarness.getResult(2).getFields().containsField("otherfield"));
// 3: non-matching wildcard returns empty set of fields
assertThat(testHarness.getResult(3).getSizeInBytes(), equalTo(totalSizeInBytes));
assertFalse(testHarness.getResult(3).getFields().containsField("suggest1"));
assertFalse(testHarness.getResult(3).getFields().containsField("suggest2"));
assertFalse(testHarness.getResult(3).getFields().containsField("otherfield"));
// 4: no fields means per-fields stats is null
assertThat(testHarness.getResult(4).getSizeInBytes(), equalTo(totalSizeInBytes));
assertNull(testHarness.getResult(4).getFields());
// 5: null fields means per-fields stats is null
assertThat(testHarness.getResult(5).getSizeInBytes(), equalTo(totalSizeInBytes));
assertNull(testHarness.getResult(5).getFields());
// the stats were only computed once
openCloseCounter.assertCount(1);
// the stats are not recomputed on a refresh
completionStatsCache.afterRefresh(true);
openCloseCounter.assertCount(1);
// but they are recomputed on the next get
completionStatsCache.get();
openCloseCounter.assertCount(2);
// and they do update
final Document document2 = new Document();
document2.add(new SuggestField("suggest1", "foo", 1));
document2.add(new SuggestField("suggest2", "bar", 1));
document2.add(new SuggestField("otherfield", "baz", 1));
indexWriter.addDocument(document2);
completionStatsCache.afterRefresh(true);
final CompletionStats updatedStats = completionStatsCache.get();
assertThat(updatedStats.getSizeInBytes(), greaterThan(totalSizeInBytes));
openCloseCounter.assertCount(3);
// beforeRefresh does not invalidate the cache
completionStatsCache.beforeRefresh();
completionStatsCache.get();
openCloseCounter.assertCount(3);
// afterRefresh does not invalidate the cache if no refresh took place
completionStatsCache.afterRefresh(false);
completionStatsCache.get();
openCloseCounter.assertCount(3);
}
}
Aggregations