Skip to content


Folders and files

Last commit message
Last commit date

Latest commit



48 Commits

Repository files navigation


This is a WIP - feel free to help out!

This is an easy Elasticsearch library for the Lucky framework. It is highly inspired by Searchkick, but it still lacks some of the advanced features that Searchkick has. Now you can just index and search.

I also got a lot of help and borrowed code from neuroplastic - Thank you, Place Labs! I also borrowed some of the analysis from Searchkick to get stemming. Thank you, Searchkick!


  1. Add the dependency to your shard.yml:

        github: confact/lucky_search
  2. Run shards install


Add following

require "lucky_search"

To the


We use ENV variables for the elasticsearch settings:

  • ELASTIC_URI - default: nil
  • ELASTIC_HOST - default:
  • ELASTIC_PORT - default: 9200
  • ELASTIC_TLS - default: false
  • ELASTIC_POOLED - default: false
  • ELASTIC_CONN_POOL - default: 10
  • ELASTIC_IDLE_POOL - default: 10
  • ELASTIC_CONN_POOL_TIMEOUT - default: 5.0

You can also set this in a config file, like below:

LuckySearch::Client.configure do |config|
  config.uri = URI.parse("https://elastic:PrHfasu6fssfsd@localhost:9200")

config variables for the env above are:

  • URI
  • host
  • port
  • tls
  • pooled
  • pool_size
  • idle_pool_size
  • pool_timeout

Good to know:

  • TLS will be automatically set to true if URI is set and the scheme is https
  • we support basic auth in the URI

Operations and Model

Add include Searchable to the operations for the models you want to add to Elaticsearch. It adds hooks to update the index on saves and delete on delete.

You also need to have a search_data method in the model class that returns a hash of data you want to index. Example:

def search_data
    "name" => name,
    "last_campaign" => get_last_campaign,
    "age" => age

Right now, it has to be flat data (no hash in the hash)

Reindex whole table task

Create a new task and change the call method to contain:

name "search.reindex"
def call

Model being the model class you want to reindex.

call the task

lucky search:reindex

search:reindex being the name of the task you created above.


To do searches, we use Lucky's query classes. We created a macro to generate methods for you.

Use an existing query or create a new SearchUser query class.

add add_lucky_search(model) to the class, example below:

class UserQuery < User::BaseQuery

You can now search by:"name")

Advanced queries

Our Query class and wrapper around it comes from neuroplastic and works similarly, I have done some changes, but it works more or less the same.

You take out a Query class and set the filters and so on and then send it in the search method:

query = UserQuery.search_empty_query
query.must({"visits" => ["monthly"]})
records =

check more in the Query documentation


Install Elasticsearch and the shards by shards install. You need to run migration for the test models from Avram by running lucky db.create and lucky db.migrate. run the tests with crystal spec


  1. Fork it (
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request