UDT query examples

You can query nested tuples and UDTs inside CQL lists and sets. A UDT facilitates handling multiple fields of related information in a table. UDTs are a specialization of tuples. In these examples, {!tuple} applies to both UDTs and tuples.

Selecting an entire UDT column in the CQL SELECT clause is supported. Selecting individual fields of a UDT is supported for unfrozen tuples.

Querying fields

{!tuple}address.street:sesame

Querying dynamic fields

<dynamicField name="user.position_*" type="text" indexed="true"  stored="true"/>
{!tuple}user.position_day1:second
{!tuple}user.position_day2:first

Querying collections

{!tuple}user.hobbies:swim

Querying across different UDT/tuple fields

+{!tuple v='father.name.firstname:Sam'} +{!tuple v='mother.name.firstname:Anne'}

In CQL, to use a single quotation mark in a string literal, you must escape it using a single quotation mark (so you’ll need to double the single quotation marks). See CQL escaping characters.

You can also use the discouraged syntax:

({!tuple}father.name.firstname:Sam AND {!tuple}mother.name.firstname:Anne)

Querying UDT/tuple fields with several conditions

You can find a tuple that satisfies several conditions. Notice how all the conditions are on the same tuple all the time. For example:

{!tuple v='address.residents.field1:Alice AND address.residents.field2:Smith'}

You can also use the discouraged syntax:

{!tuple}address.residents.field1:Alice AND address.residents.field2:Smith

The difference in syntax specifies to search across tuples or within a tuple.

  • Across tuples:

    +{!tuple v='condition1'} +{!tuple v='condition2'} +{!tuple v='condition<N>'} searches for documents that satisfy all conditions, but are not necessarily satisfied by the same single tuple/UDT.

  • Within a tuple:

    {!tuple v='condition1 AND condition2 AND condition<N>'} searches for documents that satisfy all conditions within a single tuple/UDT.

Querying nested tuples and UDTs

To query nested tuples and UDTs, use the same dot notation and the tuple query parser. Because UDTs are a specialization of tuples, use the tuple query parser for tuples and UDTs. In this example, the dot notation identifies address.resident as a UDT.

Query for locations that have a resident with the first name Alice using the nested address.residents tuple:

{!tuple}address.residents.field1:Alice

Query for locations with a resident that has the first name Alice and second name Smith:

+{!tuple v='address.residents.field1:Alice AND address.residents.field2:Smith'}

Tuples and UDTs are modelled internally as nested documents. The Apache Solr™ block join is used internally to query them. Parents are identified with the parent=true field. Children are identified with parent=false. For certain types of queries, including negative queries and empty field queries, you might need to use the parent field.

Querying for empty firstnames

The negation (-) and inclusion (+) operators must precede the {!tuple} directive:

-{!tuple}_parent_:false AND user.name.firstname:[* TO *]

Negative queries

Negative queries use this syntax:

select * from demo where solr_query='-{!tuple}name.firstname:*'

Negative queries with more than one condition must follow the Solr rules. Use this syntax:

{!tuple v='address.street:* NOT (address.street:sesame AND address.number:32)'}

or

-{!tuple v='address.street:sesame AND address.number:32'}

or

{!tuple}address.street:* NOT (address.street:sesame AND address.number:32)

Was this helpful?

Give Feedback

How can we improve the documentation?

© 2024 DataStax | Privacy policy | Terms of use

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: +1 (650) 389-6000, info@datastax.com