2

Using the Cassandra python driver mapper, cqlengine, when creating a model with a map collection it seems it is only possible to create an Index on the map values

class Foo(Model):
    id = columns.UUID(partition_key=True)
    time_id = columns.UUID(primary_key=True, clustering_order='DESC')
    bar = columns.Map(columns.Ascii(), columns.Double(), index=True)

will result in a table like

cqlsh:baz> DESCRIBE foo;
    CREATE TABLE bar.asset_metric (
        id uuid,
        time_id timeuuid,
        bar map<ascii, double>,
        PRIMARY KEY (id, time_id)
    ) WITH CLUSTERING ORDER BY (time_id DESC)
CREATE INDEX index_foo_bar ON baz.foo (values(bar));

How do you make cqlengine create the index on the map keys instead?

Richard Mathie
  • 306
  • 3
  • 6

1 Answers1

1

Richard, Currently indexing map keys is not supported by cqlengine. The code that creates the cql index query which is run is located here if you are interested.

https://github.com/datastax/python-driver/blob/master/cassandra/cqlengine/management.py#L197-L201

This is going form the cql statement which will be executed. it's going form a query along lines of.

CREATE INDEX index_name_here ON keyspace.model_name_here ("map_name_here").

By default this is going to index the values. There is a way to index the keys of a map with pure cql using the KEYS prefix.

that query would look something like this CREATE INDEX index_name_here ON keyspace.model_name_here(KEYS(map_name_here));

You can read more about collection indexing in this blog post. http://www.datastax.com/dev/blog/cql-in-2-1

Unfortunately that feature isn't supported yet in cqlengine. You will have to drop into base driver and use cql directly to create that type of index.

Greg Bestland
  • 713
  • 5
  • 6
  • Looks like what we need is that the column class handles its `index_column_name` so that specialisation in the case of a map can be handled properly i.e. for values/keys/entries ect. I got sidetracked when trying to work this out myself as I was looking at https://github.com/datastax/python-driver/blob/master/cassandra/cqlengine/metadata.py#L1340-L1344 and thought that that was where the create index string was generated. – Richard Mathie Feb 20 '16 at 23:19