Table of Contents
We describe how to index scalar data types in Creating Indexes, and we show how to read using indexes in Reading Indexes. However, non-scalar data types (Arrays, Maps and Records) require more explanation, which we give here.
Index creation is accomplished using the CREATE INDEX
statement. See CREATE INDEX
for details on this statement.
You can create an index on an array field so long as the array contains scalar data, or contains a record with scalar fields.
You cannot index a map or array that is nested beneath another map or array. This is not allowed because of the potential for an excessively large number of index entries.
Be aware that indexing an array potentially results in multiple index entries for each row, which can lead to very large indexes.
To create the index, first create the table:
CREATE TABLE myArrayTable ( uid INTEGER, testArray ARRAY(STRING), PRIMARY KEY(uid) )
Once the table has been added to the store, create the index:
CREATE INDEX arrayFieldIndex on myArrayTable (testArray)
In the case of arrays, the field can be indexed only if the array contains values that are of one of the other indexable types. For example, you can create an index on an array of Integers. You can also create an index on a specific record in an array of records. Only one array should participate in an index, otherwise the size of the index can grow exponentially because there is an index entry for each array entry.
To retrieve data using an index of arrays, create a key that identifies the array field and value that you want to retrieve.
When you perform the index lookup, the only records that will be returned will be those which have an array with at least one item matching the value set for the key object. For example, if you have individual records that contain arrays like this:
Record 1: ["One," "Two", "Three"] Record 2: ["Two", "Three", "One"] Record 3: ["One", "Three", "One"] Record 4: ["Two", "Three", "Four"]
and you then perform an array lookup on the array value "One", then Records 1 - 3 will be returned, but not 4.
For example:
try: key_d = {"testArray" : ["One"]} row_list = store.index_iterator("myArrayTable", "arrayFieldIndex", key_d, False) if not row_list: logging.debug("Table retrieval failed") else: logging.debug("Table retrieval succeeded.") for r in row_list: print r except IllegalArgumentException, iae: logging.error("Table retrieval failed.") logging.error(iae.message)