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

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

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 it appears you can handle that through passing a transform to the import. github.com/elastic/elasticsearch-rails/blob/…
Side note, if you are not commited to that library you are using maybe check this library out 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.