gpt4 book ai didi

ruby-on-rails - Rails 5:如何在Elastic Search中导入数据并执行条件搜索?

转载 作者:行者123 更新时间:2023-12-02 22:48:58 25 4
gpt4 key购买 nike

我有一个名为产品的模型。我提供了全文搜索来搜索产品并使用AngularJS在客户端进行预输入。搜索查询是一个普通的类似查询,例如:

select * from products where name like = %abc% and company_id = 123;

但是,随着表中记录的增加,使用上述方法的搜索时间增加了,因此我决定实现 flex 搜索。我正在使用两个gem 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

最佳答案

您收到的错误是因为您没有创建products索引,而here可以看到产生错误的gem的代码。

仔细查看此line!self.index_exists? index: target_index,它检查索引是否存在,如果不存在,则会引发错误。

因此,您在此处有两个选择,可以像这样强制导入:Product.import(force: true),请注意,这会破坏索引(如果存在)并再次创建它。

另一个选择是先创建索引然后执行导入:

Product.__elasticsearch__.create_index!
Product.import

我建议您阅读如何在Elasticsearch中将模型映射到索引。 Link1Link2。因为要查询,您需要了解如何创建索引。

关于ruby-on-rails - Rails 5:如何在Elastic Search中导入数据并执行条件搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45525523/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com