-
Notifications
You must be signed in to change notification settings - Fork 275
Graph Indices
An IndexableGraph
is a Graph
that supports the indexing of its vertices and edges. An index is a data structure that allows for the fast retrieval of an element by a particular key/value pair. The IndexableGraph
interface has the following methods:
public <T extends Element> Index<T> createIndex(String indexName, Class<T> indexClass, Index.Type type);
public <T extends Element> Index<T> getIndex(String indexName, Class<T> indexClass);
public Iterable<Index<? extends Element>> getIndices();
public void dropIndex(String indexName);
There are two types of indices: Type.AUTOMATIC
and Type.MANUAL
. A manual index requires the developer to manually put, get, and remove elements from the index. To create a manual index of vertices, do the following:
Index<Vertex> index = graph.createIndex("test-idx", Vertex.class, Index.Type.MANUAL);
The Index
interface has the following methods:
public String getIndexName();
public Class<T> getIndexClass();
public Type getIndexType();
public void put(String key, Object value, T element);
public Iterable<T> get(String key, Object value);
public void remove(String key, Object value, T element);
Given the index
object created previous, to add, query, and remove a vertex from this index, do the following:
index.put("name","peter",vertex);
Iterable<Vertex> results = index.get("name","peter");
index.remove("name","peter",vertex);
With manual indices, the developer must be cognizant of maintaining the index with these methods.
When the developer does not wish to maintain an index, the developer can rely on indices of Type.AUTOMATIC
. An automatic index will automatically put
and remove
elements from an index as the element mutates. That is, an automatic index indexes the properties of an element where the property key is the key and the property value is the value. As these properties change, an automatic index automatically reflects these changes. To create an automatic index, do the following:
Index<Vertex> autoIndex = graph.createIndex("test-aidx", Vertex.class, Type.AUTOMATIC);
The AutomaticIndex
interface extends Index
and provides, along with the Index
methods, the following methods:
public void addAutoIndexKey(String key);
public void removeAutoIndexKey(String key);
public Set<String> getAutoIndexKeys();
An AutomaticIndex
, when initially created will index all newly created vertices by all their properties. This is because getAutoIndexKeys()
returns null
, where null
serves to denote wildcard. To restrict the index to be aware of only certain properties, use addAutoIndexKey(String key)
. Here is an example of all this together:
autoIndex.addAutoIndexKey("name");
Vertex a = graph.addVertex(null);
a.setProperty("name","pavel");
a.setProperty("country","belarus");
Iterable<Vertex> results = autoIndex.get("name","pavel");
// results contains vertex a
results = autoIndex.get("country","belarus");
// results does not contain vertex a
Note: When an IndexableGraph
is created a new (not constructed, but when there is no historic persistence), it comes with two automatic indices named Index.VERTICES
and Index.EDGES
. These indices automatically index any created vertices and edges, respectively. If these indices are not desired, simply graph.dropIndex(Index.VERTICES)
and graph.dropIndex(Index.EDGES)
before adding elements.