Skip to main content

Creating, Listing and Removing Indexes

The Geode QueryService API provides methods to create, list and remove the index. You can also use gfsh command-line interface to create, list and remove indexes, and use cache.xml to create an index.

Creating Indexes​

Indexes can be created programmatically, by using the gfsh command line interface or by using cache.xml.

To create an index, use one of the following QueryService methods:

  • createIndex. Creates the default type of index, a range index. Use this type of index if you will be writing queries that will be doing any kind of comparison operation besides an equality comparison.
  • createKeyIndex. Creates a key index. See Creating Key Indexes for more information.
  • Deprecated. createHashIndex. Creates a hash index. See Creating Hash Indexes for more information.
  • createDefinedIndexes. Creates multiple indexes that were previously defined using defineIndex. See Creating Multiple Indexes at Once for more information.

The following sections provide examples of index creation:

Using gfsh:

gfsh> create index --name=myIndex --expression=status --region=/exampleRegion
gfsh> create index --name=myKeyIndex --type=key --expression=id --region=/exampleRegion

See Index Commands for more examples.

Using Java API:

QueryService qs = cache.getQueryService();
qs.createIndex("myIndex", "status", "/exampleRegion");
qs.createKeyIndex("myKeyIndex", "id", "/exampleRegion");

Using cache.xml:

<region name=exampleRegion>
<region-attributes . . . >
</region-attributes>
<index name="myIndex" from-clause="/exampleRegion" expression="status"/>
<index name="myKeyIndex" from-clause="/exampleRegion" expression="id" key-index="true"/>
...
</region>

Note: If you do not specify the type of index in cache.xml, the type defaults to "range".

Listing Indexes​

To retrieve a list of indexes from the cache or region, use the QueryService.getIndexes method or the gfsh command line interface.

Using gfsh:

gfsh> list indexes
gfsh> list indexes --with-stats

Using Java API:

QueryService qs = cache.getQueryService();
qs.getIndexes(); //returns a collection of all indexes in the cache
qs.getIndexes(exampleRegion); //returns a collection of all indexes in exampleRegion
qs.getIndexes(exampleRegion, myKeyIndex); //returns the index named myKeyIndex from the exampleRegion

Removing Indexes​

To remove an index or all indexes from the cache or region, use the QueryService.removeIndexes method or the gfsh command line interface.

Using gfsh:

gfsh> destroy index
gfsh> destroy index --name=myIndex
gfsh> destroy index --region=/exampleRegion

Using Java API:

QueryService qs = cache.getQueryService();
qs.removeIndexes(); //removes all indexes from the cache
qs.removeIndexes(myKeyIndex); //removes the index named myKeyIndex
qs.removeIndexes(exampleRegion); //removes all indexes from the exampleRegion