简体   繁体   English

如何专门使用“Elasticsearch”gem将elasticsearch与rails应用程序集成

[英]How to integrate elasticsearch with rails application specifically using "Elasticsearch" gem

I am new to rails and to elastic search.我是 Rails 和弹性搜索的新手。 I have seen other resources to configure using Tire, Searchkick and some others, but I want to use Elasticsearch gem.我已经看到其他资源可以使用 Tire、Searchkick 和其他一些资源进行配置,但我想使用Elasticsearch gem。 I have running rails application and running Elasticsearch server on my system but I do not how to configure them to communicate with each other.我在我的系统上运行 Rails 应用程序并运行 Elasticsearch 服务器,但我不知道如何配置它们以相互通信。

Currently, I am facing a lot of troubles to do the same.目前,我在做同样的事情时面临很多麻烦。 Any help would be highly appreciated.任何帮助将不胜感激。

For a very basic quick start of the elastic's github gem for model indexing you could do the following in development environment with elasticsearch running on localhost:9200对于用于模型索引的弹性 github gem 的非常基本的快速入门,您可以在开发环境中使用在 localhost:9200 上运行的 elasticsearch 执行以下操作

in Gemfile:在 Gemfile 中:

gem 'elasticsearch-model'

then run on terminal:然后在终端上运行:

$ bundle install

in app/models/service.rb include right after class declaration:在 app/models/service.rb 中,在类声明之后包含:

include Elasticsearch::Model

you can now play with it on console with existing data (results are just an example):您现在可以使用现有数据在控制台上使用它(结果只是一个示例):

$ rails console

# Create the index for Service model on elasticsearch
> Service.__elasticsearch__.create_index!
=> {"acknowledged"=>true}

# Import current Service records into the index
> Service.import
  Service Load (207.3ms)  SELECT  "services".* FROM "services"  ORDER BY "services"."id" ASC LIMIT 1000

# Sample search returning total results
> Service.__elasticsearch__.search("mykeyword").results.total
=> 123

For more information and details you can take a look at the project's github page有关更多信息和详细信息,您可以查看项目的github 页面

Better use elasticsearch-rails更好地使用elasticsearch-rails

in Gemfile:在 Gemfile 中:

gem install elasticsearch-rails

To import the records from your Article model, run:要从您的文章模型导入记录,请运行:

$ bundle exec rake environment elasticsearch:import:model CLASS='Article'

To limit the imported records to a certain ActiveRecord scope, pass it to the task:要将导入的记录限制在某个 ActiveRecord 范围内,请将其传递给任务:

$ bundle exec rake environment elasticsearch:import:model CLASS='Article' SCOPE='published'

Run this command to display usage instructions:运行此命令以显示使用说明:

$ bundle exec rake -D elasticsearch

If you want to use for model如果你想用于模型

elasticsearch-model , which contains search integration for Ruby/Rails models such as ActiveRecord::Base and Mongoid, elasticsearch-model ,它包含对 Ruby/Rails 模型(例如 ActiveRecord::Base 和 Mongoid)的搜索集成,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM