LATEST VERSION: 9.0.4 - CHANGELOG
Pivotal GemFire® v9.0

Creating Multiple Indexes at Once

In order to speed and promote efficiency when creating indexes, you can define multiple indexes and then create them all at once.

Defining multiple indexes before creating them speeds up the index creation process by iterating over region entries only once.

You can define multiple indexes of different types at once by specifying the --type parameter at definition time.

To define multiple indexes, you can use gfsh or the Java API:

gfsh example:

gfsh> define index --name=myIndex1 --expression=exp1 --region=/exampleRegion 

gfsh> define index --name=myIndex2 --expression="c.exp2" --region="/exampleRegion e, e.collection1 c" 

gfsh> define index --name=myIndex3 --expression=exp3 --region=/exampleRegion --type=hash 

gfsh> create defined indexes

If index creation fails, you may receive an error message in gfsh similar to the following:

gfsh>create defined indexes
Exception : org.apache.geode.cache.query.RegionNotFoundException , 
Message : Region ' /r3' not found: from  /r3Occurred on following members
1. india(s1:17866)<v1>:27809

Java API example:

 Cache cache = new CacheFactory().create();
    QueryService queryService = cache.getQueryService();
    queryService.defineIndex("name1", "indexExpr1", "regionPath1");
    queryService.defineIndex("name2", "indexExpr2", "regionPath2");
    queryService.defineHashIndex("name3", "indexExpr3", "regionPath2");
    queryService.defineKeyIndex("name4", "indexExpr4", "regionPath2");
    List<Index> indexes = queryService.createDefinedIndexes();

If one or more index population fails, Geode collect the Exceptions and continues to populate the rest of the indexes. The collected Exceptions are stored in a Map of index names and exceptions that can be accessed through MultiIndexCreationException.

Index definitions are stored locally on the gfsh client. If you want to create a new set of indexes or if one or more of the index creations fail, you might want to clear the definitions stored by using clear defined indexescommand. The defined indexes can be cleared by using the Java API:

queryService.clearDefinedIndexes();

or gfsh:

gfsh> clear defined indexes