use of org.neo4j.internal.kernel.api.IndexQueryConstraints in project neo4j by neo4j.
the class FulltextIndexProgressorTest method mustExhaustIteratorWhenLimitIsGreaterThanIterator.
@Test
void mustExhaustIteratorWhenLimitIsGreaterThanIterator() {
StubValuesIterator iterator = new StubValuesIterator();
iterator.add(1, 1.0f);
iterator.add(2, 2.0f);
iterator.add(3, 3.0f);
iterator.add(4, 4.0f);
IndexQueryConstraints constraints = unconstrained().limit(5);
StubEntityValueClient client = new StubEntityValueClient();
FulltextIndexProgressor progressor = new FulltextIndexProgressor(iterator, client, constraints);
boolean keepGoing;
do {
keepGoing = progressor.next();
} while (keepGoing);
assertThat(client.entityIds).containsExactly(1L, 2L, 3L, 4L);
assertThat(client.scores).containsExactly(1.0f, 2.0f, 3.0f, 4.0f);
}
use of org.neo4j.internal.kernel.api.IndexQueryConstraints 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.IndexQueryConstraints in project neo4j by neo4j.
the class FulltextIndexProviderTest method queryingWithIndexProgressorMustProvideScore.
@Test
void queryingWithIndexProgressorMustProvideScore() throws Exception {
long nodeId = createTheThirdNode();
IndexDescriptor index;
index = createIndex(new int[] { labelIdHej, labelIdHa, labelIdHe }, new int[] { propIdHej, propIdHa, propIdHe, propIdHo });
await(index);
List<String> acceptedEntities = new ArrayList<>();
try (KernelTransactionImplementation ktx = getKernelTransaction()) {
NodeValueIndexCursor cursor = new ExtendedNodeValueIndexCursorAdapter() {
private long nodeReference;
private IndexProgressor progressor;
@Override
public long nodeReference() {
return nodeReference;
}
@Override
public boolean next() {
return progressor.next();
}
@Override
public void initialize(IndexDescriptor descriptor, IndexProgressor progressor, PropertyIndexQuery[] query, IndexQueryConstraints constraints, boolean indexIncludesTransactionState) {
this.progressor = progressor;
}
@Override
public boolean acceptEntity(long reference, float score, Value... values) {
this.nodeReference = reference;
assertFalse(Float.isNaN(score), "score should not be NaN");
assertThat(score).as("score must be positive").isGreaterThan(0.0f);
acceptedEntities.add("reference = " + reference + ", score = " + score + ", " + Arrays.toString(values));
return true;
}
};
Read read = ktx.dataRead();
IndexReadSession indexSession = ktx.dataRead().indexReadSession(index);
read.nodeIndexSeek(indexSession, cursor, unconstrained(), fulltextSearch("hej:\"villa\""));
int counter = 0;
while (cursor.next()) {
assertThat(cursor.nodeReference()).isEqualTo(nodeId);
counter++;
}
assertThat(counter).isEqualTo(1);
assertThat(acceptedEntities.size()).isEqualTo(1);
acceptedEntities.clear();
}
}
use of org.neo4j.internal.kernel.api.IndexQueryConstraints in project neo4j by neo4j.
the class EntityValueIndexCursorTestBase method shouldPerformBooleanSearch.
@Test
void shouldPerformBooleanSearch() throws KernelException {
// given
boolean needsValues = indexParams.indexProvidesBooleanValues();
IndexQueryConstraints constraints = unordered(needsValues);
int prop = token.propertyKey(PROP_NAME);
IndexReadSession index = read.indexReadSession(schemaRead.indexGetForName(PROP_INDEX_NAME));
IndexValueCapability capability = index.reference().getCapability().valueCapability(ValueGroup.BOOLEAN.category());
try (var cursor = entityParams.allocateEntityValueIndexCursor(tx, cursors)) {
MutableLongSet uniqueIds = new LongHashSet();
// when
entityParams.entityIndexSeek(tx, index, cursor, constraints, PropertyIndexQuery.exact(prop, false));
// then
assertFoundEntitiesAndValue(cursor, 1, uniqueIds, capability, needsValues);
// when
entityParams.entityIndexSeek(tx, index, cursor, constraints, PropertyIndexQuery.exact(prop, true));
// then
assertFoundEntitiesAndValue(cursor, 1, uniqueIds, capability, needsValues);
}
}
use of org.neo4j.internal.kernel.api.IndexQueryConstraints in project neo4j by neo4j.
the class EntityValueIndexCursorTestBase method shouldPerformTextArraySearch.
@Test
void shouldPerformTextArraySearch() throws KernelException {
// given
boolean needsValues = indexParams.indexProvidesArrayValues();
IndexQueryConstraints constraints = unordered(needsValues);
int prop = token.propertyKey(PROP_NAME);
IndexReadSession index = read.indexReadSession(schemaRead.indexGetForName(PROP_INDEX_NAME));
IndexValueCapability capability = index.reference().getCapability().valueCapability(ValueGroup.TEXT_ARRAY.category());
try (var cursor = entityParams.allocateEntityValueIndexCursor(tx, cursors)) {
MutableLongSet uniqueIds = new LongHashSet();
// when
entityParams.entityIndexSeek(tx, index, cursor, constraints, PropertyIndexQuery.exact(prop, new String[] { "first", "second", "third" }));
// then
assertFoundEntitiesAndValue(cursor, 1, uniqueIds, capability, needsValues);
// when
entityParams.entityIndexSeek(tx, index, cursor, constraints, PropertyIndexQuery.exact(prop, new String[] { "fourth", "fifth", "sixth", "seventh" }));
// then
assertFoundEntitiesAndValue(cursor, 1, uniqueIds, capability, needsValues);
}
}
Aggregations