Elasticsearch for mysql view rails

239 Views Asked by At

How to create an elasticsearch index for a view in MYSQL ?

I tried making a model for a migration of a mysql view and in the model put

include Elasticsearch::Model
include Elasticsearch::Model::Callbacks

and put in elasticsearch.rb

unless client.indices.exists? index: 'history_views'
HistoryView.__elasticsearch__.create_index!
end

HistoryView.import

but it seems that the line HistoryView.import gives an error

ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'history_views.' in 'order clause': SELECT history_views.* FROM history_views ORDER BY history_views. ASC LIMIT 1000

I think the gem needs a primary key to order by it and that is not present in the view .

How can I solve this ? Do I have to override some function in the gem ?

1

There are 1 best solutions below

0
On

I found that this is the issue :

A primary key is needed which is not present in the MYSQL view

So added the line in the model.rb

  self.primary_key = 'id'

to create the primary key.

Note : id is in the select query for the view.