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

How to add new field to existed index? #692

Closed
darkleaf opened this issue Oct 3, 2019 · 3 comments
Closed

How to add new field to existed index? #692

darkleaf opened this issue Oct 3, 2019 · 3 comments

Comments

@darkleaf
Copy link

darkleaf commented Oct 3, 2019

I have an existed index and I want to add a new field into it without reindex.
Elasticsearch has the put mapping api.

@jonatas
Copy link

jonatas commented Oct 3, 2019

I think we don't have such an option on chewy.

What you can do is try to update the index definition with our DSL and reindex the entire index. Or you can create a new one asynchronous and drop the old in favor of the new one when it's ready.

@5andi5
Copy link

5andi5 commented May 26, 2020

@darkleaf this hack might help:

ok = Chewy.client.indices.put_mapping(
  index: MyChewyIndex.index_name,
  type: MyChewyIndex.type_hash.keys.first,
  body: { properties: { my_new_field: { type: :text } } }
)['acknowledged']

https://github.com/elastic/elasticsearch-ruby/blob/master/elasticsearch-api/lib/elasticsearch/api/actions/indices/put_mapping.rb

And then to initialize new field value:

Chewy.client.update_by_query(...)

But field definition must be specified manually in elasticsearch syntax as close as possible to ones created by Chewy.
Elasticsearch does not allow to change field type. Therefore mistakes are not allowed.

@darkleaf
Copy link
Author

We use this rake task

task :put_mapping => :environment do
  Dir["#{Rails.root}/app/chewy/**/*.rb"].each { |file| require_dependency file }

  Chewy::Type.descendants.each do |type|
    Chewy.client.indices.put_mapping index: type.index_name,
                                     type: type.type_name,
                                     body: type.mappings_hash
  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

4 participants