0.0
Low commit activity in last 3 years
No release in over a year
This gem has useful methods to sync rdb data with es data.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

>= 5.1.0
 Project Readme

ElasticArSync

This repository contains easy set up modules for RDB and elasticsearch index with Active Record. This is based on gem elasticsearch-rails.

日本語での説明はこちら https://ted-tech.hateblo.jp/entry/2020/08/11/103155

Usage

Preparation

Install gem sidekiq and gem redis-rails to use Asynchronous processing for your app. Install gem elasticsearch-rails, gem elasticsearch-model and gem elasticsearch for your app.

Include module ElasticArSync::Elastic::Syncable on your Model class which is inherited ActiveRecord.

and define index_config after include ElasticArSync::Elastic::Syncable if you use this module normally.

you can override index_config described after.

example below

class XXXXX < ActiveRecord
  include ElasticArSync::Elastic::Syncable
  index_config
end

then your Model class is included modules which is for sync model's table with elasticsearch index.

when you create new record elasticsearch index save new document in sync. update and delete is same as create.

this module contains class method index_setup to setup index.

setup means create new index and put alias to new index and sync whole records of RDB with new index, then you can use new index without any command.

XXXXX.index_setup

usable methods

if your Model class included ElasticArSync::Elastic::Syncable, it can use class methods below.

  • index_setup (class method) → to setup index to elsticsearch

  • create_index (class method) → to create new index to elasticsearch

  • delete_index({index_name}) (class method) → to delete index. needed argument index name which you delete

  • import_all_record(index_name) (class method) → to sync whole record with elasticsearch

  • switch_alias(new_index_name) (class method) → to switch using index, you needed create more than two index

  • mapping_list_keys (class method) → to get mapping list array of your index

  • get_aliases (class method) → to get aliase list array which you created

Customize

if you change process of after commit, you can override instance methods below. default is provided by ElasticArSync::Elastic::Worker::IndexWorker

  • document_sync_create → for callback of create

  • document_sync_update → for callback of update

  • document_sync_delete → for callback of destroy

if you define original mapping of index like type, attribute, etc, you can use index_config after include ElasticArSync::Elastic::Syncable.

example below

you can override args dynamic, number_of_shards, attr_mappings. especially default mapping is whole attributes of your Model class, so you can customize mapping attributes by overriding attr_mappings.

index_config(dynamic: 'false', number_of_shards: 1, attr_mappings: { id: 'integer', name: 'text', birth: 'date' })

you can also override part of attributes by override_mappings like below

*As a premise, your model contain [id: integer, name: string, birth: date]

index_config(override_mappings: { birth: :keyword })

then birth will mapping as keyword type in index of elasticsearch.

if you want add mapping which is not defined as schema column of model, you can save method result as well like below.

index_config(override_mappings: { full_name: :keyword })

full_name is instance method

then full_name will mapped to index mapping as keyword type. but you have to define instance method full_name to your model class.

if you define your original mapping additionally, you can also define like below.

  settings index: { number_of_shards: number_of_shards } do
    mappings do
      indexes key, type: value
    end
  end

tips: just reference here.

https://github.com/elastic/elasticsearch-rails/tree/master/elasticsearch-model

Installation

Add this line to your application's Gemfile:

gem 'elastic_ar_sync'

And then execute:

$ bundle

Or install it yourself as:

$ gem install elastic_ar_sync

Contributing

Contribution directions go here.

License

The gem is available as open source under the terms of the MIT License.