简体   繁体   中英

ElasticSearch not using all fields (array of strings) for search

I'm just getting started with ES in rails and I encounted something I don't understand:

I have an Artist model (simplified for the example):

class Artist < ActiveRecord::Base
  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks

  # simplified to get the idea
  def get_aliases
    ["alias1", "alias2"]
  end

  def as_indexed_json(options = {})
   self.as_json({
    methods: [ :get_aliases, :get_tracks ],
    only: [ :name, :get_aliases],
    include: { 
     get_tracks: { only: :name } },
   })
  end
end

My problem is that ES search is not using the get_aliases property to give results: only the 'name' attribute is searched:

I have a dev. database that has a few artists, none of them having the string "Phil" in it, but one has "Phillip" in its alias. When I try a Artist.__elasticsearch__.search('phil').count I get 0 result.

I have tried Artist.all.__elasticsearch__.import getting:

=> 0

and Artist.__elasticsearch__.refresh_index! getting:

=> {"_shards"=>{"total"=>10, "successful"=>5, "failed"=>0}}

(not sure what the difference is between the 2 commands...), but no luck :/

Is my problem comming from the fact that I use an array of strings as property ? Should I index it differently ?

Any idea/help is welcome !

EDIT:
I'm using
gem 'elasticsearch-rails', '~> 0.1.7'
gem 'elasticsearch-model', '~> 0.1.7'

and ElasticSearch server installed is 1.7.1

Thanks to Omarvelous comment, I got it working, the difference being in the as_indexed_json method:

def as_indexed_json(options = {})
  self.as_json({
    methods: [ :get_aliases, :get_tracks ],
    only: [ :name, :get_aliases],
    include: { 
     get_tracks: { only: :name } },
   })
end

Should be (note the get_aliases NOT a symbol in the 'only' array):

def as_indexed_json(options = {})
  self.as_json({
    methods: [ :get_aliases, :get_tracks ],
    only: [ :name, get_aliases],
    include: { 
     get_tracks: { only: :name } },
   })
end

Both implementations (the symbols or the actual method) will give you the same result in console:

irb(main):016:0> Artist.find(7).as_indexed_json
=> {"name"=>"Mick Jagger", "get_aliases"=>["Michael Philip Jagger"], "get_tracks"=>[]}

But in ES, this is quite a different story !

If anyone has a good link to learn how to request ES through Rails, I'd be grateful ;)

Thanks again to Omarvelous ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM