lucky_search
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!
Installation
-
Add the dependency to your
shard.yml
:dependencies: lucky_search: github: confact/lucky_search
-
Run
shards install
Usage
Add following
require "lucky_search"
To the shards.cr
Settings
We use ENV variables for the elasticsearch settings:
- ELASTIC_URI - default: nil
- ELASTIC_HOST - default: 127.0.0.1
- 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")
end
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
}
end
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
lucky_search_reindex(Model)
end
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.
Query
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
add_lucky_search(User)
end
You can now search by: UserQuery.search("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 = UserQuery.search(query)
check more in the Query documentation
Development
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
Contributing
- Fork it (https://github.com/confact/lucky_search/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
Contributors
- Håkan Nylén - creator and maintainer
- Caspian Baska - creator and maintainer of Neuroplastic, which this library is based on