How can I do search category.name by using elasticsearch in Rails? -
my search working fine. have type "1" or "2" results of "roommate" or "sublet". model has column called category_id
integer. model category
has column :name
string.
thus, have category_id 1 having "roommate" , 2 "sublet"
below housing model:
class housing < activerecord::base extend friendlyid friendly_id :title, use: :slugged include elasticsearch::model include elasticsearch::model::callbacks belongs_to :user belongs_to :category validates :title, :presence => true validates :category_id, :presence => true validates :created_at, :presence => false validates :user_email, :presence => true validates :description, :presence => false validates_length_of :title, :maximum => 30 def self.search(query) __elasticsearch__.search( { query: { # multi_match: { simple_query_string: { query: query, fields: ['title^10', 'category_id'] } } } ) end end
how can fix fields: ['title^10', 'category_id']
user can search "roommate" instead of must search integer "1" result of roommate ?
i tried fields: ['title^10', 'category.name']
not working.
fields: ['title^10', 'category.name']
won't work unless have correct mapping defined. elasticsearch doesn't know associations. es document store , searches records using it's own document store. unless add category name document stored in es, won't able search it.
tl;dr
define mapping. example:
mapping dynamic: 'strict' indexes :category indexes :name end indexes :title end
here category stored nested object inside index , hence searchable using category.name
Comments
Post a Comment