Commit 33471d9f authored by Mark Chao's avatar Mark Chao

ES: proxy object setup for elasticsearch-rails

We no longer include Elasticsearch::Model, which would set up
those included modules. Therefore we have to do this manually.

Allow __transform to handle versions
parent be5af716
......@@ -5,9 +5,40 @@
require 'gitlab/current_settings'
Gitlab.ee do
require 'elasticsearch/model'
### Modified from elasticsearch-model/lib/elasticsearch/model.rb
[
Elasticsearch::Model::Client::ClassMethods,
Elasticsearch::Model::Naming::ClassMethods,
Elasticsearch::Model::Indexing::ClassMethods,
Elasticsearch::Model::Searching::ClassMethods
].each do |mod|
Elasticsearch::Model::Proxy::ClassMethodsProxy.include mod
end
[
Elasticsearch::Model::Client::InstanceMethods,
Elasticsearch::Model::Naming::InstanceMethods,
Elasticsearch::Model::Indexing::InstanceMethods,
Elasticsearch::Model::Serializing::InstanceMethods
].each do |mod|
Elasticsearch::Model::Proxy::InstanceMethodsProxy.include mod
end
Elasticsearch::Model::Proxy::InstanceMethodsProxy.class_eval <<-CODE, __FILE__, __LINE__ + 1
def as_indexed_json(options={})
target.respond_to?(:as_indexed_json) ? target.__send__(:as_indexed_json, options) : super
end
CODE
### Monkey patches
Elasticsearch::Model::Response::Records.prepend GemExtensions::Elasticsearch::Model::Response::Records
Elasticsearch::Model::Adapter::Multiple::Records.prepend GemExtensions::Elasticsearch::Model::Adapter::Multiple::Records
Elasticsearch::Model::Indexing::InstanceMethods.prepend GemExtensions::Elasticsearch::Model::Indexing::InstanceMethods
Elasticsearch::Model::Adapter::ActiveRecord::Importing.prepend GemExtensions::Elasticsearch::Model::Adapter::ActiveRecord::Importing
module Elasticsearch
module Model
......
# frozen_string_literal: true
module GemExtensions
module Elasticsearch
module Model
module Adapter
module ActiveRecord
module Importing
def __transform
lambda { |model| { index: { _id: model.id, data: model.__elasticsearch__.version(version_namespace).as_indexed_json } } }
end
end
end
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment