Using CQL

DSE Search supports production-grade implementation of CQL Solr queries. You can develop CQL-centric applications supporting full-text search without having to work with Solr-specific APIs.

You can use a solr_query expression in a SELECT statement to retrieve Solr data from Cassandra. In DataStax Enterprise 3.2 and later, CQL 3 tables, including compound primary keys and collections, are supported. CQL3 tables must already exist before creating the Solr core. Thrift/CQL2 tables are automatically created from the Solr schema if no table exists when creating the Solr core.

Synopsis

SELECT select expression
 FROM table
 [WHERE solr_query = 'search expression'] [LIMIT n]

<search expression> syntax is a Solr query string that conforms to the Lucene syntax and Solr query syntax. You enclose the Solr query string in single quotation marks. For example, after running the Wikipedia demo you can use these Solr query strings:

Type of Query Example Description
Field search 'title:natio* AND Kenya' You can use multiple fields defined in the schema: 'title:natio* AND body:CarlosAragonés'
Wildcard search 'Ken?a' Use ? or * for single or multi-character searches.
Fuzzy search 'Kenya~' Use with caution, many hits can occur.
Phrase search '"American football player"' Searches for the phrase enclosed in double quotation marks.
Proximity search '"football Bolivia"~10' Searches for football and Bolivia within 10 words of each other.
Range searches 'title:[football TO soccer}' Supports both inclusive and exclusive bounds using square brackets and curly braces, respectively.
Term boosting '"football"^4 "soccer"' By default, the boost factor is 1. Must be a positive number.
Boolean operator '+Macedonian football' AND, +, OR, NOT and - can be used.
Grouping '(football OR soccer) AND Carlos Aragonés' Use parentheses to group clauses.
Field grouping 'title:(+football +"Bolivia")' Use parentheses to group multiple clauses into one field.

Tips for using solr_query

If you use CQL to query DSE Search and do not get a response, the likely cause is incorrect solr_query syntax. In response to incorrect syntax, DSE Search simply returns no results and logs an error message in the Cassandra log. Check the Cassandra log, which is located in /var/log/cassandra by default.

A SELECT expression reads one or more records from a Cassandra table and returns a result-set of rows. Each row consists of a partition key and a collection of columns corresponding to the query. Unlike the projection in a SQL SELECT, there is no guarantee that the results will contain all of the columns specified. An error does not occur if you request non-existent columns. Check the Cassandra log for error messages.

LimitationIf you index a legacy table, which by definition uses the COMPACT STORAGE directive, in Solr you cannot use the key alias, for example id, in a SELECT statement, for example SELECT id FROM . . . Querying such a table returns an error because the table schema is updated from Thrift and the key alias is lost. A SELECT key FROM . . . returns the correct result.

CQL Example

To query the Wikipedia demo search results:

Procedure

  1. Connect to the cqlsh. On the Mac, for example:
    cd install_location/bin
    
    ./cqlsh
  2. Use the wiki keyspace and include the solr_query expression in a CQL select statement to find the titles in the table named solr that begin with the letters natio:
    USE wiki;
    
    SELECT title FROM solr WHERE solr_query='title:natio*';
    The output, sorted in lexical order, appears:
     title
    --------------------------------------------------------------------------
                                         Kenya national under-20 football team
                                           Bolivia national football team 2002
                                      Israel men's national inline hockey team
      List of French born footballers who have played for other national teams
                                           Bolivia national football team 1999
                                           Bolivia national football team 2001
                                           Bolivia national football team 2000