API: Legacy

class py2neo.legacy.LegacyNode(*labels, **properties)[source]

Legacy Node object for pre-2.0 servers. Does not synchronise label information with server.

class py2neo.legacy.Index(content_type, uri, name=None)[source]

Searchable database index which can contain either nodes or relationships.

See also

Graph.get_or_create_index()

add(key, value, entity)[source]

Add an entity to this index under the key:value pair supplied:

# create a node and obtain a reference to the "People" node index
alice, = graph.create({"name": "Alice Smith"})
people = graph.get_or_create_index(neo4j.Node, "People")

# add the node to the index
people.add("family_name", "Smith", alice)

Note that while Neo4j indexes allow multiple entities to be added under a particular key:value, the same entity may only be represented once; this method is therefore idempotent.

add_if_none(key, value, entity)[source]

Add an entity to this index under the key:value pair supplied if no entry already exists at that point:

# obtain a reference to the "Rooms" node index and
# add node `alice` to room 100 if empty
rooms = graph.get_or_create_index(neo4j.Node, "Rooms")
rooms.add_if_none("room", 100, alice)

If added, this method returns the entity, otherwise None is returned.

content_type

Return the type of entity contained within this index. Will return either Node or Relationship.

create(key, value, abstract)[source]

Create and index a new node or relationship using the abstract provided.

create_if_none(key, value, abstract)[source]

Create a new entity with the specified details within the current index, under the key:value pair supplied, if no such entity already exists. If creation occurs, the new entity will be returned, otherwise None will be returned:

# obtain a reference to the "Contacts" node index and
# create a node for Alice if one does not already exist
contacts = graph.get_or_create_index(neo4j.Node, "Contacts")
alice = contacts.create_if_none("name", "SMITH, Alice", {
    "given_name": "Alice Jane", "family_name": "Smith",
    "phone": "01234 567 890", "mobile": "07890 123 456"
})
get(key, value)[source]

Fetch a list of all entities from the index which are associated with the key:value pair supplied:

# obtain a reference to the "People" node index and
# get all nodes where `family_name` equals "Smith"
people = graph.get_or_create_index(neo4j.Node, "People")
smiths = people.get("family_name", "Smith")
get_or_create(key, value, abstract)[source]

Fetch a single entity from the index which is associated with the key:value pair supplied, creating a new entity with the supplied details if none exists:

# obtain a reference to the "Contacts" node index and
# ensure that Alice exists therein
contacts = graph.get_or_create_index(neo4j.Node, "Contacts")
alice = contacts.get_or_create("name", "SMITH, Alice", {
    "given_name": "Alice Jane", "family_name": "Smith",
    "phone": "01234 567 890", "mobile": "07890 123 456"
})

# obtain a reference to the "Friendships" relationship index and
# ensure that Alice and Bob's friendship is registered (`alice`
# and `bob` refer to existing nodes)
friendships = graph.get_or_create_index(neo4j.Relationship, "Friendships")
alice_and_bob = friendships.get_or_create(
    "friends", "Alice & Bob", (alice, "KNOWS", bob)
)
name

Return the name of this index.

query(query)[source]

Query the index according to the supplied query criteria, returning a list of matched entities:

# obtain a reference to the "People" node index and
# get all nodes where `family_name` equals "Smith"
people = graph.get_or_create_index(neo4j.Node, "People")
s_people = people.query("family_name:S*")

The query syntax used should be appropriate for the configuration of the index being queried. For indexes with default configuration, this should be Apache Lucene query syntax.

remove(key=None, value=None, entity=None)[source]

Remove any entries from the index which match the parameters supplied. The allowed parameter combinations are:

key, value, entity
remove a specific entity indexed under a given key-value pair
key, value
remove all entities indexed under a given key-value pair
key, entity
remove a specific entity indexed against a given key but with any value
entity
remove all occurrences of a specific entity regardless of key and value
class py2neo.legacy.LegacyReadBatch(graph)[source]

Generic batch execution facility for data read requests,

get_indexed_nodes(index, key, value)[source]

Fetch all nodes indexed under a given key-value pair.

Parameters:
  • index (str or Index) – index name or instance
  • key (str) – key under which nodes are indexed
  • value – value under which nodes are indexed
Returns:

batch request object

class py2neo.legacy.LegacyWriteBatch(graph)[source]

Generic batch execution facility for data write requests. Most methods return a BatchRequest object that can be used as a reference in other methods. See the create method for an example of this.

add_to_index(cls, index, key, value, entity)[source]

Add an existing node or relationship to an index.

Parameters:
  • cls (Node or Relationship) – the type of indexed entity
  • index (Index or str) – index or index name
  • key (str) – index entry key
  • value – index entry value
  • entity (concrete or reference) – node or relationship to add to the index
Returns:

batch request object

add_to_index_or_fail(cls, index, key, value, entity)[source]

Add an existing node or relationship uniquely to an index, failing the entire batch if such an entry already exists.

Warning

Uniqueness modes for legacy indexes have been broken in recent server versions and therefore this method may not work as expected.

Parameters:
  • cls (Node or Relationship) – the type of indexed entity
  • index (Index or str) – index or index name
  • key (str) – index entry key
  • value – index entry value
  • entity (concrete or reference) – node or relationship to add to the index
Returns:

batch request object

create_in_index_or_fail(cls, index, key, value, abstract=None)[source]

Create a new node or relationship and add it uniquely to an index, failing the entire batch if such an entry already exists.

Warning

Uniqueness modes for legacy indexes have been broken in recent server versions and therefore this method may not work as expected.

Parameters:
  • cls (Node or Relationship) – the type of indexed entity
  • index (Index or str) – index or index name
  • key (str) – index entry key
  • value – index entry value
  • abstract – abstract node or relationship to create
Returns:

batch request object

get_or_add_to_index(cls, index, key, value, entity)[source]

Fetch a uniquely indexed node or relationship if one exists, otherwise add an existing entity to the index.

Parameters:
  • cls (Node or Relationship) – the type of indexed entity
  • index (Index or str) – index or index name
  • key (str) – index entry key
  • value – index entry value
  • entity (concrete or reference) – node or relationship to add to the index
Returns:

batch request object

get_or_create_in_index(cls, index, key, value, abstract=None)[source]

Fetch a uniquely indexed node or relationship if one exists, otherwise create a new entity and add that to the index.

Parameters:
  • cls (Node or Relationship) – the type of indexed entity
  • index (Index or str) – index or index name
  • key (str) – index entry key
  • value – index entry value
  • abstract – abstract node or relationship to create
Returns:

batch request object

remove_from_index(cls, index, key=None, value=None, entity=None)[source]

Remove any nodes or relationships from an index that match a particular set of criteria. Allowed parameter combinations are:

key, value, entity
remove a specific node or relationship indexed under a given key-value pair
key, entity
remove a specific node or relationship indexed against a given key and with any value
entity
remove all occurrences of a specific node or relationship regardless of key or value
Parameters:
  • cls (Node or Relationship) – the type of indexed entity
  • index (Index or str) – index or index name
  • key (str) – index entry key
  • value – index entry value
  • entity (concrete or reference) – node or relationship to remove from the index
Returns:

batch request object