首页 > 解决方案 > Rails-elasticsearch: unable to perform custom mapping content of the table

问题描述

I am trying to make a custom index of my model to elasticsearch serwer. Somehow I can render only a structure (selected columns) but without a records.

My model:

require 'elasticsearch/model'

class Lead < ApplicationRecord
  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks

   mappings dynamic: false do
      indexes :id, type: 'keyword'
      indexes :lead_status, type: 'keyword'
      indexes :country
      indexes :city
      indexes :title
      indexes :description
      indexes :contact_person
    end
end

And then I try to create an index from rails console:

2.5.1 :004 > Lead.__elasticsearch__.create_index!(force:true)
2018-11-26 04:19:55 +0100: DELETE http://localhost:9200/leads [status:404, request:0.034s, query:N/A]
2018-11-26 04:19:55 +0100: < {"error":{"root_cause":[{"type":"index_not_found_exception","reason":"no such index","resource.type":"index_or_alias","resource.id":"leads","index_uuid":"_na_","index":"leads"}],"type":"index_not_found_exception","reason":"no such index","resource.type":"index_or_alias","resource.id":"leads","index_uuid":"_na_","index":"leads"},"status":404}
2018-11-26 04:19:55 +0100: [404] {"error":{"root_cause":[{"type":"index_not_found_exception","reason":"no such index","resource.type":"index_or_alias","resource.id":"leads","index_uuid":"_na_","index":"leads"}],"type":"index_not_found_exception","reason":"no such index","resource.type":"index_or_alias","resource.id":"leads","index_uuid":"_na_","index":"leads"},"status":404}
[!!!] Index does not exist (Elasticsearch::Transport::Transport::Errors::NotFound)
2018-11-26 04:19:55 +0100: HEAD http://localhost:9200/leads [status:404, request:0.007s, query:N/A]
2018-11-26 04:19:55 +0100: < 
2018-11-26 04:19:55 +0100: [404] 
2018-11-26 04:19:56 +0100: PUT http://localhost:9200/leads [status:200, request:0.707s, query:n/a]
2018-11-26 04:19:56 +0100: > {"settings":{},"mappings":{"_doc":{"dynamic":false,"properties":{"id":{"type":"keyword"},"lead_status":{"type":"keyword"},"country":{"type":"text"},"city":{"type":"text"},"title":{"type":"text"},"description":{"type":"text"},"contact_person":{"type":"text"}}}}}
2018-11-26 04:19:56 +0100: < {"acknowledged":true,"shards_acknowledged":true,"index":"leads"}
 => {"acknowledged"=>true, "shards_acknowledged"=>true, "index"=>"leads"}

After that on my local elasticsearch server on /leads I can see my custom structure but without any records. How can I fix this issue?

Rails version: 5.2.1 Elasticsearch: 6.4.3 elasticsearch-model/rails gems: 6.0.0

标签: ruby-on-railsrubyelasticsearchelasticsearch-rails

解决方案


我对这种类型的东西使用 es-elasticity。但是从 elasticsearch 的输出来看,它只是创建索引,您必须索引对象才能让它们显示在 elasticsearch 中。

从查看文档看来,您可以通过以下方式导入Lead.import


推荐阅读