Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

appendix 2: Cypher Queries

Ben Wong edited this page Dec 4, 2018 · 6 revisions

Plain Cypher

nb: can be run via the web interface, eg: localhost:7474 or via grapheneDB web interface (heroku)

Find a work order

MATCH (n:`Graph::WorkOrder`) WHERE n.reference = 'WORK_ORDER_REF' RETURN n

50 recent work orders

MATCH (n:`Graph::WorkOrder`) WHERE n.reference < 'A' RETURN n ORDER BY n.reference DESC LIMIT 50

Work orders with other related orders

MATCH p=(n)-[*3..9 {extra: false}]-()
WHERE n.reference > 'A_RECENT_WORK_ORDER_REF' AND n.reference < 'A'
RETURN p

Related work orders

MATCH p=(n {reference: 'WORK_ORDER_REF'})-[*..9 {extra: false}]-() RETURN p

Incremental delete (neo4j is unhappy when deleting > 60k nodes)

MATCH (n:`Graph::ModelName`)
WHERE n.reference > '01500000'
WITH n
LIMIT 50000
DETACH DELETE n

Last Note

MATCH (n:`Graph::Note`)
RETURN n
ORDER BY n.note_id DESC
LIMIT 1

Running Cypher via Ruby

Delete All

Neo4j::ActiveBase.current_session.query('MATCH (n) WHERE NOT n:`Neo4j::Migrations::SchemaMigration` DETACH DELETE n')

Using the Ruby Models/Neo4j-API

Linking work orders together so that they show up in related work orders

def cite(references)
  pairs = references.zip(references.drop 1)
  pairs.each do |(r1, r2)|
    next unless r1 && r2
    from = Graph::WorkOrder.find_by(reference: r1) || Graph::WorkOrder.create!(reference: r1, created: Time.current, property_reference: '0', source: 'console')
    to = Graph::WorkOrder.find_by(reference: r2) || Graph::WorkOrder.create!(reference: r2, created: Time.current, property_reference: '0', source: 'console')
    Graph::Citation.create!(from_node: from, to_node: to, extra: from.related.include?(to), source: 'console')
  end
end

cite(['00118196', '00118197', '00118198',
     '00118199', '00118200', '00118201', 
     '00118202', '00118203', '00118204', 
     '00118205'])