Search code examples
rubyelasticsearchchewy-gem

How to update Elasticsearch field type with Chewy


My question is about changing a field type in my elasticsearch index using the elasticsearch ruby client chewy.

I am trying to update a field type in my index. I am getting this error: illegal_argument_exception.

I have read that it is impossible to change type in an existing index, so I was thinking about reindexing everything: rake chewy:reset. I have tried many things... I can't get my reindexation working.

The detailed error:

Elasticsearch::Transport::Transport::Errors::BadRequest: [400] {"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"mapper [enabled] cannot be changed from type [text] to [boolean]"}],"type":"illegal_argument_exception","reason":"mapper [enabled] cannot be changed from type [text] to [boolean]"},"status":400}

My index as it was before (field enabled was a texte, by default):

class SearchIndex < Chewy::Index
  define_type Company, delete_if: :deleted_at do
    ...
    field :enabled
    ...
  end
end

My index as I want it to be:

class SearchIndex < Chewy::Index
  define_type Company, delete_if: :deleted_at do
    ...
    field :enabled, type: 'boolean'
    ...
  end
end

How can I do with Chewy (without requesting to ElasticSearch via curl, if possible) to get my index reindexed with the new field type ?

Thanks.


Solution

  • I found out why Elsaticsearch didn't wanted me to change my type. It was because my index got some other type containing the same field (at least the same field name), but with another field type.

    So, all the field with the same name must have the same type. Then you will be able to (and you will have to) delete your index and recreate it. (SearchIndex::purge! does the job)