py2neo 查询语句

py2neo 查询语句

NodeMatcher

>>> from py2neo import Graph
>>> from py2neo.matching import *
>>> g = Graph()
>>> nodes = NodeMatcher(g)
>>> keanu = nodes.match("Person", name="Keanu Reeves").first()
>>> keanu
Node('Person', born=1964, name='Keanu Reeves')

可以对查询结果进行进一步查询:

按ID查找节点

>>> matches.get(1234)
Node('Person', name='Alice')

对查找结果排序

match.order_by("_.name", "max(_.a, _.b)")

根据where语句查询

>>> nodes.match("Person").where("_.born % 10 = 0").all()
[Node('Person', born=1950, name='Ed Harris'),
 Node('Person', born=1960, name='Hugo Weaving'),
 Node('Person', born=1940, name='Al Pacino'),
 Node('Person', born=1970, name='Jay Mohr'),
 Node('Person', born=1970, name='River Phoenix'),
 Node('Person', born=1940, name='James L. Brooks'),
 Node('Person', born=1960, name='Annabella Sciorra'),
 Node('Person', born=1970, name='Ethan Hawke'),
 Node('Person', born=1940, name='James Cromwell'),
 Node('Person', born=1950, name='John Patrick Stanley'),
 Node('Person', born=1970, name='Brooke Langton'),
 Node('Person', born=1930, name='Gene Hackman'),
 Node('Person', born=1950, name='Howard Deutch'),
 Node('Person', born=1930, name='Richard Harris'),
 Node('Person', born=1930, name='Clint Eastwood'),
 Node('Person', born=1940, name='John Hurt'),
 Node('Person', born=1960, name='John Goodman'),
 Node('Person', born=1980, name='Christina Ricci'),
 Node('Person', born=1960, name='Oliver Platt')]

match.where("_.name =~ 'J.*'")

=~是不等于的意思,.是任意字符,*是前面的字符大于等于0个。整体意思就是查到节点中name属性不以J开头的节点

判断是否为空

IS_NULL()

查询以…开头的节点

>>> nodes.match("Person", name=STARTS_WITH("Kevin")).all()
[Node('Person', born=1958, name='Kevin Bacon'),
 Node('Person', born=1957, name='Kevin Pollak')]

查询以…结尾的节点

>>> nodes.match("Person", name=ENDS_WITH("Wachowski")).all()
[Node('Person', born=1967, name='Andy Wachowski'),
 Node('Person', born=1965, name='Lana Wachowski')]

查询包含某个字符串的节点

>>> nodes.match("Person", name=CONTAINS("eve")).all()
[Node('Person', born=1967, name='Steve Zahn'),
 Node('Person', born=1964, name='Keanu Reeves')]

按正则表达式查找

>>> nodes.match("Person", name=LIKE("Ke.*n")).all()
[Node('Person', born=1958, name='Kevin Bacon'),
 Node('Person', born=1962, name='Kelly Preston')]

包含在列表当中

>>> nodes.match("Person", born=IN([1962, 1964, 1966])).all()
[Node('Person', born=1964, name='Keanu Reeves'),
 Node('Person', born=1962, name='Tom Cruise'),
 Node('Person', born=1962, name='Demi Moore'),
 Node('Person', born=1966, name='Kiefer Sutherland'),
 Node('Person', born=1962, name='Anthony Edwards'),
 Node('Person', born=1962, name='Kelly Preston'),
 Node('Person', born=1966, name='John Cusack'),
 Node('Person', born=1962, name="Rosie O'Donnell"),
 Node('Person', born=1966, name='Halle Berry'),
 Node('Person', born=1966, name='Matthew Fox')]

与查找

>>> nodes.match("Person", born=AND(GE(1964), LE(1966))).all()
[Node('Person', born=1965, name='John C. Reilly'),
 Node('Person', born=1964, name='Keanu Reeves'),
 Node('Person', born=1965, name='Lana Wachowski'),
 Node('Person', born=1966, name='Kiefer Sutherland'),
 Node('Person', born=1966, name='John Cusack'),
 Node('Person', born=1966, name='Halle Berry'),
 Node('Person', born=1965, name='Tom Tykwer'),
 Node('Person', born=1966, name='Matthew Fox')]

或查找

>>> nodes.match("Person", name=OR(STARTS_WITH("H"), ENDS_WITH("h"))).all()
[Node('Person', born=1960, name='Hugo Weaving'),
 Node('Person', born=1943, name='J.T. Walsh'),
 Node('Person', born=1941, name='Jim Cash'),
 Node('Person', born=1963, name='Helen Hunt'),
 Node('Person', born=1950, name='Howard Deutch'),
 Node('Person', born=1966, name='Halle Berry'),
 Node('Person', born=1985, name='Emile Hirsch')]

异或查找

>>> nodes.match("Person", name=XOR(STARTS_WITH("H"), ENDS_WITH("h"))).all()
[Node('Person', born=1960, name='Hugo Weaving'),
 Node('Person', born=1943, name='J.T. Walsh'),
 Node('Person', born=1941, name='Jim Cash'),
 Node('Person', born=1963, name='Helen Hunt'),
 Node('Person', born=1966, name='Halle Berry'),
 Node('Person', born=1985, name='Emile Hirsch')]

限制查找的数量

>>> nodes.match("Person").where(name=LIKE("K.*")).order_by("_.name").limit(3).all()
[Node('Person', born=1964, name='Keanu Reeves'),
 Node('Person', born=1957, name='Kelly McGillis'),
 Node('Person', born=1962, name='Kelly Preston')]

你可能感兴趣的:(算法,python,neo4j,py2neo)