简体   繁体   English

如何在Rails中使用Elasticsearch

[英]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: 自从我从事轮胎工作已经有一段时间了,但是从我的头顶开始:

  • Solution 1: Fire up a rails console and save a new Movie object (eg Move.create(...) ). 解决方案1:启动Rails控制台并保存一个新的Movie对象(例如Move.create(...) )。 This might create the index for you, not sure though. 可能会为您创建索引,但不确定。
  • Solution 2: Run 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.

相关问题 Rails,Tire,Elasticsearch:如何使用同义词? - Rails, Tire, Elasticsearch: how to use synonyms? 如何在 Ruby on Rails 的 Rest Api 项目中使用 Chewy 作为 Elasticsearch? - How use Chewy as Elasticsearch in Rest Api project of Ruby on Rails? 如何在导轨上通过Elasticsearch植入刻面 - How implant Facets with elasticsearch on rails 如何在Heroku上使用Elasticsearch - How to use Elasticsearch on Heroku 在另一个Rails应用程序中使用一个Rails应用程序的Elasticsearch服务器 - Use one elasticsearch server of rails application in another rails application 如何使用ElasticSearch-Rails查询dsl返回相关关系 - How can I use ElasticSearch-Rails query dsl to return related relationships 如何专门使用“Elasticsearch”gem将elasticsearch与rails应用程序集成 - How to integrate elasticsearch with rails application specifically using "Elasticsearch" gem 如何在Rails上获得Elasticsearch性能优化 - how to obtain elasticsearch performance optimization in ruby on rails 如何在rails elasticsearch上模糊匹配ruby。 - How to fuzzy match a ruby on rails elasticsearch. 如何从 Rails 中的 elasticsearch 中检索所有记录 - How to retreive all the records from elasticsearch in rails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM