A Crystal client for Sonic search backend.
This is my first project writing crystal, and is a port of
sonic-ruby. This took about 4 hours, and went fairly smoothly. I've pegged the version to sonic-ruby
, so if development continues it will be in lock step on major and minor versions.
- I've commented with
#cr
for crystal specific modifications or#rb
for examples of how it was originally implemented. - RSpec
include_context
andinclude_examples
replaced with macros Sonic::Connection#socket
usesTCPSocket#open
rather thanTCPSocket#open
- Replaced
String#starts_with?('X ')
with=~ /^X /
. Sonic::Channels::Base
required all sub class methods to be defined so there's a block inbase.cr
there with empty method definitions.- Most messages passed to
Sonic::Channels::Base#execute
use aarr.join(" ")
rather than*arr
due to issues with variable sized Tuples.
Add following line to your shard.yml
:
dependencies:
sonic:
github: babelian/sonic-crystal
version: ~> 0.2.2
And then execute:
$ shards install
$ docker-compose run app guardian
Start with creating new client:
client = Sonic::Client.new('localhost', 1491, 'SecretPassword')
Now you can use #channel
method in order to connect to specific channels:
control = client.channel(:control)
ingest = client.channel(:ingest)
search = client.channel(:search)
Learn more about Sonic Channels.
# Init `ingest` channel
ingest = client.channel(:ingest)
# Add data to index
ingest.push('users', 'all', 1, 'Alexander Tipugin')
# => true
# Remove data from index
ingest.pop('users', 'all', 1, 'Alexander Tipugin')
# => 2
# Count collection/bucket/object items
ingest.count('users', 'all', 1)
# => 1
# Flush entire collection
ingest.flushc('users')
# => 1
# Flush entire bucket inside collection
ingest.flushb('users', 'all')
# => 1
# Flush specific object inside bucket
ingest.flusho('users', 'all', 1)
# => 2
# Init `search` channel
search = client.channel(:search)
# Find indexed object by term
search.query('users', 'all', 'tipugin')
# => 1
# Auto-complete word
search.suggest('users', 'all', 'alex')
# => alexander