use of org.neo4j.internal.kernel.api.NodeValueIndexCursor 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.NodeValueIndexCursor in project neo4j by neo4j.
the class FulltextProcedures method queryFulltextForNodes.
@SystemProcedure
@Description("Query the given full-text index. Returns the matching nodes, 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.queryNodes", mode = READ)
public Stream<NodeOutput> queryFulltextForNodes(@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 != NODE) {
throw new IllegalArgumentException("The '" + name + "' index (" + indexReference + ") is an index on " + entityType + ", so it cannot be queried for nodes.");
}
NodeValueIndexCursor cursor = tx.cursors().allocateNodeValueIndexCursor(tx.cursorContext(), tx.memoryTracker());
IndexReadSession indexSession = tx.dataRead().indexReadSession(indexReference);
IndexQueryConstraints constraints = queryConstraints(options);
tx.dataRead().nodeIndexSeek(indexSession, cursor, constraints, PropertyIndexQuery.fulltextSearch(query));
Spliterator<NodeOutput> spliterator = new SpliteratorAdaptor<>() {
@Override
public boolean tryAdvance(Consumer<? super NodeOutput> action) {
while (cursor.next()) {
long nodeReference = cursor.nodeReference();
float score = cursor.score();
NodeOutput nodeOutput = NodeOutput.forExistingEntityOrNull(transaction, nodeReference, score);
if (nodeOutput != null) {
action.accept(nodeOutput);
return true;
}
}
cursor.close();
return false;
}
};
Stream<NodeOutput> stream = StreamSupport.stream(spliterator, false);
return stream.onClose(cursor::close);
}
Aggregations