Rails - ElasticSearch - Multiple Indexes in One Model

I have a Post model:

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 

I have 1 model, 2 very different functions that need a completely different index.

How can I build 2 different indexes in 1 model and tell __elasticsearch__.search which index should it use?

+6
source share
1 answer

Do you know that you can use 2 models for one database table? I would mention common methods and one model for an index, or three models for regular use, and two others specifically for indexes. It might seem like a hacker at first, but in the end it might be a cleaner solution. Let me know how this happens: p

0
source

Source: https://habr.com/ru/post/983163/


All Articles