熱門文章

2016年10月13日 星期四

[Rails][Gem] 搜尋引擎 searchkick

Gemfile 加入

gem 'searchkick'


bundle install

➜  eat git:(master) brew install elasticsearch
==> Downloading https://download.elasticsearch.org/elasticsearch/release/org/elasticsearch/distribution/tar/elasticsearch/2.3.4/elasticsearch-2.3.4.tar.gz
######################################################################## 100.0%
Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink bin/elasticsearch
/usr/local/bin is not writable.

You can try again using:
  brew link elasticsearch
==> Caveats
Data:    /usr/local/var/elasticsearch/elasticsearch_QJpioneer/
Logs:    /usr/local/var/log/elasticsearch/elasticsearch_QJpioneer.log
Plugins: /usr/local/Cellar/elasticsearch/2.3.4/libexec/plugins/
Config:  /usr/local/etc/elasticsearch/
plugin script: /usr/local/Cellar/elasticsearch/2.3.4/libexec/bin/plugin

To have launchd start elasticsearch now and restart at login:
  brew services start elasticsearch
Or, if you don't want/need a background service you can just run:
  elasticsearch
==> Summary

🍺  /usr/local/Cellar/elasticsearch/2.3.4: 59 files, 29.4M, built in 1 minute 16 seconds


➜  eat git:(master) brew link elasticsearch
Linking /usr/local/Cellar/elasticsearch/2.3.4... 
Error: Could not symlink bin/elasticsearch

/usr/local/bin is not writable.



重新安裝 homebrew
➜  eat git:(master) /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"



再試一次

➜  eat git:(master) brew install elasticsearch
==> Using the sandbox
==> Downloading https://download.elasticsearch.org/elasticsearch/release/org/elasticsearch/distribution/tar/elasticsearch/2.4.1/elasticsearch-2.4.1.tar.gz
######################################################################## 100.0%
==> Caveats
Data:    /usr/local/var/elasticsearch/elasticsearch_QJpioneer/
Logs:    /usr/local/var/log/elasticsearch/elasticsearch_QJpioneer.log
Plugins: /usr/local/Cellar/elasticsearch/2.4.1/libexec/plugins/
Config:  /usr/local/etc/elasticsearch/
plugin script: /usr/local/Cellar/elasticsearch/2.4.1/libexec/bin/plugin

To have launchd start elasticsearch now and restart at login:
  brew services start elasticsearch
Or, if you don't want/need a background service you can just run:
  elasticsearch
==> Summary

🍺  /usr/local/Cellar/elasticsearch/2.4.1: 60 files, 29.1M, built in 1 minute 14 seconds


查看安裝訊息

➜  eat git:(master) brew info elasticsearch
elasticsearch: stable 2.4.1, devel 5.0.0-beta1, HEAD
Distributed search & analytics engine
https://www.elastic.co/products/elasticsearch
/usr/local/Cellar/elasticsearch/2.3.4 (59 files, 29.4M)
  Built from source on 2016-10-12 at 06:08:24
/usr/local/Cellar/elasticsearch/2.4.1 (60 files, 29.1M) *
  Built from source on 2016-10-12 at 13:28:01
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/elasticsearch.rb
==> Requirements
Required: java >= 1.7
==> Caveats
Data:    /usr/local/var/elasticsearch/elasticsearch_QJpioneer/
Logs:    /usr/local/var/log/elasticsearch/elasticsearch_QJpioneer.log
Plugins: /usr/local/Cellar/elasticsearch/2.4.1/libexec/plugins/
Config:  /usr/local/etc/elasticsearch/
plugin script: /usr/local/Cellar/elasticsearch/2.4.1/libexec/bin/plugin

To have launchd start elasticsearch now and restart at login:
  brew services start elasticsearch
Or, if you don't want/need a background service you can just run:

  elasticsearch


在最下方有啟動方式

To have launchd start elasticsearch now and restart at login:
  brew services start elasticsearch
Or, if you don't want/need a background service you can just run:

  elasticsearch


我使用第一個方式

➜  eat git:(master) brew services start elasticsearch
==> Tapping homebrew/services
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-services'...
remote: Counting objects: 10, done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 10 (delta 0), reused 6 (delta 0), pack-reused 0
Unpacking objects: 100% (10/10), done.
Checking connectivity... done.
Tapped 0 formulae (36 files, 46.1K)

Service `elasticsearch` already started, use `brew services restart elasticsearch` to restart.





接著在瀏覽器輸入:http://localhost:9200 可以看到以下資訊

{
  "name" : "Yandroth",
  "cluster_name" : "elasticsearch_QJpioneer",
  "cluster_uuid" : "KJCz3pnURXGzuM5Q2mCzlQ",
  "version" : {
    "number" : "2.4.1",
    "build_hash" : "c67dc32e24162035d18d6fe1e952c4cbcbe79d16",
    "build_timestamp" : "2016-09-27T18:57:55Z",
    "build_snapshot" : false,
    "lucene_version" : "5.5.2"
  },
  "tagline" : "You Know, for Search"
}


Add searchkick to models you want to search.
現在,將 searchkick 加入到要實現搜尋的model


     


接著 command line

rake searchkick:reindex CLASS=Store

出現警告
➜  eat git:(master) rake searchkick:reindex CLASS=Store
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from <top (required)> at /Users/QJpioneer/railsbridge/railsfun/ruby12weeks/eat/config/application.rb:7)
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from <top (required)> at /Users/QJpioneer/railsbridge/railsfun/ruby12weeks/eat/config/application.rb:7)
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from <top (required)> at /Users/QJpioneer/railsbridge/railsfun/ruby12weeks/eat/config/application.rb:7)

DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from <top (required)> at /Users/QJpioneer/railsbridge/railsfun/ruby12weeks/eat/config/application.rb:7)


ake searchkick:reindex:all
還是錯誤,但最下面有reindex 完成

➜  eat git:(master) rake searchkick:reindex:all        
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from <top (required)> at /Users/QJpioneer/railsbridge/railsfun/ruby12weeks/eat/config/application.rb:7)
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from <top (required)> at /Users/QJpioneer/railsbridge/railsfun/ruby12weeks/eat/config/application.rb:7)
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from <top (required)> at /Users/QJpioneer/railsbridge/railsfun/ruby12weeks/eat/config/application.rb:7)
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from <top (required)> at /Users/QJpioneer/railsbridge/railsfun/ruby12weeks/eat/config/application.rb:7)
Reindexing Store...

Reindex complete



routes.rb

在該model 下加入

   collection do
  get 'search'
  end






Rails c

irb(main):002:0> Store.reindex
  Store Load (0.3ms)  SELECT  "stores".* FROM "stores" ORDER BY "stores"."id" ASC LIMIT ?  [["LIMIT", 1000]]
  Store Import (154.7ms)  {"count":13}

=> true



➜  eat git:(master) rake routes

就可以看到

           search_stores GET    /stores/search(.:format)                     stores#search






沒有留言:

張貼留言