diff options
| author | hukl <contact@smyck.org> | 2009-04-28 00:15:53 +0200 |
|---|---|---|
| committer | hukl <contact@smyck.org> | 2009-05-01 17:14:02 +0200 |
| commit | 4bd16f053847f2efe347ebda9136ef2233ee0d2c (patch) | |
| tree | f4c11f89455de991c8d87726d5757b752e7129e2 /vendor/plugins/thinking-sphinx/lib/thinking_sphinx/facet_collection.rb | |
| parent | d3a9b46ba5c863a0ff377dcffae9a494fe476e02 (diff) | |
added thinking_sphinx plugin for fulltext search on nodes and heads
Diffstat (limited to 'vendor/plugins/thinking-sphinx/lib/thinking_sphinx/facet_collection.rb')
| -rw-r--r-- | vendor/plugins/thinking-sphinx/lib/thinking_sphinx/facet_collection.rb | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/vendor/plugins/thinking-sphinx/lib/thinking_sphinx/facet_collection.rb b/vendor/plugins/thinking-sphinx/lib/thinking_sphinx/facet_collection.rb new file mode 100644 index 0000000..1ad9d1a --- /dev/null +++ b/vendor/plugins/thinking-sphinx/lib/thinking_sphinx/facet_collection.rb | |||
| @@ -0,0 +1,60 @@ | |||
| 1 | module ThinkingSphinx | ||
| 2 | class FacetCollection < Hash | ||
| 3 | attr_accessor :arguments | ||
| 4 | |||
| 5 | def initialize(arguments) | ||
| 6 | @arguments = arguments.clone | ||
| 7 | @attribute_values = {} | ||
| 8 | @facets = [] | ||
| 9 | end | ||
| 10 | |||
| 11 | def add_from_results(facet, results) | ||
| 12 | facet = facet_from_object(results.first, facet) if facet.is_a?(String) | ||
| 13 | |||
| 14 | self[facet.name] ||= {} | ||
| 15 | @attribute_values[facet.name] ||= {} | ||
| 16 | @facets << facet | ||
| 17 | |||
| 18 | results.each_with_groupby_and_count { |result, group, count| | ||
| 19 | facet_value = facet.value(result, group) | ||
| 20 | |||
| 21 | self[facet.name][facet_value] ||= 0 | ||
| 22 | self[facet.name][facet_value] += count | ||
| 23 | @attribute_values[facet.name][facet_value] = group | ||
| 24 | } | ||
| 25 | end | ||
| 26 | |||
| 27 | def for(hash = {}) | ||
| 28 | arguments = @arguments.clone | ||
| 29 | options = arguments.extract_options! | ||
| 30 | options[:with] ||= {} | ||
| 31 | |||
| 32 | hash.each do |key, value| | ||
| 33 | attrib = facet_for_key(key).attribute_name | ||
| 34 | options[:with][attrib] = underlying_value key, value | ||
| 35 | end | ||
| 36 | |||
| 37 | arguments << options | ||
| 38 | ThinkingSphinx::Search.search *arguments | ||
| 39 | end | ||
| 40 | |||
| 41 | private | ||
| 42 | |||
| 43 | def underlying_value(key, value) | ||
| 44 | case value | ||
| 45 | when Array | ||
| 46 | value.collect { |item| underlying_value(key, item) } | ||
| 47 | else | ||
| 48 | @attribute_values[key][value] | ||
| 49 | end | ||
| 50 | end | ||
| 51 | |||
| 52 | def facet_for_key(key) | ||
| 53 | @facets.detect { |facet| facet.name == key } | ||
| 54 | end | ||
| 55 | |||
| 56 | def facet_from_object(object, name) | ||
| 57 | object.sphinx_facets.detect { |facet| facet.attribute_name == name } | ||
| 58 | end | ||
| 59 | end | ||
| 60 | end \ No newline at end of file | ||
