Search code examples
sphinxthinking-sphinx

Correct syntax to HAVING with thinking sphinx


I have a query and I want to run the same with thinking sphinx, but I'm having problems with HAVING clause. The relevant code is the following:

#technology_index.rb
  has taggings.tag_id, :as => :tag_id
  has id, :as => :technology_id

#technology.rb
    search(params[:query],
      with: { tag_id: params[:filter].values.flatten },
      group_by: :technology_id,
      having: "COUNT(`technology_id`)=#{params[:filter].size}"
    )

This throws me the following error (new lines in error message are mine):

sphinxql: syntax error, unexpected IDENT, expecting '*' near '`technology_id`)=1 LIMIT 0, 20; 
SHOW META' - SELECT *, groupby() AS sphinx_internal_group, id AS sphinx_document_id, 
count(DISTINCT sphinx_document_id) AS sphinx_internal_count 
FROM `technology_core` WHERE MATCH('fibrosis') AND `tag_id` IN (2) 
AND `sphinx_deleted` = 0 GROUP BY `technology_id` HAVING COUNT(`technology_id`)=1 
LIMIT 0, 20; SHOW META

The problem is with having: clause, because if I remove it, at least the query works. Is supposed since 2.2.1 beta HAVING is supported (using 2.2.6), the same for TS 3.1 (using 3.1.2). I couldn't find any info about use of HAVING in the docs, but if I try something like having: "COUNT(*)>1" works so I guess, I'm just invoking it incorrectly. What should be the correct way to use having: with TS ?


Solution

  • It seems that something like having: "COUNT('technology_id')=#{params[:filter].size}" is still not supported by sphinx.
    Anyway my query worked with having: "COUNT(*)=#{params[:filter].size} as I was grouping by technology_id.