CipherStash
CipherStash Documentation

Querying Records

Since CipherStash is all about being a searchable encrypted data store, we pride ourselves on having a rich set of querying-related features.

Queries are executed with the Collection#query method. This requires the collection to first be loaded, which gives you the Collection object you need:

require "cipherstash/client"

stash = CipherStash::Client.new
collection = stash.collection("movies")

The Collection#query method does almost all its work in a block you pass to it. This block receives an object that you call methods on to define the query’s constraints, ordering, and so on.

A simple example looks like this:

res = collection.query do |movie|
  movie.title.match("Star Trek")
end

The res variable will now contain an instance of Collection::QueryResult. This has accessor methods to allow you to get the matching records, along with the value of any aggregates.

For example, you can print out all the movie titles that matched:

res.records.each do |r|
  puts "- #{r["title"]}"
end

You can modify the way that matching records are returned:

  • Limit and offset to restrict how many records are returned;
  • Ordering to specify how the returned records are sorted; and
  • Aggregates to perform server-side operations on the matching records.

CipherStash’s querying works entirely on encrypted indexes. Each index type provides its own set of querying operators and features, and so each has its own page.

  • Exact Indexes -- for all data types, when you need to find exactly equal values.
  • Range Query -- for inequality querying (less-than, greater-than, etc) on numeric data.
  • Match Query -- full-text search on string data in pre-defined fields.
  • Dynamic match query -- full-text search across all string data in the record.
  • Field dynamic match query -- full-text search on string data in arbitrary fields.