Cassandra lucene queries with UDT
Published in
2 min readFeb 23, 2018
Create keyspace
CREATE KEYSPACE IF NOT EXISTS chainz
WITH REPLICATION = {
'class' : 'SimpleStrategy',
'replication_factor': 1
};
Create UDT
CREATE TYPE chainz.event(
type text,
user text
);
Create table
CREATE TABLE chainz.document(
id UUID PRIMARY KEY,
type text,
date text,
events set<frozen<event>>
);
Create lucene index
CREATE CUSTOM INDEX event_index ON chainz.document()
USING 'com.stratio.cassandra.lucene.Index'
WITH OPTIONS = {
'refresh_seconds': '1',
'schema': '{
fields: {
"events.type" : {type: "string"},
"events.user" : {type: "string"}
}
}'
};
Insert sample data
INSERT INTO document(id, type) VALUES(now(), 'INVOICE');INSERT INTO document(id, type, events) VALUES(now(), 'INVOICE', {{type: 'DELETE', user: 'ERANGA'}});INSERT INTO document(id, type, events) VALUES(now(), 'INVOICE', {{type: 'CREATE', user: 'ERANGA'}});INSERT INTO document(id, type, events) VALUES(now(), 'INVOICE', {{type: 'DELETE', user: 'PAGERO'}});INSERT INTO document(id, type, events) VALUES(now(), 'INVOICE', {});
Queries
1. All records
select * from document;
2. Match filter (single filter)
SELECT * FROM document WHERE expr(event_index, '{
filter: {
type: "match",
field: "events.type",
value: "CREATE"
}
}');
3. Match filter (multiple filters)
SELECT * FROM document WHERE expr(event_index, '{
filter: [
{
type: "match",
field: "events.type",
value: "CREATE"
},
{
type: "match",
field: "events.user",
value: "ERANGA"
}
]
}');
SELECT * FROM document WHERE expr(event_index, '{
filter: [
{
type: "match",
field: "events.type",
value: "CREATE"
},
{
type: "match",
field: "events.user",
value: "ERA"
}
]
}');
4. Wildcard filter
SELECT * FROM document WHERE expr(event_index, '{
filter: [
{
type: "match",
field: "events.type",
value: "CREATE"
},
{
type: "wildcard",
field: "events.user",
value: "ERA*"
}
]
}');