Building a dynamic SearchKick query throws syntax error

122 views Asked by At

If I run this query:

self.send("search", where: {geo_location: { near:[ params[:latitude], params[:longitude] ]}}, boost_by: {luxury_shopper: {factor: 80}} && {movie_goer: {factor: 20}})

everything works great and I get my result. But if I run this query

booster = "boost_by: {luxury_shopper: {factor: 80}} && {movie_goer: {factor: 20}}"

self.send("search", where: {geo_location: { near:[ params[:latitude], params[:longitude] ]}}, booster)

I get SyntaxError: unexpected ')', expecting =>, but I can't figure out why, or more importantly, how to write this query. The booster part is dynamic. I get an arbitrary number of attributes to "boost by" and insert them into the query.

1

There are 1 answers

0
Eugene Petrov On BEST ANSWER

You pass a string where searchkick expects a hash, and I'm not sure does it merge options automatically, the following should do the job:

booster = { boost_by: { luxury_shopper: {factor: 80}, movie_goer: {factor: 20} } }
self.send("search", {where: {geo_location: { near:[ params[:latitude], params[:longitude] ]}}}.merge(booster))