Search in sources :

Example 11 with PropertyIndexQuery

use of org.neo4j.internal.kernel.api.PropertyIndexQuery in project neo4j by neo4j.

the class TransactionImpl method findNodes.

@Override
public ResourceIterator<Node> findNodes(final Label myLabel, final String key, final String value, final StringSearchMode searchMode) {
    checkLabel(myLabel);
    checkPropertyKey(key);
    checkArgument(value != null, "Template must not be null");
    KernelTransaction transaction = kernelTransaction();
    TokenRead tokenRead = transaction.tokenRead();
    int labelId = tokenRead.nodeLabel(myLabel.name());
    int propertyId = tokenRead.propertyKey(key);
    PropertyIndexQuery query = getIndexQuery(value, searchMode, propertyId);
    return nodesByLabelAndProperty(transaction, labelId, query);
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) PropertyIndexQuery(org.neo4j.internal.kernel.api.PropertyIndexQuery) TokenRead(org.neo4j.internal.kernel.api.TokenRead)

Example 12 with PropertyIndexQuery

use of org.neo4j.internal.kernel.api.PropertyIndexQuery in project neo4j by neo4j.

the class StartOldDbOnCurrentVersionAndCreateFusionIndexIT method countIndexedNodes.

private static int countIndexedNodes(GraphDatabaseAPI db, Label label, String... keys) throws Exception {
    try (InternalTransaction tx = (InternalTransaction) db.beginTx()) {
        KernelTransaction ktx = tx.kernelTransaction();
        TokenRead tokenRead = ktx.tokenRead();
        int labelId = tokenRead.nodeLabel(label.name());
        int[] propertyKeyIds = new int[keys.length];
        for (int i = 0; i < propertyKeyIds.length; i++) {
            propertyKeyIds[i] = tokenRead.propertyKey(keys[i]);
        }
        PropertyIndexQuery[] predicates = new PropertyIndexQuery[propertyKeyIds.length];
        for (int i = 0; i < propertyKeyIds.length; i++) {
            predicates[i] = PropertyIndexQuery.exists(propertyKeyIds[i]);
        }
        IndexDescriptor index = single(ktx.schemaRead().index(SchemaDescriptor.forLabel(labelId, propertyKeyIds)));
        IndexReadSession indexSession = ktx.dataRead().indexReadSession(index);
        int count = 0;
        try (NodeValueIndexCursor cursor = ktx.cursors().allocateNodeValueIndexCursor(ktx.cursorContext(), ktx.memoryTracker())) {
            ktx.dataRead().nodeIndexSeek(indexSession, cursor, unconstrained(), predicates);
            while (cursor.next()) {
                count++;
            }
        }
        tx.commit();
        return count;
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) PropertyIndexQuery(org.neo4j.internal.kernel.api.PropertyIndexQuery) NodeValueIndexCursor(org.neo4j.internal.kernel.api.NodeValueIndexCursor) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) TokenRead(org.neo4j.internal.kernel.api.TokenRead) IndexReadSession(org.neo4j.internal.kernel.api.IndexReadSession)

Example 13 with PropertyIndexQuery

use of org.neo4j.internal.kernel.api.PropertyIndexQuery in project neo4j by neo4j.

the class FusionIndexReader method query.

@Override
public void query(QueryContext context, IndexProgressor.EntityValueClient cursor, IndexQueryConstraints constraints, PropertyIndexQuery... predicates) throws IndexNotApplicableKernelException {
    IndexSlot slot = slotSelector.selectSlot(predicates, PropertyIndexQuery::valueCategory);
    if (slot != null) {
        instanceSelector.select(slot).query(context, cursor, constraints, predicates);
    } else {
        if (constraints.isOrdered()) {
            throw new UnsupportedOperationException(format("Tried to query index with unsupported order %s. Supported orders for query %s are %s.", constraints.order(), Arrays.toString(predicates), IndexOrder.NONE));
        }
        BridgingIndexProgressor multiProgressor = new BridgingIndexProgressor(cursor, descriptor.schema().getPropertyIds());
        cursor.initialize(descriptor, multiProgressor, predicates, constraints, false);
        try {
            instanceSelector.forAll(reader -> {
                try {
                    reader.query(context, multiProgressor, constraints, predicates);
                } catch (IndexNotApplicableKernelException e) {
                    throw new InnerException(e);
                }
            });
        } catch (InnerException e) {
            throw e.getCause();
        }
    }
}
Also used : IndexNotApplicableKernelException(org.neo4j.internal.kernel.api.exceptions.schema.IndexNotApplicableKernelException) PropertyIndexQuery(org.neo4j.internal.kernel.api.PropertyIndexQuery) BridgingIndexProgressor(org.neo4j.kernel.api.index.BridgingIndexProgressor)

Example 14 with PropertyIndexQuery

use of org.neo4j.internal.kernel.api.PropertyIndexQuery in project neo4j by neo4j.

the class SimpleValueIndexReader method toLuceneQuery.

