1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Elastic SearchでString型のカラムが取り込めないエラー

Last updated at Posted at 2019-05-17

環境

  • Rails 5.2.1
  • Ruby 2.6.1
  • Elastic Search 6.3.2

ElasticSearchに企業マスタデータを取り込むため設定

module MstCompanySearchable
  extend ActiveSupport::Concern

  included do
    include Elasticsearch::Model

    # インデックスするフィールドの一覧
    INDEX_FIELDS = %w(name).freeze

    # インデックス名
    index_name "es_mst_company_#{Rails.env}"

    # マッピング情報
    settings do
      mapping dynamic: 'false' do # 動的にマッピングを生成しない
        indexes :name, analyzer: 'kuromoji', type: 'string'
      end
    end

    # インデックスするデータを生成
    # @return [Hash]
    def as_indexed_json(option = {})
      as_json.select {|k, _| INDEX_FIELDS.include?(k)}
    end
  end

  module ClassMethods
    def create_index!(options={})
      client = __elasticsearch__.client
      client.indices.delete index: index_name rescue nil if options[:force]
      client.indices.create(index: index_name,
                            body: {
                                settings: settings.to_hash,
                                mappings: mappings.to_hash
                            })
    end
  end
end

create_index!を実行するとエラーが発生

{"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"No handler for type [string] declared on field [name]"}]

ElasticSearch6系からはkeywordもしくはTextを使うらしい
参照: https://www.elastic.co/jp/blog/strings-are-dead-long-live-strings

indexes :name, analyzer: 'kuromoji', type: 'string'indexes :name, analyzer: 'kuromoji', type: 'text'に変更してあげると解決

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?