Creating an Index

Learn to create an index on a field in a Oracle NoSQL Database table using Oracle NoSQL Database SDK for Spring Data.

To create an index on a field in an Oracle NoSQL Database table from the Spring Data Framework, you use NosqlTemplate.runTableRequest() method.

In the application, you instantiate the NosqlTemplate class by providing the NosqlTemplate.create(NosqlDbConfig nosqlDBConfig) method with the instance of the AppConfig class. You then modify the table using the NosqlTemplate.runTableRequest() method. You provide the NoSQL statement for the index creation in the NosqlTemplate.runTableRequest() method.

Example 2-5 Creating an Index on a table using Spring Data Framework

The following example shows how to create an index on the lastName field in the Student table.
 
/* Create an Index on the lastName field of the Users Table. */
 
try {
    AppConfig config = new AppConfig();
    NosqlTemplate idx = NosqlTemplate.create(config.nosqlDbConfig());
    idx.runTableRequest("CREATE INDEX IF NOT EXISTS nameIdx ON Student(kv_json_.lastName AS STRING)");
    System.out.println("Index created successfully");
} catch (Exception e) {
    System.out.println("Exception creating index" + e);
}

For details on table creation, see Accessing Oracle NoSQL Database Using Spring Data Framework.