[英]How to use elasticsearch in rails
I am totally new in elastic search. 我是弹性搜索的新手。 i installed the elastic search in my system. 我在系统中安装了弹性搜索。 at localhost:9200 it is showing 在localhost:9200处显示
{
"ok" : true,
"status" : 200,
"name" : "Unseen",
"version" : {
"number" : "0.90.10",
"build_hash" : "0a5781f44876e8d1c30b6360628d59cb2a7a2bbb",
"build_timestamp" : "2014-01-10T10:18:37Z",
"build_snapshot" : false,
"lucene_version" : "4.6"
},
"tagline" : "You Know, for Search"
}
I included these two gems in my gem file. 我在宝石文件中包含了这两个宝石。
gem 'tire'
gem 'elasticsearch'
I have a movie model & a controller called search_controller. 我有一个电影模型和一个名为search_controller的控制器。
movie.rb movie.rb
class Movie < ActiveRecord::Base
include Tire::Model::Search
include Tire::Model::Callbacks
mapping do
indexes :title
end
def self.search(params)
binding.pry
tire.search(load: true, page: 1, per_page: 10) do
query { string params[:query]} if params[:query].present?
end
migration for the movies model 迁移电影模型
class CreateMovies < ActiveRecord::Migration
def self.up
create_table :movies do |t|
t.string :title
t.timestamps
end
def self.down
drop_table movies
end
end
end
search_controller search_controller
class SearchController < ApplicationController
def index
if(params[:query]).present?
@results=Movie.search(params)
else
@results=[]
end
end
end
view/search/index
<h1>Search#index</h1>
<p>Find me in app/views/search/index.html.erb</p>
<%= form_tag search_index_path, method: :get do %>
<p>
<%= text_field_tag :query, params[:query] %>
<%= submit_tag "Search", name: nil %>
</p>
<% end %>
<% if @results %>
<% @results.each do |fetchresults| %>
<%= fetchresults.title %>
<%= fetchresults.year %><br/>
<% end %>
<% end %>
routes.rb resource :search root 'search#index' route.rb资源:搜索根目录“ search#index”
i know i should index my records to use it in elastic search,but not getting any clue how & where to save those files,& how to use them. 我知道我应该索引我的记录以在弹性搜索中使用它,但不知道如何以及在何处保存这些文件以及如何使用它们。 when i am trying to search it is showing an error 当我尝试搜索时显示错误
404 : {"error":"IndexMissingException[[movies] missing]","status":404}
When I added elasticsearch to my rails application I used searchkick: 当我将Elasticsearch添加到我的Rails应用程序中时,我使用了searchkick:
https://github.com/ankane/searchkick https://github.com/ankane/searchkick
It is really user friendly and fast to set up. 它真的是用户友好和快速设置。 You can use rake searchkick:reindex:all
command to index all data. 您可以使用rake searchkick:reindex:all
命令来索引所有数据。
It's been a while since I worked with tire, but from the top of my head: 自从我从事轮胎工作已经有一段时间了,但是从我的头顶开始:
Movie
object (eg Move.create(...)
). 解决方案1:启动Rails控制台并保存一个新的Movie
对象(例如Move.create(...)
)。 This might create the index for you, not sure though. 这可能会为您创建索引,但不确定。 rake environment tire:import:all
in the project's root folder This should reindex all your models into ElasticSearch. 解决方案2:运行rake environment tire:import:all
在项目的根文件夹中这应该将所有模型重新索引到ElasticSearch中。 Hope that helps. 希望有所帮助。
Update: 更新:
index = Tire::Index.new('oldskool')
index.delete
index.create
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.