Search code examples
solrcassandradatastax-enterprisedatastax-java-driver

Using PreparedStatement with solr_query in DataStax Enterprise


I am trying to use PreparedStatement with solr_query using the following code:

PreparedStatement preparedStatement = cassandraSession.prepare(
    "SELECT * FROM users WHERE solr_query = 'username:? OR email:?';"
);

BoundStatement boundStatement = new BoundStatement(preparedStatement);

ResultSet results = cassandraSession.execute(
    boundStatement.bind(
        username,
        email
    )
);

When I execute the code above, I am getting the following exception: java.lang.IllegalArgumentException: Prepared statement has only 0 variables, 2 values provided

How can I use prepared statements with solr_query properly?

I am using DataStax Enterprise 4.5.3 which uses Cassandra 2.0.11.82 and Solr 4.6.0.2.8. I am using the DataStax Java driver.


Solution

  • Sorry, but CQL does not support variable substitution within string constants, only for a complete string constant. The Solr query string is parsed again on every SELECT statement execution anyway, so having the Solr query string "prepared" would save no overhead, so you should simply supply the entire Solr query string as a prepared statement variable, as in "WHERE solr_query = ?".