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)));
}
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));
}
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));
});
}
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));
});
}
Aggregations