Search in sources :

Example 6 with NodeResult

use of apoc.result.NodeResult in project neo4j-apoc-procedures by neo4j-contrib.

the class SchemaIndex method orderedByText.

@Procedure
@Description("apoc.index.orderedByText(label,key,operator,value,sort-relevance,limit) yield node - schema string search which keeps index order and adds limit, operator is 'STARTS WITH' or 'CONTAINS'")
public Stream<NodeResult> orderedByText(@Name("label") String label, @Name("key") String key, @Name("operator") String operator, @Name("value") String value, @Name("relevance") boolean relevance, @Name("limit") long limit) throws SchemaRuleNotFoundException, IndexNotFoundKernelException, DuplicateSchemaRuleException {
    SortedIndexReader sortedIndexReader = getSortedIndexReader(label, key, limit, getSort(value, value, relevance));
    PrimitiveLongIterator it = queryForString(sortedIndexReader, operator, value);
    return Util.toLongStream(it).mapToObj(id -> new NodeResult(db.getNodeById(id)));
}
Also used : PrimitiveLongIterator(org.neo4j.collection.primitive.PrimitiveLongIterator) SortedIndexReader(org.neo4j.kernel.api.impl.schema.reader.SortedIndexReader) NodeResult(apoc.result.NodeResult) Description(org.neo4j.procedure.Description) Procedure(org.neo4j.procedure.Procedure)

Example 7 with NodeResult

use of apoc.result.NodeResult in project neo4j-apoc-procedures by neo4j-contrib.

the class Xml method importToGraph.

@Procedure(mode = Mode.WRITE, value = "apoc.xml.import")
public Stream<NodeResult> importToGraph(@Name("url") String url, @Name(value = "config", defaultValue = "{}") Map<String, Object> config) throws IOException, XMLStreamException {
    final XMLStreamReader xml = getXMLStreamReaderFromUrl(url);
    XmlImportConfig importConfig = new XmlImportConfig(config);
    // TODO: make labels, reltypes and magic properties configurable
    // stores parents and their most recent child
    Deque<ParentAndChildPair> parents = new ArrayDeque<>();
    org.neo4j.graphdb.Node root = db.createNode(Label.label("XmlDocument"));
    setPropertyIfNotNull(root, "_xmlVersion", xml.getVersion());
    setPropertyIfNotNull(root, "_xmlEncoding", xml.getEncoding());
    root.setProperty("url", url);
    parents.push(new ParentAndChildPair(root));
    org.neo4j.graphdb.Node last = root;
    org.neo4j.graphdb.Node lastWord = root;
    while (xml.hasNext()) {
        xml.next();
        switch(xml.getEventType()) {
            case XMLStreamConstants.START_DOCUMENT:
                // xmlsteamreader starts off by definition at START_DOCUMENT prior to call next() - so ignore this one
                break;
            case XMLStreamConstants.PROCESSING_INSTRUCTION:
                org.neo4j.graphdb.Node pi = db.createNode(Label.label("XmlProcessingInstruction"));
                pi.setProperty("_piData", xml.getPIData());
                pi.setProperty("_piTarget", xml.getPITarget());
                last = connectWithParent(pi, parents.peek(), last);
                break;
            case XMLStreamConstants.START_ELEMENT:
                final QName qName = xml.getName();
                final org.neo4j.graphdb.Node tag = db.createNode(Label.label("XmlTag"));
                tag.setProperty("_name", qName.getLocalPart());
                for (int i = 0; i < xml.getAttributeCount(); i++) {
                    tag.setProperty(xml.getAttributeLocalName(i), xml.getAttributeValue(i));
                }
                last = connectWithParent(tag, parents.peek(), last);
                parents.push(new ParentAndChildPair(tag));
                break;
            case XMLStreamConstants.CHARACTERS:
                String text = xml.getText().trim();
                String[] words = text.split("\\s");
                for (int i = 0; i < words.length; i++) {
                    final String currentWord = words[i];
                    if (!currentWord.isEmpty()) {
                        org.neo4j.graphdb.Node word = db.createNode(Label.label("XmlWord"));
                        word.setProperty("text", currentWord);
                        last = connectWithParent(word, parents.peek(), last);
                        if (importConfig.isCreateNextWordRelationship()) {
                            lastWord.createRelationshipTo(word, RelationshipType.withName("NEXT_WORD"));
                            lastWord = word;
                        }
                    }
                }
                break;
            case XMLStreamConstants.END_ELEMENT:
                ParentAndChildPair parent = parents.pop();
                if (parent.getPreviousChild() != null) {
                    parent.getPreviousChild().createRelationshipTo(parent.getParent(), RelationshipType.withName("LAST_CHILD_OF"));
                }
                break;
            case XMLStreamConstants.END_DOCUMENT:
                parents.pop();
                break;
            case XMLStreamConstants.COMMENT:
            case XMLStreamConstants.SPACE:
                // intentionally do nothing
                break;
            default:
                log.warn("xml file contains a {} type structure - ignoring this.", xml.getEventType());
        }
    }
    if (!parents.isEmpty()) {
        throw new IllegalStateException("non empty parents");
    }
    return Stream.of(new NodeResult(root));
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) QName(javax.xml.namespace.QName) NodeResult(apoc.result.NodeResult)

Example 8 with NodeResult

use of apoc.result.NodeResult in project neo4j-apoc-procedures by neo4j-contrib.

the class SubgraphTest method testSubgraphWithMaxDepthShouldContainExpectedNodes.

@Test
public void testSubgraphWithMaxDepthShouldContainExpectedNodes() throws Throwable {
    String controlQuery = "MATCH (m:Movie {title: 'The Matrix'})-[*0..3]-(subgraphNode) return collect(distinct subgraphNode) as subgraph";
    List<NodeResult> subgraph;
    try (Transaction tx = db.beginTx()) {
        Result result = db.execute(controlQuery);
        subgraph = (List<NodeResult>) result.next().get("subgraph");
    }
    String query = "MATCH (m:Movie {title: 'The Matrix'}) CALL apoc.path.subgraphNodes(m,{maxLevel:3}) yield node return COLLECT(node) as subgraphNodes";
    TestUtil.testCall(db, query, (row) -> {
        List<NodeResult> subgraphNodes = (List<NodeResult>) row.get("subgraphNodes");
        assertEquals(subgraph.size(), subgraphNodes.size());
        assertTrue(subgraph.containsAll(subgraphNodes));
    });
}
Also used : Transaction(org.neo4j.graphdb.Transaction) NodeResult(apoc.result.NodeResult) List(java.util.List) NodeResult(apoc.result.NodeResult) Result(org.neo4j.graphdb.Result) RelationshipResult(apoc.result.RelationshipResult) Test(org.junit.Test)

Example 9 with NodeResult

use of apoc.result.NodeResult in project neo4j-apoc-procedures by neo4j-contrib.

the class SubgraphTest method testSubgraphWithLabelFilterShouldContainExpectedNodes.

@Test
public void testSubgraphWithLabelFilterShouldContainExpectedNodes() throws Throwable {
    String controlQuery = "MATCH path = (:Person {name: 'Keanu Reeves'})-[*0..3]-(subgraphNode) where all(node in nodes(path) where node:Person) return collect(distinct subgraphNode) as subgraph";
    List<NodeResult> subgraph;
    try (Transaction tx = db.beginTx()) {
        Result result = db.execute(controlQuery);
        subgraph = (List<NodeResult>) result.next().get("subgraph");
    }
    String query = "MATCH (k:Person {name: 'Keanu Reeves'}) CALL apoc.path.subgraphNodes(k,{maxLevel:3, labelFilter:'+Person'}) yield node return COLLECT(node) as subgraphNodes";
    TestUtil.testCall(db, query, (row) -> {
        List<NodeResult> subgraphNodes = (List<NodeResult>) row.get("subgraphNodes");
        assertEquals(subgraph.size(), subgraphNodes.size());
        assertTrue(subgraph.containsAll(subgraphNodes));
    });
}
Also used : Transaction(org.neo4j.graphdb.Transaction) NodeResult(apoc.result.NodeResult) List(java.util.List) NodeResult(apoc.result.NodeResult) Result(org.neo4j.graphdb.Result) RelationshipResult(apoc.result.RelationshipResult) Test(org.junit.Test)

Aggregations

NodeResult (apoc.result.NodeResult)9 RelationshipResult (apoc.result.RelationshipResult)6 List (java.util.List)6 Test (org.junit.Test)6 Result (org.neo4j.graphdb.Result)6 Transaction (org.neo4j.graphdb.Transaction)6 RefactorConfig (apoc.refactor.util.RefactorConfig)1 QName (javax.xml.namespace.QName)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 PrimitiveLongIterator (org.neo4j.collection.primitive.PrimitiveLongIterator)1 SortedIndexReader (org.neo4j.kernel.api.impl.schema.reader.SortedIndexReader)1 Description (org.neo4j.procedure.Description)1 Procedure (org.neo4j.procedure.Procedure)1