Put this line into /app/assets/stylesheets/application.css:-
*= stub active_admin
*= stub active_admin
sudo yum -y update sudo ln -sf /usr/share/zoneinfo/America/Indianapolis \ /etc/localtime sudo yum -y install gcc make
wget http://redis.googlecode.com/files/redis-2.6.0-rc3.tar.gz tar xzf redis-2.6.0-rc3.tar.gz cd redis-2.6.0-rc3 make
sudo mkdir /etc/redis /var/lib/redis sudo cp src/redis-server src/redis-cli /usr/local/bin sudo cp redis.conf /etc/redis
sudo nano /etc/redis/redis.conf
[..] daemonize yes [..]
[..] bind 127.0.0.1 [..]
[..] dir /var/lib/redis [..]
wget https://raw.github.com/saxenap/install-redis-amazon-linux-centos/master/redis-server
sudo mv redis-server /etc/init.d sudo chmod 755 /etc/init.d/redis-server sudo nano /etc/init.d/redis-server
redis="/usr/local/bin/redis-server"
sudo chkconfig --add redis-server sudo chkconfig --level 345 redis-server on
sudo service redis-server start
brew install elasticsearchcurl -X GET http://localhost:9200/ in the terminal.
rails new demoapp
cd demoapp
echo "gem 'elasticsearch-rails'" >> Gemfile
echo "gem 'elasticsearch-model'" >> Gemfile
bundle
Create the Article model and populate the db with some data that we will search against later:
rails g model article title:string body:text
rake db:migrate
rails c
hello_world = Article.create title: 'Hello World', body: 'Lovely day!'
power_search = Article.create title: 'Power Search', body: 'Elasticsearch rules the world!'
(Those variable names will be used later in the examples, so keep them noted).
class Article < ActiveRecord::Base
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
end
Add this line to the top of the app Rakefile to include the handy elasticsearch rake tasks:require 'elasticsearch/rails/tasks/import'
The new tasks can be listed with bundle exec rake -D elasticsearch.bundle exec rake environment elasticsearch:import:all
We can do the very same thing in the rails console:Article.import
search = Article.search 'world'
This will return an object with class Elasticsearch::Model::Response::Responseresultsmethod, which conveniently wraps the JSON returned by Elasticsearch in ruby objects:
search.results.map { |res| res.title }
# => ["Hello World", "Power Search"]
If you want to see the actual found records, then you just need to call records on that object:search.records
The above query could be rewritten using Elasticsearch JSON DSL syntax like follows:Article.search('{"query": {"match": {"_all": "world"}}}').records
Luckily the gem allows us to use both JSON or ruby hash syntax and of course we prefer the latter, don’t we? All the following examples will use ruby hashes.title field:
records = Article.search(query: {multi_match: {query: 'world', fields: ['title^10', 'body']}}).records
records.size # => 2
records.first == hello_world # => true
Let’s change the records order boosting the body field instead:
records = Article.search(query: {multi_match: {query: 'world', fields: ['title', 'body^10']}}).records
records.first == power_search # => true
fuzziness param:
records = Article.search(query: {match: {_all: {query: 'wold', fuzziness: 2}}}).records
records.first.title # => "Hello World"
fuzziness represents the maximum allowed Levenshtein distance, it accepts an integer between 0 and 2 (where 2 means the fuzziest search possible) or the string “AUTO” which will generate an edit distance based on the charachers length of the terms in the query.<a href='http://www.facebook.com/sharer.php?s=100&p[title]=WEB1CHANNEL&p[summary]=Web+Design+Company+in+Dubai.&p[url]=http://www.webchannel.ae&p[images][0]=http://qacode.com/test.jpg'>Facebook Share</a>