Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to update mappings for an existing index #1698

Open
andreynering opened this issue Nov 13, 2024 · 0 comments
Open

Add ability to update mappings for an existing index #1698

andreynering opened this issue Nov 13, 2024 · 0 comments

Comments

@andreynering
Copy link

Hi there,

For a given model in our app, we use custom mappings with dynamic: 'strict' and have a custom search_data method that return the data to be indexed in the right schema.

In some situations, we want to just add a couple of attributes to the schema, and it is not needed to create a new index to replace the old one like reindex do. Instead, we'd like to just update the schema for the existing index, and the searches for the attributes previously available will keep working until we re-index the whole table to have the new ones (and in our app, this means many many hours).

I managed to implement a simple method to do that, but I believe this could be available and documented on Searchkick itself.

class Model < ApplicationRecord
  def self.update_index_mappings!
    index_name = Model.searchkick_index.name
    mappings = Model.searchkick_index.options[:mappings]
    json = Oj.dump(mappings)

    Rails.logger.info("Updating index mappings for #{index_name}: #{json.inspect}")

    Excon.put(
      "#{ENV['OPENSEARCH_URL']}/#{index_name}/_mapping",
      headers: { 'Content-Type' => 'application/json' },
      body: Oj.dump(mappings)
    )
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant