Ruby on rails 如何在rails中使用elasticsearch
我对弹性搜索是全新的。 我在我的系统中安装了弹性搜索。 在localhost:9200上显示Ruby on rails 如何在rails中使用elasticsearch,ruby-on-rails,ruby,
elasticsearch,tire,Ruby On Rails,Ruby,
elasticsearch,Tire,我对弹性搜索是全新的。 我在我的系统中安装了弹性搜索。 在localhost:9200上显示 { "ok" : true, "status" : 200, "name" : "Unseen", "version" : { "number" : "0.90.10", "build_hash" : "0a5781f44876e8d1c30b6360628d59cb2a7a2bbb", "build_timestamp" : "2014-01-10T10:18:
{
"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"
}
我在我的gem文件中包括了这两个gem
gem 'tire'
gem 'elasticsearch'
我有一个电影模型&一个名为search\u controller的控制器
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
我已经有一段时间没有使用轮胎了,但从我的头顶上看:
- 解决方案1:启动rails控制台并保存新的
对象(例如电影
)。这可能会为您创建索引,但不确定Move.create(…)
- 解决方案2:在项目的根文件夹中运行
,这会将所有模型重新索引到ElasticSearch中rake-environment-tire:import:all
index = Tire::Index.new('oldskool')
index.delete
index.create
当我将elasticsearch添加到rails应用程序时,我使用了searchkick:
这是真正的用户友好和快速设置。您可以使用
rake searchkick:reindex:all
命令索引所有数据。谢谢您的回答。我已经试过了。在尝试执行这些操作时,显示了创建索引时出现的错误[ERROR]——elasticsearch返回:500:{“ERROR”:“IndexCreationException[[custom index name]未能创建索引];嵌套:NoClassDefFoundError[无法初始化类org.elasticsearch.index.codec.PostingFormat.PostingFormats];”,“status”:500}rake环境轮胎的同一种错误:导入:ALL是否正常?错误消息基本上说,电影索引尚未创建。您可以尝试从rails控制台(rails c
)中通过编程调用创建它:index=Tire::index.new('movies')index.delete index.create
共有3条语句。我更新了答案,注释弄乱了格式。当我试图执行第二条和第三条语句时,给出了false和false…:(irb(main):001:0>index=Tire::index.new('movie')=>#irb(main):002:0>index.delete=>false irb(main):003:0>index.create=>false我不知道它背后真正在做什么,但它对我很有用,非常感谢。在电影模型中,使用movie.import,在SearchController中使用Moview.search参数[:query]
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 %>
404 : {"error":"IndexMissingException[[movies] missing]","status":404}
index = Tire::Index.new('oldskool')
index.delete
index.create