Solution when class_cast_exception appears in create_index! Of elasticsearch-rails (7 series)

Event

I am learning ElasticSearch with Rails by referring to Create a search function with Rails and Elasticsearch and try various things --- Part 1: Creating a sample application.

When I prepared the index and model and tried to create an index, the following error occurred.

irb(main):001:0> Manga.create_index!
Traceback (most recent call last):
        2: from (irb):1
        1: from app/models/concerns/manga_searchable.rb:50:in `create_index!'
Elasticsearch::Transport::Transport::Errors::InternalServerError ([500] {"error":{"root_cause":[{"type":"class_cast_exception","reason":"class java.lang.String cannot be cast to class java.util.Map (java.lang.String and java.util.Map are in module java.base of loader 'bootstrap')"}],"type":"class_cast_exception","reason":"class java.lang.String cannot be cast to class java.util.Map (java.lang.String and java.util.Map are in module java.base of loader 'bootstrap')"},"status":500})

Solutions

The solution is to specify the document type.

app/models/xxx.rb


module XXXSearchable
  extend ActiveSupport::Concern

  included do
    include Elasticsearch::Model
    #  ...abridgement...

    #Document type must be specified
    document_type "es_xxx_#{Rails.env}"
  end
end

A similar Issue was up, so I was able to solve it by looking at this.

It seems that the cause is that the old version (up to 6 series) gems guessed type and document_type at the time of mapping, but 7 series no longer guessed type and document_type.

What is a document type in the first place?

In Elasticsearch, one sentence unit to be stored is called a document. To borrow the expression First Elasticsearch --Qiita, the document corresponds to the record of RDBMS.

And the document type is "** document name **". The word "type" is included, so it feels like a pattern, but the substance is a "name". It's a point that tends to be a little confusing.

Recommended Posts

Solution when class_cast_exception appears in create_index! Of elasticsearch-rails (7 series)
<java> When "EXCEPTION_ACCESS_VIOLATION" appears in awt
[Rails] Solution when migration error occurs in acts-as-taggable-on