Beu*_*uun 6 ruby-on-rails elasticsearch
我有一个Post模型:
class Post < ActiveRecord::Base
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
# I WANT THIS FUNCTION EXECUTED ON index1
def self.search1(query)
__elasticsearch__.search(
{
query:
}
)
end
# I WANT THIS FUNCTION EXECUTED ON index2
def self.search2(query)
__elasticsearch__.search(
{
query:
}
)
end
index_name "index1"
# I NEED ANOTHER INDEX ? HOW CAN I DO ?
settings index1: { number_of_shards: 1 } do
mappings dynamic: 'false' do
indexes :title, analyzer: 'english'
end
end
end
Post.__elasticsearch__.client.indices.delete index: "index1" rescue nil
Post.__elasticsearch__.client.indices.create index: "index1", body: { settings: Post.settings.to_hash, mappings: Post.mappings.to_hash }
Post.import
Run Code Online (Sandbox Code Playgroud)
我有1个模型,2个非常不同的函数需要完全不同的索引.
如何在1个模型中构建2个不同的索引并告诉__elasticsearch__.search它应该使用哪个索引?
您知道可以对同一个数据库表使用 2 个模型吗?我会关注共享方法,每个索引使用一个模型,即 3 个模型,一个用于常规使用,另外 2 个模型专门用于索引。一开始可能感觉像是黑客攻击,但最终可能是一个更干净的解决方案。让我知道进展如何:p