use of com.apple.foundationdb.record.provider.foundationdb.indexes.TextIndexTestUtils.COMPLEX_DOC in project fdb-record-layer by FoundationDB.
the class LuceneIndexTest method searchForAutoCompleteCrossingMultipleFields.
@Test
void searchForAutoCompleteCrossingMultipleFields() throws Exception {
try (FDBRecordContext context = openContext()) {
openRecordStore(context, metaDataBuilder -> {
metaDataBuilder.removeIndex(TextIndexTestUtils.SIMPLE_DEFAULT_NAME);
metaDataBuilder.addIndex(COMPLEX_DOC, COMPLEX_MULTIPLE_TEXT_INDEXES_WITH_AUTO_COMPLETE);
});
// Write 8 texts and 6 of them contain the key "good"
recordStore.saveRecord(createComplexDocument(1623L, "Good morning", "", 1));
recordStore.saveRecord(createComplexDocument(1624L, "Good afternoon", "", 1));
recordStore.saveRecord(createComplexDocument(1625L, "good evening", "", 1));
recordStore.saveRecord(createComplexDocument(1626L, "Good night", "", 1));
recordStore.saveRecord(createComplexDocument(1627L, "", "That's really good!", 1));
recordStore.saveRecord(createComplexDocument(1628L, "", "I'm good", 1));
recordStore.saveRecord(createComplexDocument(1629L, "", "Hello Record Layer", 1));
RecordType recordType = recordStore.saveRecord(createComplexDocument(1630L, "", "Hello FoundationDB!", 1)).getRecordType();
List<IndexEntry> results = recordStore.scanIndex(COMPLEX_MULTIPLE_TEXT_INDEXES_WITH_AUTO_COMPLETE, IndexScanType.BY_LUCENE_AUTO_COMPLETE, TupleRange.allOf(Tuple.from("good")), null, ScanProperties.FORWARD_SCAN).asList().get();
results.stream().forEach(i -> assertDocumentPartialRecordFromIndexEntry(recordType, i, (String) i.getKey().get(i.getKeySize() - 1), (String) i.getKey().get(i.getKeySize() - 2), IndexScanType.BY_LUCENE_AUTO_COMPLETE));
assertEquals(6, context.getTimer().getCounter(FDBStoreTimer.Counts.LUCENE_SCAN_MATCHED_AUTO_COMPLETE_SUGGESTIONS).getCount());
assertEntriesAndSegmentInfoStoredInCompoundFile(AutoCompleteSuggesterCommitCheckAsync.getSuggestionIndexSubspace(recordStore.indexSubspace(COMPLEX_MULTIPLE_TEXT_INDEXES_WITH_AUTO_COMPLETE), TupleHelpers.EMPTY), context, "_0.cfs", true);
// Assert the count of suggestions
assertEquals(6, results.size());
// Assert the suggestions' keys
List<String> suggestions = results.stream().map(i -> (String) i.getKey().get(i.getKeySize() - 1)).collect(Collectors.toList());
assertEquals(ImmutableList.of("good evening", "Good night", "Good morning", "Good afternoon", "I'm good", "That's really good!"), suggestions);
// Assert the corresponding field for the suggestions
List<String> fields = results.stream().map(i -> (String) i.getKey().get(i.getKeySize() - 2)).collect(Collectors.toList());
assertEquals(ImmutableList.of("text", "text", "text", "text", "text2", "text2"), fields);
// Assert the suggestions are sorted according to their values, which are determined by the position of the term into the indexed text
List<Long> values = results.stream().map(i -> (Long) i.getValue().get(0)).collect(Collectors.toList());
List<Long> valuesSorted = new ArrayList<>(values);
Collections.sort(valuesSorted, Collections.reverseOrder());
assertEquals(valuesSorted, values);
assertEquals(values.get(0), values.get(1));
assertEquals(values.get(1), values.get(2));
assertEquals(values.get(2), values.get(3));
assertTrue(values.get(3) > values.get(4));
assertTrue(values.get(4) > values.get(5));
}
}
Aggregations