【发布时间】:2018-01-13 11:56:15
【问题描述】:
我有一个名为 Product 的模型。我提供了一个全文搜索来搜索产品并使用 AngularJS 在客户端生成一个预先输入。搜索查询是一个普通的类似查询:
select * from products where name like = %abc% and company_id = 123;
但是随着我在表中的记录越来越多,使用上述方法的搜索时间增加了,所以我决定实现弹性搜索。我正在使用两个宝石 gem 'elasticsearch-model'and gem 'elasticsearch-rails'。
如何索引产品型号。我尝试在 rails 控制台 中使用这个Product._elasticsearch_.create_index! 命令,它给出了以下错误:
2.4.0 :006 > Product._elasticsearch_.create_index!
ArgumentError: products does not exist to be imported into. Use create_index! or the :force option to create it.
以下是我的实现:
产品模型
require 'elasticsearch/model'
class Product < ApplicationRecord
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
belongs_to :company
belongs_to :category
belongs_to :gst
belongs_to :user
has_many :invoice_details
self.per_page = 100
Product.import
end
【问题讨论】:
-
Paras,
Product.import是product.rb文件中的行吗?你应该创建两个问题,因为你在这个问题中问了两件事。 -
是的。我当然会那样做。谢谢。
-
@MatayoshiMariano Product.import 行应该在哪里?
-
@ManoharsinhRana 它应该在一个脚本中,你应该运行它来从你的数据库迁移到 ElasticSearch
-
@ManoharsinhRana 是的,这是在您包含
Elasticsearch::Model模块时添加的内容
标签: ruby-on-rails ruby elasticsearch ruby-on-rails-5