use of com.apple.foundationdb.record.TestRecordsTextProto.ComplexDocument in project fdb-record-layer by FoundationDB.
the class TextIndexTest method queryComplexDocumentsCovering.
@Test
public void queryComplexDocumentsCovering() throws Exception {
final List<String> textSamples = Arrays.asList(TextSamples.FRENCH, TextSamples.GERMAN, TextSamples.ROMEO_AND_JULIET_PROLOGUE, TextSamples.YIDDISH);
final List<ComplexDocument> documents = IntStream.range(0, textSamples.size()).mapToObj(i -> ComplexDocument.newBuilder().setDocId(i).setGroup(i % 2).setText(textSamples.get(i)).setScore(i).build()).collect(Collectors.toList());
try (FDBRecordContext context = openContext()) {
openRecordStore(context, metaDataBuilder -> {
metaDataBuilder.removeIndex(TextIndexTestUtils.SIMPLE_DEFAULT_NAME);
metaDataBuilder.addIndex(COMPLEX_DOC, COMPLEX_TEXT_BY_GROUP);
});
documents.forEach(recordStore::saveRecord);
// Try to plan a covered query with separate group and doc_id fields
RecordQuery query = RecordQuery.newBuilder().setRecordType(COMPLEX_DOC).setRequiredResults(Arrays.asList(field("group"), field("doc_id"))).setFilter(Query.and(Query.field("group").equalsValue(0L), Query.field("text").text().containsPhrase("continuance of their parents' rage"))).build();
RecordQueryPlan plan = planner.plan(query);
assertThat(plan, coveringIndexScan(textIndexScan(allOf(indexName(COMPLEX_TEXT_BY_GROUP.getName()), groupingBounds(hasTupleString("[[0],[0]]")), textComparison(equalTo(new Comparisons.TextComparison(Comparisons.Type.TEXT_CONTAINS_PHRASE, "continuance of their parents' rage", null, DefaultTextTokenizer.NAME)))))));
assertEquals(822541560, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
assertEquals(-1798902497, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
assertEquals(770172924, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
List<ComplexDocument> results = recordStore.executeQuery(plan).map(rec -> ComplexDocument.newBuilder().mergeFrom(rec.getRecord()).build()).asList().get();
assertEquals(results.size(), 1);
ComplexDocument result = results.get(0);
assertEquals(result.getGroup(), 0L);
assertEquals(result.getDocId(), 2L);
assertThat(result.hasScore(), is(false));
// Try to plan a covered query with one concatenated field
query = RecordQuery.newBuilder().setRecordType(COMPLEX_DOC).setRequiredResults(Collections.singletonList(concatenateFields("group", "doc_id"))).setFilter(Query.and(Query.field("group").equalsValue(0L), Query.field("text").text().containsPhrase("continuance of their parents' rage"))).build();
plan = planner.plan(query);
assertThat(plan, coveringIndexScan(textIndexScan(allOf(indexName(COMPLEX_TEXT_BY_GROUP.getName()), groupingBounds(hasTupleString("[[0],[0]]")), textComparison(equalTo(new Comparisons.TextComparison(Comparisons.Type.TEXT_CONTAINS_PHRASE, "continuance of their parents' rage", null, DefaultTextTokenizer.NAME)))))));
assertEquals(822541560, plan.planHash(PlanHashable.PlanHashKind.LEGACY));
assertEquals(-1798902497, plan.planHash(PlanHashable.PlanHashKind.FOR_CONTINUATION));
assertEquals(770172924, plan.planHash(PlanHashable.PlanHashKind.STRUCTURAL_WITHOUT_LITERALS));
results = recordStore.executeQuery(plan).map(rec -> ComplexDocument.newBuilder().mergeFrom(rec.getRecord()).build()).asList().get();
assertEquals(results.size(), 1);
result = results.get(0);
assertEquals(result.getGroup(), 0L);
assertEquals(result.getDocId(), 2L);
assertThat(result.hasScore(), is(false));
commit(context);
}
}
use of com.apple.foundationdb.record.TestRecordsTextProto.ComplexDocument in project fdb-record-layer by FoundationDB.
the class TextIndexTest method saveCombinedByGroup.
@Test
public void saveCombinedByGroup() throws Exception {
final SimpleDocument simpleDocument = SimpleDocument.newBuilder().setGroup(0).setDocId(1907L).setText(TextSamples.ANGSTROM).build();
final ComplexDocument complexDocument = ComplexDocument.newBuilder().setGroup(0).setDocId(966L).setText(TextSamples.AETHELRED).build();
try (FDBRecordContext context = openContext()) {
openRecordStore(context, metaDataBuilder -> {
metaDataBuilder.removeIndex(TextIndexTestUtils.SIMPLE_DEFAULT_NAME);
metaDataBuilder.addMultiTypeIndex(Arrays.asList(metaDataBuilder.getRecordType(COMPLEX_DOC), metaDataBuilder.getRecordType(SIMPLE_DOC)), COMBINED_TEXT_BY_GROUP);
});
recordStore.saveRecord(simpleDocument);
int firstKeys = getSaveIndexKeyCount(recordStore);
assertEquals(8, firstKeys);
recordStore.saveRecord(complexDocument);
int secondKeys = getSaveIndexKeyCount(recordStore) - firstKeys;
assertEquals(11, secondKeys);
List<Map.Entry<Tuple, List<Integer>>> entryList = scanMapEntries(recordStore, COMBINED_TEXT_BY_GROUP, Tuple.from(0, "was"));
assertEquals(Arrays.asList(entryOf(Tuple.from(0L, 966L), Collections.singletonList(7)), entryOf(Tuple.from(1907L), Collections.singletonList(4))), entryList);
commit(context);
}
}
Aggregations