0

I'm implementing ElasticSearch integration in my model :

require 'elasticsearch/model'
class MissionDef < ActiveRecord::Base
  # field: name (String(40))
  # field: icon (String(2000))
  # field: definition (String)
  # field: public, boolean

  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks

  def as_indexed_json(options={})
    self.as_json(
     only: [:id, :name]
     )
  end
end

and In my Rails Console I did:

MissionDef.import

MissionDef.first

MissionDef id: 226, ade_channel_key: "d403d658313e6c35ce", name: "Test Mission", icon: "/app/assets/images/badges/Showedup.png", definition: "{}", deleted_at: nil, created_at: "2015-08-04 11:30:08", updated_at: "2015-08-04 11:30:08", container_id: 883, public: true

My Query

1) when I'm doing search with other field value other than name and id which is not indexed it gives me the search result. for eg:

result = MissionDef.search 'app' --- it works result.records.count => 1

which should not be the case I guess.

2) Missiondef.first.as_indexed_json => does not work properly it gives me o/p as whole object as JSON

{"id"=>226, "ade_channel_key"=>"d403d658313e6c35ce", "name"=>"Test Mission", "icon"=>"/app/assets/images/badges/Showedup.png", "definition"=>"{}", "created_at"=>Tue, 04 Aug 2015 11:30:08 UTC +00:00, "updated_at"=>Tue, 04 Aug 2015 11:30:08 UTC +00:00, "container_id"=>883, "public"=>true}

wurde
  • 2,487
  • 2
  • 20
  • 39
Mangala
  • 43
  • 1
  • 8

1 Answers1

0

I've not used the elasticsearch-rails gem before, but by default I know that elasticsearch will search entire documents, so if you only want to specify specific fields in your query. So in your given example, the app is matching the icon field: /app/assets/images/badges/Showedup.png

Try specifying the fields you want to search - read more in their documentation here: https://github.com/elastic/elasticsearch-rails/blob/master/elasticsearch-model/README.md#the-elasticsearch-dsl

So in this case it may be something like:

result = MissionDef.search query: {match:  {name: "app" }}
result.records.count #=> should be 0 this time
seddy
  • 801
  • 8
  • 16
  • Thank you, but my 2) query too not working Missiondef.first.as_indexed_json instead of giving id and name in json format it gives whole object in json format – Mangala Aug 10 '15 at 09:19
  • Sorry Mangala, I'm not sure what the answer to (2) might be, it does look odd though. I've not used `elasticsearch-rails` before so I'm not sure how the `as_indexed_json` method is supposed to be used. However, [from the example in the documentation](https://github.com/elastic/elasticsearch-rails/tree/master/elasticsearch-model#model-serialization), the one difference I can see is that it doesn't include the `Callbacks` module, which may be part of it perhaps? – seddy Aug 11 '15 at 12:47
  • Thanks I have got my query solved for both the questions. Thanks – Mangala Aug 12 '15 at 13:38