use of com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext in project fdb-record-layer by FoundationDB.
the class TextIndexTest method querySimpleDocumentsWithoutPositions.
@Test
public void querySimpleDocumentsWithoutPositions() throws Exception {
final List<SimpleDocument> documents = TextIndexTestUtils.toSimpleDocuments(Arrays.asList(TextSamples.ANGSTROM, TextSamples.AETHELRED, TextSamples.ROMEO_AND_JULIET_PROLOGUE, TextSamples.FRENCH));
// Query but make sure
try (FDBRecordContext context = openContext()) {
openRecordStore(context, metaDataBuilder -> {
metaDataBuilder.removeIndex(TextIndexTestUtils.SIMPLE_DEFAULT_NAME);
metaDataBuilder.addIndex(SIMPLE_DOC, SIMPLE_TEXT_NO_POSITIONS);
});
documents.forEach(recordStore::saveRecord);
// Queries that *don't* require position information should be planned to use the index
assertEquals(Arrays.asList(1L, 2L, 3L), querySimpleDocumentsWithIndex(Query.field("text").text().containsAny("king civil récu"), SIMPLE_TEXT_NO_POSITIONS.getName(), 0, true));
assertEquals(Collections.singletonList(2L), querySimpleDocumentsWithIndex(Query.field("text").text().containsAll("unclean verona"), SIMPLE_TEXT_NO_POSITIONS.getName(), 0, true));
assertEquals(Arrays.asList(0L, 1L, 2L, 3L), querySimpleDocumentsWithIndex(Query.field("text").text().containsPrefix("th"), SIMPLE_TEXT_NO_POSITIONS.getName(), 0, true));
// Queries that *do* require position information must be planned as scans
assertEquals(Collections.singletonList(2L), querySimpleDocumentsWithScan(Query.field("text").text().containsPhrase("civil blood makes civil hands unclean"), 0));
assertEquals(Collections.singletonList(3L), querySimpleDocumentsWithScan(Query.field("text").text().containsAll("France Napoleons", 3), 0));
commit(context);
}
final List<SimpleDocument> newDocuments = documents.stream().map(doc -> doc.toBuilder().setDocId(doc.getDocId() + documents.size()).build()).collect(Collectors.toList());
// Upgrade to writing position information
try (FDBRecordContext context = openContext()) {
openRecordStore(context, metaDataBuilder -> {
metaDataBuilder.removeIndex(TextIndexTestUtils.SIMPLE_DEFAULT_NAME);
metaDataBuilder.addIndex(SIMPLE_DOC, new Index(SIMPLE_TEXT_NO_POSITIONS.getName(), SIMPLE_TEXT_NO_POSITIONS.getRootExpression(), IndexTypes.TEXT));
});
newDocuments.forEach(recordStore::saveRecord);
// Queries that *don't* require position information produce the same plan
assertEquals(Arrays.asList(1L, 2L, 3L, 5L, 6L, 7L), querySimpleDocumentsWithIndex(Query.field("text").text().containsAny("king civil récu"), SIMPLE_TEXT_NO_POSITIONS.getName(), 0, true));
assertEquals(Arrays.asList(2L, 6L), querySimpleDocumentsWithIndex(Query.field("text").text().containsAll("unclean verona"), SIMPLE_TEXT_NO_POSITIONS.getName(), 0, true));
assertEquals(Arrays.asList(0L, 1L, 2L, 4L, 5L, 6L, 3L, 7L), querySimpleDocumentsWithIndex(Query.field("text").text().containsPrefix("th"), SIMPLE_TEXT_NO_POSITIONS.getName(), 0, true));
// Queries that *do* require position information now use the index, but previously written documents show up in the
// query spuriously
assertEquals(Arrays.asList(2L, 6L), querySimpleDocumentsWithIndex(Query.field("text").text().containsPhrase("civil blood makes civil hands unclean"), SIMPLE_TEXT_NO_POSITIONS.getName(), 0, true));
assertEquals(Collections.singletonList(2L), querySimpleDocumentsWithIndex(Query.field("text").text().containsPhrase("unclean verona"), SIMPLE_TEXT_NO_POSITIONS.getName(), 0, true));
assertEquals(Arrays.asList(3L, 7L), querySimpleDocumentsWithIndex(Query.field("text").text().containsAll("France Napoleons", 3), SIMPLE_TEXT_NO_POSITIONS.getName(), 0, true));
assertEquals(Collections.singletonList(3L), querySimpleDocumentsWithIndex(Query.field("text").text().containsAll("Thiers Napoleons", 3), SIMPLE_TEXT_NO_POSITIONS.getName(), 0, true));
commit(context);
}
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext in project fdb-record-layer by FoundationDB.
the class TextIndexTest method invalidScans.
@Test
public void invalidScans() throws Exception {
try (FDBRecordContext context = openContext()) {
openRecordStore(context);
final Index index = recordStore.getRecordMetaData().getIndex(TextIndexTestUtils.SIMPLE_DEFAULT_NAME);
assertThrows(RecordCoreException.class, () -> recordStore.scanIndex(index, BY_VALUE, TupleRange.ALL, null, ScanProperties.REVERSE_SCAN));
assertThrows(RecordCoreException.class, () -> recordStore.scanIndex(index, BY_GROUP, TupleRange.ALL, null, ScanProperties.REVERSE_SCAN));
assertThrows(RecordCoreException.class, () -> recordStore.scanIndex(index, BY_RANK, TupleRange.ALL, null, ScanProperties.REVERSE_SCAN));
assertThrows(RecordCoreException.class, () -> recordStore.scanIndex(index, BY_TIME_WINDOW, TupleRange.ALL, null, ScanProperties.REVERSE_SCAN));
}
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext in project fdb-record-layer by FoundationDB.
the class TextIndexTest method textIndexPerf100InsertOneBatch.
@Tag(Tags.Performance)
@Test
public void textIndexPerf100InsertOneBatch() throws Exception {
// Create 1000 records
Random r = new Random();
List<SimpleDocument> records = getRandomRecords(r, 100);
long startTime = System.nanoTime();
try (FDBRecordContext context = openContext()) {
openRecordStore(context);
for (int i = 0; i < records.size(); i++) {
recordStore.saveRecord(records.get(i));
}
commit(context);
}
long endTime = System.nanoTime();
LOGGER.info("performed 100 serial insertions in {} seconds.", (endTime - startTime) * 1e-9);
printUsage();
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext in project fdb-record-layer by FoundationDB.
the class TextIndexTest method queryMapsWithGroups.
@Test
public void queryMapsWithGroups() throws Exception {
final List<String> textSamples = Arrays.asList(TextSamples.ROMEO_AND_JULIET_PROLOGUE, TextSamples.AETHELRED, TextSamples.ROMEO_AND_JULIET_PROLOGUE, TextSamples.ANGSTROM);
final List<MapDocument> documents = IntStream.range(0, textSamples.size() / 2).mapToObj(i -> MapDocument.newBuilder().setDocId(i).addEntry(MapDocument.Entry.newBuilder().setKey("a").setValue(textSamples.get(i * 2)).build()).addEntry(MapDocument.Entry.newBuilder().setKey("b").setValue(textSamples.get(i * 2 + 1)).build()).setGroup(i % 2).build()).collect(Collectors.toList());
try (FDBRecordContext context = openContext()) {
openRecordStore(context, metaDataBuilder -> metaDataBuilder.addIndex(MAP_DOC, MAP_ON_VALUE_GROUPED_INDEX));
documents.forEach(recordStore::saveRecord);
assertEquals(Collections.singletonList(1L), queryMapDocumentsWithGroupedIndex("a", Query.field("value").text().containsPhrase("both alike in dignity"), 1L, 1376087127));
assertEquals(Collections.singletonList(0L), queryMapDocumentsWithGroupedIndex("b", Query.field("value").text().containsAny("king anders"), 0L, -1204479544));
commit(context);
}
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext in project fdb-record-layer by FoundationDB.
the class TextIndexTest method queryMultiTypeDocuments.
@Test
public void queryMultiTypeDocuments() throws Exception {
final List<String> bothTypes = Arrays.asList(SIMPLE_DOC, COMPLEX_DOC);
final List<String> simpleTypes = Collections.singletonList(SIMPLE_DOC);
final List<String> complexTypes = Collections.singletonList(COMPLEX_DOC);
final List<String> textSamples = Arrays.asList(TextSamples.ROMEO_AND_JULIET_PROLOGUE, TextSamples.ROMEO_AND_JULIET_PROLOGUE, TextSamples.ANGSTROM, TextSamples.AETHELRED, TextSamples.FRENCH, TextSamples.GERMAN);
final List<Message> documents = IntStream.range(0, textSamples.size()).mapToObj(i -> {
final String text = textSamples.get(i);
if (i % 2 == 0) {
return SimpleDocument.newBuilder().setDocId(i).setText(text).setGroup(i % 4).build();
} else {
return ComplexDocument.newBuilder().setDocId(i).setText(text).setGroup(i % 4).build();
}
}).collect(Collectors.toList());
try (FDBRecordContext context = openContext()) {
openRecordStore(context, metaDataBuilder -> {
metaDataBuilder.getRecordType(COMPLEX_DOC).setPrimaryKey(field("doc_id"));
metaDataBuilder.removeIndex(TextIndexTestUtils.SIMPLE_DEFAULT_NAME);
metaDataBuilder.addMultiTypeIndex(Arrays.asList(metaDataBuilder.getRecordType(SIMPLE_DOC), metaDataBuilder.getRecordType(COMPLEX_DOC)), MULTI_TYPE_INDEX);
});
documents.forEach(recordStore::saveRecord);
assertEquals(Arrays.asList(0L, 1L), queryMultiTypeDocuments(Query.field("text").text().containsPhrase("where we lay our scene"), bothTypes, 1755757799));
assertEquals(Collections.singletonList(0L), queryMultiTypeDocuments(Query.field("text").text().containsPhrase("where we lay our scene"), simpleTypes, -1489953261));
assertEquals(Collections.singletonList(1L), queryMultiTypeDocuments(Query.field("text").text().containsPhrase("where we lay our scene"), complexTypes, -1333764399));
assertEquals(Arrays.asList(2L, 4L, 5L), queryMultiTypeDocuments(Query.field("text").text().containsPrefix("na"), bothTypes, -714642562));
assertEquals(Arrays.asList(2L, 4L), queryMultiTypeDocuments(Query.field("text").text().containsPrefix("na"), simpleTypes, 334613674));
assertEquals(Collections.singletonList(5L), queryMultiTypeDocuments(Query.field("text").text().containsPrefix("na"), complexTypes, 490802536));
commit(context);
}
}
Aggregations