Skip to content

Instantly share code, notes, and snippets.

@clemens
Created December 17, 2010 12:51
Show Gist options
  • Save clemens/744886 to your computer and use it in GitHub Desktop.
Save clemens/744886 to your computer and use it in GitHub Desktop.
the query selects all ids from the full branch from the nested set
class Product
define_index do
# ...
has %(
(
SELECT GROUP_CONCAT(IFNULL(parent.`id`, '0') SEPARATOR ',')
FROM `categories` AS node,
`categories` AS parent
WHERE node.`merchant_id` = `products`.`merchant_id` AND
parent.`merchant_id` = `products`.`merchant_id` AND
node.`lft` BETWEEN parent.`lft` AND parent.`rgt` AND node.`id` = `category_id`
ORDER BY parent.`lft`
)
), :source => :query, :type => :multi, :as => :category_ids, :facet => true
end
end
@clemens
Copy link
Author

clemens commented Dec 17, 2010

The query selects all ids from the full branch from the nested set of categories per merchant (see http://dev.mysql.com/tech-resources/articles/hierarchical-data.html "Retrieving a Single Path"). I want to have a facet for that but here's what Thinking Sphinx is giving me (cut the gem directory for brevity):

Product.facets('adidas')
NoMethodError: undefined method ( SELECT GROUP_CONCAT(IFNULL(parent.id, '0') SEPARATOR ',') FROMcategoriesAS node, categoriesAS parent WHERE node.merchant_id=products.merchant_idAND parent.merchant_id=products.merchant_idAND node.lftBETWEEN parent.lftAND parent.rgtAND node.id=category_id ORDER BY parent.lft ) ' for #<Product:0x104cc1410> from GEM_DIR/activerecord-2.3.8/lib/active_record/attribute_methods.rb:264:inmethod_missing'
from GEM_DIR/thinking-sphinx-1.4.0/lib/thinking_sphinx/facet.rb:107:in send' from GEM_DIR/thinking-sphinx-1.4.0/lib/thinking_sphinx/facet.rb:107:intranslate'
from GEM_DIR/thinking-sphinx-1.4.0/lib/thinking_sphinx/facet.rb:80:in value' from GEM_DIR/thinking-sphinx-1.4.0/lib/thinking_sphinx/facet_search.rb:125:inadd_from_results'
from GEM_DIR/thinking-sphinx-1.4.0/lib/thinking_sphinx/search.rb:264:in each_with_match' from (irb):33:ineach_with_index'
from GEM_DIR/thinking-sphinx-1.4.0/lib/thinking_sphinx/search.rb:263:in each' from GEM_DIR/thinking-sphinx-1.4.0/lib/thinking_sphinx/search.rb:263:ineach_with_index'
from GEM_DIR/thinking-sphinx-1.4.0/lib/thinking_sphinx/search.rb:263:in each_with_match' from GEM_DIR/thinking-sphinx-1.4.0/lib/thinking_sphinx/facet_search.rb:124:inadd_from_results'
from GEM_DIR/thinking-sphinx-1.4.0/lib/thinking_sphinx/facet_search.rb:50:in populate' from (irb):33:ineach_with_index'
from GEM_DIR/thinking-sphinx-1.4.0/lib/thinking_sphinx/facet_search.rb:47:in each' from GEM_DIR/thinking-sphinx-1.4.0/lib/thinking_sphinx/facet_search.rb:47:ineach_with_index'
from GEM_DIR/thinking-sphinx-1.4.0/lib/thinking_sphinx/facet_search.rb:47:in populate' from GEM_DIR/thinking-sphinx-1.4.0/lib/thinking_sphinx/facet_search.rb:13:ininitialize'
from GEM_DIR/thinking-sphinx-1.4.0/lib/thinking_sphinx/search_methods.rb:422:in new' from GEM_DIR/thinking-sphinx-1.4.0/lib/thinking_sphinx/search_methods.rb:422:infacets'
from (irb):33
from :0>>

@clemens
Copy link
Author

clemens commented Dec 17, 2010

It's Rails 2.3, Thinking Sphinx 1.4.0, Riddle 1.2.1 and Sphinx 0.9.9 r2117.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment