Search code examples
ruby-on-railsrubyelasticsearchopensearchamazon-opensearch

Integrating Opensearch with Ruby on Rails


Currently, my Rails application utilizes Elasticsearch with the elasticsearch-model and elasticsearch-rails gems for integration:

require 'elasticsearch/model'
class Article < ActiveRecord::Base
  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks

  settings :index => {
    :number_of_shards => 1,
    :max_result_window => MAX_RESULT_WINDOW
  },
  :analysis => {
    :filter => {
      :email => {
        :type => 'pattern_capture',
        :preserve_original => 1,
        :patterns => [
          '([^@]+)',
          '(\\p{L}+)',
          '(\\d+)',
          '@(.+)',
          '([^-@]+)'
        ]
      }
    },
    :analyzer => {
      :email => {
        :tokenizer => 'uax_url_email',
        :filter => [
          'email',
          'lowercase',
          'unique'
        ]
      }
    }
  } do
    mappings do
      indexes :sent_at, type: 'date', fields: { raw: { type: 'date', index: 'not_analyzed' } }
    end
  end

  def as_indexed_json(options={})
    self.as_json(
      only: [:id, :send_at],
      methods: [:student_email, :subject, :deleted_at]
    )
  end
end

Article.__elasticsearch__.create_index!
Article.import

Now, I intend to transition to OpenSearch, but I haven't found any gem that provides similar functionality, such as

Opensearch.client.import

Does anyone have suggestions on how to achieve this migration? Any help would be greatly appreciated.


Solution

  • Searchkick supports both Elasticsearch and Opensearch: https://github.com/ankane/searchkick