Indexing on column field is not working with ElasticSearch

987 Views Asked by At

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}

1

There are 1 best solutions below

3
On

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