Search code examples
javahazelcastdistributed-cachinghazelcast-imapcomposite-index

How to create Composite index in Hazelcast


I am trying to improve performance of Hazelcast lookup by using composite key. I have a class entity

Class Entity {
    private Long id;
    private String field1;
    private String field2;
    private String field3;
    // getter and setters
}

I have added a composite index comprising of above 3 fields in hazelcast-server.xml

...
<map name="Entity">
    <max-idle-seconds>2678400</max-idle-seconds>
    <time-to-live-seconds>2678400</time-to-live-seconds>
    <backup-count>3</backup-count>
    <async-backup-count>3</async-backup-count>
    <read-backup-data>true</read-backup-data>
        <indexes>
        <index ordered="false">field1, field2, field3</index>
    </indexes>
</map>
...

Querying Hazelcast map

EntryObject entryObject = new PredicateBuilder().getEntryObject();
PredicateBuilder predicate = entryObject.get("field1").equal("value1")
    .and(entryObject.get("field2").equal("value2"))
    .and(entryObject.get("field3").equal("value3"));
IMap<Long, Entity> entityCache = hazelcastInstance.getMap("Entity")
List<Entity> routings = new ArrayList<>(entityCache.values(predicate));

The code is working fine with and without the index.

Questions

  1. Is this the correct way of creating and using composite index?
  2. Is there a way to check if the index is actually being used by the query? (I could not get any index related info on hazelcast management-center console)

I have scanned a lot hazelcast documentation and internet forums but could not find concrete answers. Hazelcast version: 3.12; Java version: 8


Solution

  • The only way I've found is IMap.getLocalMapStats().getIndexStats() as described here: https://docs.hazelcast.org/docs/3.12.1/manual/html-single/index.html#map-index-statistics