-
Notifications
You must be signed in to change notification settings - Fork 2
Searchkick Elastic search 使用祕技
GEM: searchkick (優化比較好用,比官網好用(官網很多語法難用)) https://github.com/ankane/searchkick https://github.com/run26kimo/searchkick_example
#安裝步驟
brew install elasticsearch
# start the server
elasticsearch
gem 'searchkick'
class Product < ActiveRecord::Base
searchkick
end
##Automatic Failover
Create an initializer config/initializers/elasticsearch.rb with multiple hosts:
Searchkick.client = Elasticsearch::Client.new(hosts: ["localhost:9200", "localhost:9201"], retry_on_failure: true)
如果原本已有商品可以下指令讓他產生index到ELS裡面
Product.reindex
bin/rake searchkick:reindex:all RAILS_ENV=production
然後就是這麼簡單就可以開始用了
def search
search_params = { page: params[:page],
per_page: 10,
suggest: true,
highlight: { tag: "<mark>" },
aggs: [:category_name]
}
search_params.merge!(where: { category_name: params[:cn] }) if params[:cn].present?
@products = Product.search(params[:q], search_params)
用法如上
#如果要推上server How To Install and Configure Elasticsearch on Ubuntu 16.04 https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-elasticsearch-on-ubuntu-16-04 How To Install and Configure Elasticsearch on Ubuntu 14.04 https://www.rosehosting.com/blog/install-elasticsearch-on-ubuntu-14-04/ 先用上面這個 https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-elasticsearch-on-ubuntu-14-04 https://www.digitalocean.com/community/tutorials/how-to-set-up-a-production-elasticsearch-cluster-on-ubuntu-14-04
plugin: head WEB-UI用來監測的
http://www.searchly.com/
如果方法都裝不起來才要用可是只有5MB for free
searchjoy scope 避免n+1 synonyms 可以設定同義字 獲建立CVS檔案
搜尋的tree 要小一點 避免消耗太多記憶體 elastic 要跟rails app的server要分開 避免消耗太多記憶體