use of org.neo4j.internal.kernel.api.RelationshipValueIndexCursor in project neo4j by neo4j.
the class LuceneFulltextTestSupport method assertQueryFindsIds.
void assertQueryFindsIds(KernelTransaction ktx, boolean nodes, String indexName, String query, long... ids) throws Exception {
IndexDescriptor index = ktx.schemaRead().indexGetForName(indexName);
IndexReadSession indexSession = ktx.dataRead().indexReadSession(index);
MutableLongSet set = LongSets.mutable.of(ids);
if (nodes) {
try (NodeValueIndexCursor cursor = ktx.cursors().allocateNodeValueIndexCursor(ktx.cursorContext(), ktx.memoryTracker())) {
ktx.dataRead().nodeIndexSeek(indexSession, cursor, unconstrained(), PropertyIndexQuery.fulltextSearch(query));
while (cursor.next()) {
long nodeId = cursor.nodeReference();
assertTrue(set.remove(nodeId), format("Result returned node id %d, expected one of %s", nodeId, Arrays.toString(ids)));
}
}
} else {
try (RelationshipValueIndexCursor cursor = ktx.cursors().allocateRelationshipValueIndexCursor(ktx.cursorContext(), ktx.memoryTracker())) {
ktx.dataRead().relationshipIndexSeek(indexSession, cursor, unconstrained(), PropertyIndexQuery.fulltextSearch(query));
while (cursor.next()) {
long relationshipId = cursor.relationshipReference();
assertTrue(set.remove(relationshipId), format("Result returned relationship id %d, expected one of %s", relationshipId, Arrays.toString(ids)));
}
}
}
if (!set.isEmpty()) {
fail("Number of results differ from expected. " + set.size() + " IDs were not found in the result: " + set);
}
}
use of org.neo4j.internal.kernel.api.RelationshipValueIndexCursor in project neo4j by neo4j.
the class AbstractIndexQueryingTest method relationshipIndexSeekMustThrowOnWrongIndexEntityType.
@Test
void relationshipIndexSeekMustThrowOnWrongIndexEntityType() throws IndexNotFoundKernelException {
IndexDescriptor index = schemaRead.indexGetForName("ftsNodes");
IndexReadSession indexReadSession = read.indexReadSession(index);
try (RelationshipValueIndexCursor cursor = cursors.allocateRelationshipValueIndexCursor(NULL, EmptyMemoryTracker.INSTANCE)) {
assertThrows(IndexNotApplicableKernelException.class, () -> read.relationshipIndexSeek(indexReadSession, cursor, unconstrained(), PropertyIndexQuery.fulltextSearch("search")));
}
}
use of org.neo4j.internal.kernel.api.RelationshipValueIndexCursor in project neo4j by neo4j.
the class KernelReadTracerTxStateTest method shouldTraceRelationshipIndexCursor.
@Test
void shouldTraceRelationshipIndexCursor() throws KernelException, TimeoutException {
// given
int connection;
int name;
String indexName = "myIndex";
IndexDescriptor index;
try (KernelTransaction tx = beginTransaction()) {
connection = tx.tokenWrite().relationshipTypeGetOrCreateForName("Connection");
name = tx.tokenWrite().propertyKeyGetOrCreateForName("name");
tx.commit();
}
try (KernelTransaction tx = beginTransaction()) {
SchemaDescriptor schema = SchemaDescriptor.fulltext(EntityType.RELATIONSHIP, array(connection), array(name));
IndexPrototype prototype = IndexPrototype.forSchema(schema, DESCRIPTOR).withName(indexName).withIndexType(IndexType.FULLTEXT);
index = tx.schemaWrite().indexCreate(prototype);
tx.commit();
}
try (KernelTransaction tx = beginTransaction()) {
Predicates.awaitEx(() -> tx.schemaRead().indexGetState(index) == ONLINE, 1, MINUTES);
long n1 = tx.dataWrite().nodeCreate();
long n2 = tx.dataWrite().nodeCreate();
long r = tx.dataWrite().relationshipCreate(n1, connection, n2);
tx.dataWrite().relationshipSetProperty(r, name, Values.stringValue("transformational"));
tx.commit();
}
// when
TestKernelReadTracer tracer = new TestKernelReadTracer();
try (KernelTransaction tx = beginTransaction();
RelationshipValueIndexCursor cursor = tx.cursors().allocateRelationshipValueIndexCursor(NULL, tx.memoryTracker())) {
cursor.setTracer(tracer);
IndexReadSession indexReadSession = tx.dataRead().indexReadSession(index);
tx.dataRead().relationshipIndexSeek(indexReadSession, cursor, unconstrained(), PropertyIndexQuery.fulltextSearch("transformational"));
assertTrue(cursor.next());
tracer.assertEvents(OnIndexSeek(), OnRelationship(cursor.relationshipReference()));
assertFalse(cursor.next());
tracer.assertEvents();
}
}
use of org.neo4j.internal.kernel.api.RelationshipValueIndexCursor in project neo4j by neo4j.
the class FulltextProcedures method queryFulltextForRelationships.
@SystemProcedure
@Description("Query the given full-text index. Returns the matching relationships, and their Lucene query score, ordered by score. " + "Valid keys for the options map are: 'skip' to skip the top N results; 'limit' to limit the number of results returned.")
@Procedure(name = "db.index.fulltext.queryRelationships", mode = READ)
public Stream<RelationshipOutput> queryFulltextForRelationships(@Name("indexName") String name, @Name("queryString") String query, @Name(value = "options", defaultValue = "{}") Map<String, Object> options) throws Exception {
if (callContext.isSystemDatabase()) {
return Stream.empty();
}
IndexDescriptor indexReference = getValidIndex(name);
awaitOnline(indexReference);
EntityType entityType = indexReference.schema().entityType();
if (entityType != RELATIONSHIP) {
throw new IllegalArgumentException("The '" + name + "' index (" + indexReference + ") is an index on " + entityType + ", so it cannot be queried for relationships.");
}
RelationshipValueIndexCursor cursor = tx.cursors().allocateRelationshipValueIndexCursor(tx.cursorContext(), tx.memoryTracker());
IndexReadSession indexReadSession = tx.dataRead().indexReadSession(indexReference);
IndexQueryConstraints constraints = queryConstraints(options);
tx.dataRead().relationshipIndexSeek(indexReadSession, cursor, constraints, PropertyIndexQuery.fulltextSearch(query));
Spliterator<RelationshipOutput> spliterator = new SpliteratorAdaptor<>() {
@Override
public boolean tryAdvance(Consumer<? super RelationshipOutput> action) {
while (cursor.next()) {
long relationshipReference = cursor.relationshipReference();
float score = cursor.score();
RelationshipOutput relationshipOutput = RelationshipOutput.forExistingEntityOrNull(transaction, relationshipReference, score);
if (relationshipOutput != null) {
action.accept(relationshipOutput);
return true;
}
}
cursor.close();
return false;
}
};
return StreamSupport.stream(spliterator, false).onClose(cursor::close);
}
use of org.neo4j.internal.kernel.api.RelationshipValueIndexCursor in project neo4j by neo4j.
the class RelationshipIndexTransactionStateTest method assertEntityAndValueForScan.
@Override
void assertEntityAndValueForScan(Set<Pair<Long, Value>> expected, KernelTransaction tx, IndexDescriptor index, boolean needsValues, Object anotherValueFoundByQuery) throws Exception {
IndexReadSession indexSession = tx.dataRead().indexReadSession(index);
try (RelationshipValueIndexCursor relationships = tx.cursors().allocateRelationshipValueIndexCursor(tx.cursorContext(), tx.memoryTracker())) {
tx.dataRead().relationshipIndexScan(indexSession, relationships, unordered(needsValues));
assertEntityAndValue(expected, tx, needsValues, anotherValueFoundByQuery, new RelationshipCursorAdapter(relationships));
}
}
Aggregations