0

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

andrzej541
  • 919
  • 1
  • 9
  • 21

1 Answers1

1

I use es-elasticity for this type of stuff. But from the output from elasticsearch it is only creating the index, you must index the objects in order for them to show up in elasticsearch.

From gandering at the docs it looks like you could import by doing Lead.import

Austio
  • 5,939
  • 20
  • 34
  • Thank you for your response Austio. I did as you said. I've established an index (create_index! method) and them I imported my Leads. The only problem is that still somehow elasticsearch ignores my restrictions and imports a lot of fields which I did not intented to fetch. E.g. emails table, ignored on my custom mapping method in Lead model. How can I change that and fetch only selected fields? – andrzej541 Nov 26 '18 at 05:19
  • @andrzej541 it appears you can handle that through passing a transform to the import. https://github.com/elastic/elasticsearch-rails/blob/52f649bd40d112fb4576acdbd58940099c03d008/elasticsearch-model/lib/elasticsearch/model/importing.rb#L81 – Austio Nov 26 '18 at 17:38
  • Side note, if you are not commited to that library you are using maybe check this library out https://github.com/doximity/es-elasticity It has document models that are independent from your database models which is much better because you are not limited to a single index per model. – Austio Nov 26 '18 at 17:40