use of org.neo4j.kernel.api.index.IndexProgressor in project neo4j by neo4j.
the class RelationshipIndexedRelationshipStoreScanTest method mockIdsReturnedFromTokenQueries.
private void mockIdsReturnedFromTokenQueries() {
// Make token index reader return different ids for the different tokens
doAnswer(invocation -> {
IndexProgressor.EntityTokenClient client = invocation.getArgument(0);
TokenPredicate token = invocation.getArgument(2);
client.initialize(new IndexProgressor() {
private final PrimitiveIterator.OfLong relationshipsWithType1 = Arrays.stream(new long[] { 1, 2, 4, 8 }).iterator();
private final PrimitiveIterator.OfLong relationshipsWithType2 = Arrays.stream(new long[] { 2, 5, 6 }).iterator();
@Override
public boolean next() {
PrimitiveIterator.OfLong relationshipsWithType = relationshipsWithType1;
if (token.tokenId() == 2) {
relationshipsWithType = relationshipsWithType2;
}
if (relationshipsWithType.hasNext()) {
client.acceptEntity(relationshipsWithType.nextLong(), null);
return true;
}
return false;
}
@Override
public void close() {
}
}, token.tokenId(), IndexOrder.NONE);
return null;
}).when(relationshipTypeScanReader).query(any(), any(), any(), any(), any());
}
use of org.neo4j.kernel.api.index.IndexProgressor 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.kernel.api.index.IndexProgressor in project neo4j by neo4j.
the class FulltextIndexReader method query.
@Override
public void query(QueryContext context, IndexProgressor.EntityValueClient client, IndexQueryConstraints constraints, PropertyIndexQuery... queries) throws IndexNotApplicableKernelException {
BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder();
for (PropertyIndexQuery indexQuery : queries) {
if (indexQuery.type() == PropertyIndexQuery.IndexQueryType.fulltextSearch) {
PropertyIndexQuery.FulltextSearchPredicate fulltextSearch = (PropertyIndexQuery.FulltextSearchPredicate) indexQuery;
try {
queryBuilder.add(parseFulltextQuery(fulltextSearch.query()), BooleanClause.Occur.SHOULD);
} catch (ParseException e) {
throw new RuntimeException("Could not parse the given fulltext search query: '" + fulltextSearch.query() + "'.", e);
}
} else {
// Not fulltext query
assertNotComposite(queries);
assertCypherCompatible();
Query query;
if (indexQuery.type() == PropertyIndexQuery.IndexQueryType.stringContains) {
PropertyIndexQuery.StringContainsPredicate scp = (PropertyIndexQuery.StringContainsPredicate) indexQuery;
String searchTerm = QueryParser.escape(scp.contains().stringValue());
Term term = new Term(propertyNames[0], "*" + searchTerm + "*");
query = new WildcardQuery(term);
} else if (indexQuery.type() == PropertyIndexQuery.IndexQueryType.stringSuffix) {
PropertyIndexQuery.StringSuffixPredicate ssp = (PropertyIndexQuery.StringSuffixPredicate) indexQuery;
String searchTerm = QueryParser.escape(ssp.suffix().stringValue());
Term term = new Term(propertyNames[0], "*" + searchTerm);
query = new WildcardQuery(term);
} else if (indexQuery.type() == PropertyIndexQuery.IndexQueryType.stringPrefix) {
PropertyIndexQuery.StringPrefixPredicate spp = (PropertyIndexQuery.StringPrefixPredicate) indexQuery;
String searchTerm = spp.prefix().stringValue();
Term term = new Term(propertyNames[0], searchTerm);
query = new LuceneDocumentStructure.PrefixMultiTermsQuery(term);
} else if (indexQuery.getClass() == PropertyIndexQuery.ExactPredicate.class && indexQuery.valueGroup() == ValueGroup.TEXT) {
PropertyIndexQuery.ExactPredicate exact = (PropertyIndexQuery.ExactPredicate) indexQuery;
String searchTerm = ((TextValue) exact.value()).stringValue();
Term term = new Term(propertyNames[0], searchTerm);
query = new ConstantScoreQuery(new TermQuery(term));
} else if (indexQuery.getClass() == PropertyIndexQuery.TextRangePredicate.class) {
PropertyIndexQuery.TextRangePredicate sp = (PropertyIndexQuery.TextRangePredicate) indexQuery;
query = newRangeSeekByStringQuery(propertyNames[0], sp.from(), sp.fromInclusive(), sp.to(), sp.toInclusive());
} else {
throw new IndexNotApplicableKernelException("A fulltext schema index cannot answer " + indexQuery.type() + " queries on " + indexQuery.valueCategory() + " values.");
}
queryBuilder.add(query, BooleanClause.Occur.MUST);
}
}
Query query = queryBuilder.build();
ValuesIterator itr = searchLucene(query, constraints, context, context.cursorContext(), context.memoryTracker());
IndexProgressor progressor = new FulltextIndexProgressor(itr, client, constraints);
client.initialize(index, progressor, queries, constraints, true);
}
use of org.neo4j.kernel.api.index.IndexProgressor in project neo4j by neo4j.
the class FusionIndexReaderTest method closeIteratorMustCloseAll.
// close iterator
@Test
void closeIteratorMustCloseAll() throws Exception {
// given
IndexProgressor[] progressors = new IndexProgressor[aliveReaders.length];
for (int i = 0; i < aliveReaders.length; i++) {
int slot = i;
doAnswer(invocation -> {
IndexProgressor.EntityValueClient client = invocation.getArgument(1);
IndexProgressor progressor = mock(IndexProgressor.class);
client.initialize(DESCRIPTOR, progressor, getIndexQueryArgument(invocation), invocation.getArgument(2), false);
progressors[slot] = progressor;
return null;
}).when(aliveReaders[i]).query(any(), any(), any(), any());
}
// when
try (NodeValueIterator iterator = new NodeValueIterator()) {
fusionIndexReader.query(NULL_CONTEXT, iterator, unconstrained(), PropertyIndexQuery.exists(PROP_KEY));
}
// then
for (IndexProgressor progressor : progressors) {
verify(progressor).close();
}
}
Aggregations