简体   繁体   中英

Thinking sphinx with rails - order by association column

Is there an ability to order search results by association column in thinking sphinx?

I have index defined as follows:

ThinkingSphinx::Index.define :demand, :with => :active_record, :delta => true do
  indexes client(:name), as: :client, sortable: true
  has(created_at, updated_at, client_id)
end

The search is performed in controller:

Demand.search(params[:query], order: "#{params[:sort]} #{params[:direction].try(:upcase)}")

The value of params[order] is "client.name" , params[:direction] is either "asc" or "desc"

Here is the query that is generated by sphinx:

SELECT * FROM `demand_core`, `demand_delta` WHERE MATCH('Test_client @sphinx_internal_class_name (Demand)') AND sphinx_deleted = 0 ORDER BY client.name DESC LIMIT 0, 20

When I perform search I get the following error:

ThinkingSphinx::SyntaxError - sphinxql: syntax error, unexpected CONST_FLOAT, expecting $end near '.name DESC LIMIT 0, 20; SHOW META':

I've been searching for a while for the solution but couldn't find one. So the question is how can i apply ordering in sphinx after i perform the search?

You need to use the field/attribute names when sorting - so in your situation, it should be client , not client.name . Hopefully it's not too tricky to change what params[:sort] value is passed through to the controller.

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