I'm using the elasticsearch-rails gem and I'm trying to search a model that has a many to many relationship with another model.
Here are my models:
Customer has_and_belongs_to_many :locations
Location has_and_belongs_to_many :customers
I understand that ES doesn't support Many to Many associations, so I believe that I should be able to index an array of the associated location id's with each customer document. I'm not sure how to do this. As a result, I want to be able to search the customer database and set filters so that I am only searching customers associated with specific locations.
I don't currently have any mappings in place. This is mainly where I need help.
Here is a query I built with a previous project that handled filtering properly for a product database. I want to do the same thing here except filter by the associated locations.
filters = []
#filters.push term: {"status":"Disabled"}
if !params[:product_type].blank?
filters.push term: {"product_type":"#{params[:product_type]}"}
end
if !params[:category].blank?
filters.push term: {"category":"#{params[:category]}"}
end
if !params[:brand].blank?
filters.push term: {"brand":"#{params[:brand]}"}
end
if filters != nil
@products = Product.search(
query:{
function_score:{
query:{
bool:{
must:{
multi_match:{
fields: ['brand^10', '_all'],
query: "#{query}",
fuzziness: "AUTO"
}
},
filter:{
bool:{
must:filters
}
}
}
},
field_value_factor:{
field: "popularity",
modifier: "log1p",
factor: 0.5
},
boost_mode: "sum"
}
})
Thanks in advance.