This topic explains how to create, list, and remove indexes in VMware Tanzu GemFire.
The Tanzu GemFire 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.
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.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 --expression=id --region=/exampleRegion --type=key
For more examples, see Index Commands in Basic GemFire gfsh Commands by Functional Area.
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, the type defaults to range
.
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
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