5.5.2 Using Automatic Indexes for Property Graph Data

An automatic text index provides automatic indexing of vertices or edges by a set of property keys. Its main purpose is to increase the speed of lookups over vertices and edges based on particular key/value pair. If an automatic index for the given key is enabled, then key/value pair lookups will be performed as a text search against the index instead of as a database lookup.

When specifying an automatic index over a property graph, use the following methods to create, remove, and manipulate an automatic index:

  • OraclePropertyGraph.createKeyIndex(String key, Class elementClass, Parameter[] parameters): Creates an automatic index for all elements of type elementClass by the given property key. The index is configured based on the specified parameters.

  • OraclePropertyGraph.createKeyIndex(String[] keys, Class elementClass, Parameter[] parameters): Creates an automatic index for all elements of type elementClass by using a set of property keys. The index is configured based on the specified parameters.

  • OraclePropertyGraph.dropKeyIndex(String key, Class elementClass): Drops the automatic index for all elements of type elementClass for the given property key.

  • OraclePropertyGraph.dropKeyIndex(String[] keys, Class elementClass): Drops the automatic index for all elements of type elementClass for the given set of property keys.

  • OraclePropertyGraph.getAutoIndex(Class elementClass): Gets an index instance of the automatic index for type elementClass.

  • OraclePropertyGraph.getIndexedKeys(Class elementClass): Gets the set of indexed keys currently used in an automatic index for all elements of type elementClass.

By default, indexes are configured based on the OracleIndexParameters associated with the property graph using the method opg.setDefaultIndexParameters(indexParams).

Indexes can also be created by specifying a different set of parameters. This is shown in the following code snippet.

// Create an OracleIndexParameters object to get Index configuration (search engine, etc).
OracleIndexParameters indexParams = OracleIndexParameters.buildFS(args)  
 
// Create auto indexing on above properties for all vertices
opg.createKeyIndex("name", Vertex.class, indexParams.getParameters());

The code fragment in the next example executes a query over all vertices to find all matching vertices with the key/value pair name:Robert Smith. This operation will execute a lookup into the text index.

Additionally, wildcard searches are supported by specifying the parameter useWildCards in the getVertices API call. Wildcard search is only supported when automatic indexes are enabled for the specified property key.

// Find all vertices with name Robert Smith. 
    Iterator<Vertices> vertices = opg.getVertices("name", "Robert Smith").iterator();
    System.out.println("----- Vertices with name Robert Smith -----");
    countV = 0;
    while (vertices.hasNext()) {
      System.out.println(vertices.next());
      countV++;
    }
    System.out.println("Vertices found: " + countV);
 
   // Find all vertices with name including keyword "Smith"
   // Wildcard searching is supported.
    boolean useWildcard = true;
    Iterator<Vertices> vertices = opg.getVertices("name", "*Smith*").iterator();
    System.out.println("----- Vertices with name *Smith* -----");
    countV = 0;
    while (vertices.hasNext()) {
      System.out.println(vertices.next());
      countV++;
    }
    System.out.println("Vertices found: " + countV);

The preceding code example produces output like the following:

----- Vertices with name Robert Smith-----
Vertex ID 1 {name:str:Robert Smith, role:str:political authority, occupation:str:CEO of Example Corporation, country:str:United States, political party:str:Bipartisan, religion:str:Unknown}
Vertices found: 1
 
----- Vertices with name *Smith* -----
Vertex ID 1 {name:str:Robert Smith, role:str:political authority, occupation:str:CEO of Example Corporation, country:str:United States, political party:str:Bipartisan, religion:str:Unknown}
Vertices found: 1