[Java programming language only]

ObjectQuery tutorial - step 2

With the following steps, you can continue to create an ObjectGrid with one map and an index, along with a schema for the map. Then you can insert an object into the cache and later retrieve it using a simple query.

Before you begin

Be sure that you have completed ObjectQuery tutorial - step 1 before proceeding with this step of the tutorial.

Procedure

Schema and index
Application.java


// Create an index
    HashIndex idx= new HashIndex();
    idx.setName("theItemName");
    idx.setAttributeName("itemName");
    idx.setRangeIndex(true);
    idx.setFieldAccessAttribute(true);
    orderBMap.addMapIndexPlugin(idx);
}
The index must be a com.ibm.websphere.objectgrid.plugins.index.HashIndex instance with the following settings:
  • The Name is arbitrary, but must be unique for a given BackingMap.
  • The AttributeName is the name of the field or bean property which the indexing engine uses to introspect the class. In this case, it is the name of the field for which you will create an index.
  • RangeIndex must always be true.
  • FieldAccessAttribute should match the value set in the QueryMapping object when the query schema was created. In this case, the Java™ object is accessed using the fields directly.

When a query runs that filters on the itemName field, the query engine automatically uses the defined index. Using the index allows the query to run much faster and a map scan is not needed. The next step demonstrates how an index can be used to optimize the query.

Next step