private static Query toLuceneQuery(PropertyIndexQuery... predicates) throws IndexNotApplicableKernelException {
    PropertyIndexQuery predicate = predicates[0];
    switch(predicate.type()) {
        case exact:
            Value[] values = new Value[predicates.length];
            for (int i = 0; i < predicates.length; i++) {
                assert predicates[i].type() == exact : "Exact followed by another query predicate type is not supported at this moment.";
                values[i] = ((PropertyIndexQuery.ExactPredicate) predicates[i]).value();
            }
            return LuceneDocumentStructure.newSeekQuery(values);
        case exists:
            for (PropertyIndexQuery p : predicates) {
                if (p.type() != IndexQueryType.exists) {
                    throw new IndexNotApplicableKernelException("Exists followed by another query predicate type is not supported.");
                }
            }
            return LuceneDocumentStructure.newScanQuery();
        case range:
            assertNotComposite(predicates);
            if (predicate.valueGroup() == ValueGroup.TEXT) {
                PropertyIndexQuery.TextRangePredicate sp = (PropertyIndexQuery.TextRangePredicate) predicate;
                return LuceneDocumentStructure.newRangeSeekByStringQuery(sp.from(), sp.fromInclusive(), sp.to(), sp.toInclusive());
            }
            throw new UnsupportedOperationException(format("Range scans of value group %s are not supported", predicate.valueGroup()));
        case stringPrefix:
            assertNotComposite(predicates);
            PropertyIndexQuery.StringPrefixPredicate spp = (PropertyIndexQuery.StringPrefixPredicate) predicate;
            return LuceneDocumentStructure.newRangeSeekByPrefixQuery(spp.prefix().stringValue());
        case stringContains:
            assertNotComposite(predicates);
            PropertyIndexQuery.StringContainsPredicate scp = (PropertyIndexQuery.StringContainsPredicate) predicate;
            return LuceneDocumentStructure.newWildCardStringQuery(scp.contains().stringValue());
        case stringSuffix:
            assertNotComposite(predicates);
            PropertyIndexQuery.StringSuffixPredicate ssp = (PropertyIndexQuery.StringSuffixPredicate) predicate;
            return LuceneDocumentStructure.newSuffixStringQuery(ssp.suffix().stringValue());
        default:
            // todo figure out a more specific exception
            throw new RuntimeException("Index query not supported: " + Arrays.toString(predicates));
    }
}
Also used : IndexNotApplicableKernelException(org.neo4j.internal.kernel.api.exceptions.schema.IndexNotApplicableKernelException) PropertyIndexQuery(org.neo4j.internal.kernel.api.PropertyIndexQuery) Value(org.neo4j.values.storable.Value)

Example 15 with PropertyIndexQuery

use of org.neo4j.internal.kernel.api.PropertyIndexQuery in project neo4j by neo4j.

the class CompositeIndexAccessorCompatibility method shouldSeekInOrderExactWithRange.

private void shouldSeekInOrderExactWithRange(IndexOrder order, Object o0, Object o1, Object o2, Object o3, Object o4, Object o5) throws Exception {
    // Todo use random value instead
    Object baseValue = 1;
    PropertyIndexQuery exact = exact(100, baseValue);
    PropertyIndexQuery range = range(200, Values.of(o0), true, Values.of(o5), true);
    IndexOrderCapability indexOrders = orderCapability(exact, range);
    if (order == IndexOrder.ASCENDING) {
        Assume.assumeTrue("Assume support for order " + order, indexOrders.supportsAsc());
    } else if (order == IndexOrder.DESCENDING) {
        Assume.assumeTrue("Assume support for order " + order, indexOrders.supportsDesc());
    }
    updateAndCommit(asList(add(1, descriptor.schema(), baseValue, o0), add(1, descriptor.schema(), baseValue, o5), add(1, descriptor.schema(), baseValue, o1), add(1, descriptor.schema(), baseValue, o4), add(1, descriptor.schema(), baseValue, o2), add(1, descriptor.schema(), baseValue, o3)));
    SimpleEntityValueClient client = new SimpleEntityValueClient();
    try (AutoCloseable ignored = query(client, order, exact, range)) {
        List<Long> seenIds = assertClientReturnValuesInOrder(client, order);
        assertThat(seenIds.size()).isEqualTo(6);
    }
}
Also used : PropertyIndexQuery(org.neo4j.internal.kernel.api.PropertyIndexQuery) IndexOrderCapability(org.neo4j.internal.schema.IndexOrderCapability) SimpleEntityValueClient(org.neo4j.storageengine.api.schema.SimpleEntityValueClient)

Aggregations

PropertyIndexQuery (org.neo4j.internal.kernel.api.PropertyIndexQuery)24 ArrayList (java.util.ArrayList)6 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)6 Value (org.neo4j.values.storable.Value)5 Test (org.junit.jupiter.api.Test)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 EnumSource (org.junit.jupiter.params.provider.EnumSource)3 Pair (org.neo4j.internal.helpers.collection.Pair)3 IndexReadSession (org.neo4j.internal.kernel.api.IndexReadSession)3 TokenRead (org.neo4j.internal.kernel.api.TokenRead)3 IndexNotApplicableKernelException (org.neo4j.internal.kernel.api.exceptions.schema.IndexNotApplicableKernelException)3 ValueIndexEntryUpdate (org.neo4j.storageengine.api.ValueIndexEntryUpdate)3 PointValue (org.neo4j.values.storable.PointValue)3 BooleanQuery (org.apache.lucene.search.BooleanQuery)2 Query (org.apache.lucene.search.Query)2 TermQuery (org.apache.lucene.search.TermQuery)2 Test (org.junit.Test)2 BridgingIndexProgressor (org.neo4j.kernel.api.index.BridgingIndexProgressor)2 SimpleEntityValueClient (org.neo4j.storageengine.api.schema.SimpleEntityValueClient)2 Duration (java.time.Duration)1