Permalink
Browse files

Now searching with Sphinx for quick global search.

  • Loading branch information...
vojto committed Jul 1, 2010
1 parent 289fc67 commit 5dbe11a3e77590ff3ee9034b439dce726fecc335
Showing with 4,067 additions and 2 deletions.
  1. +11 −0 app/controllers/searches_controller.rb
  2. +1 −1 app/views/layouts/_quick_search.html.haml
  3. +1 −1 config/routes.rb
  4. +37 −0 lib/sphinx_search_engine.rb
  5. +5 −0 lib/tasks/index.rake
  6. +41 −0 vendor/plugins/sphinx/README.rdoc
  7. +21 −0 vendor/plugins/sphinx/Rakefile
  8. +1 −0 vendor/plugins/sphinx/init.rb
  9. +5 −0 vendor/plugins/sphinx/install.rb
  10. +6 −0 vendor/plugins/sphinx/lib/sphinx.rb
  11. +1,093 −0 vendor/plugins/sphinx/lib/sphinx/client.rb
  12. +50 −0 vendor/plugins/sphinx/lib/sphinx/request.rb
  13. +69 −0 vendor/plugins/sphinx/lib/sphinx/response.rb
  14. +112 −0 vendor/plugins/sphinx/spec/client_response_spec.rb
  15. +469 −0 vendor/plugins/sphinx/spec/client_spec.rb
  16. +8 −0 vendor/plugins/sphinx/spec/fixtures/default_search.php
  17. +8 −0 vendor/plugins/sphinx/spec/fixtures/default_search_index.php
  18. +11 −0 vendor/plugins/sphinx/spec/fixtures/excerpt_custom.php
  19. +8 −0 vendor/plugins/sphinx/spec/fixtures/excerpt_default.php
  20. +11 −0 vendor/plugins/sphinx/spec/fixtures/excerpt_flags.php
  21. +9 −0 vendor/plugins/sphinx/spec/fixtures/field_weights.php
  22. +9 −0 vendor/plugins/sphinx/spec/fixtures/filter.php
  23. +9 −0 vendor/plugins/sphinx/spec/fixtures/filter_exclude.php
  24. +9 −0 vendor/plugins/sphinx/spec/fixtures/filter_float_range.php
  25. +9 −0 vendor/plugins/sphinx/spec/fixtures/filter_float_range_exclude.php
  26. +9 −0 vendor/plugins/sphinx/spec/fixtures/filter_range.php
  27. +9 −0 vendor/plugins/sphinx/spec/fixtures/filter_range_exclude.php
  28. +10 −0 vendor/plugins/sphinx/spec/fixtures/filter_range_int64.php
  29. +10 −0 vendor/plugins/sphinx/spec/fixtures/filter_ranges.php
  30. +10 −0 vendor/plugins/sphinx/spec/fixtures/filters.php
  31. +13 −0 vendor/plugins/sphinx/spec/fixtures/filters_different.php
  32. +9 −0 vendor/plugins/sphinx/spec/fixtures/geo_anchor.php
  33. +9 −0 vendor/plugins/sphinx/spec/fixtures/group_by_attr.php
  34. +9 −0 vendor/plugins/sphinx/spec/fixtures/group_by_attrpair.php
  35. +9 −0 vendor/plugins/sphinx/spec/fixtures/group_by_day.php
  36. +9 −0 vendor/plugins/sphinx/spec/fixtures/group_by_day_sort.php
  37. +9 −0 vendor/plugins/sphinx/spec/fixtures/group_by_month.php
  38. +9 −0 vendor/plugins/sphinx/spec/fixtures/group_by_week.php
  39. +9 −0 vendor/plugins/sphinx/spec/fixtures/group_by_year.php
  40. +10 −0 vendor/plugins/sphinx/spec/fixtures/group_distinct.php
  41. +9 −0 vendor/plugins/sphinx/spec/fixtures/id_range.php
  42. +9 −0 vendor/plugins/sphinx/spec/fixtures/id_range64.php
  43. +9 −0 vendor/plugins/sphinx/spec/fixtures/index_weights.php
  44. +8 −0 vendor/plugins/sphinx/spec/fixtures/keywords.php
  45. +9 −0 vendor/plugins/sphinx/spec/fixtures/limits.php
  46. +9 −0 vendor/plugins/sphinx/spec/fixtures/limits_cutoff.php
  47. +9 −0 vendor/plugins/sphinx/spec/fixtures/limits_max.php
  48. +9 −0 vendor/plugins/sphinx/spec/fixtures/limits_max_cutoff.php
  49. +9 −0 vendor/plugins/sphinx/spec/fixtures/match_all.php
  50. +9 −0 vendor/plugins/sphinx/spec/fixtures/match_any.php
  51. +9 −0 vendor/plugins/sphinx/spec/fixtures/match_boolean.php
  52. +9 −0 vendor/plugins/sphinx/spec/fixtures/match_extended.php
  53. +9 −0 vendor/plugins/sphinx/spec/fixtures/match_extended2.php
  54. +9 −0 vendor/plugins/sphinx/spec/fixtures/match_fullscan.php
  55. +9 −0 vendor/plugins/sphinx/spec/fixtures/match_phrase.php
  56. +9 −0 vendor/plugins/sphinx/spec/fixtures/max_query_time.php
  57. +12 −0 vendor/plugins/sphinx/spec/fixtures/miltiple_queries.php
  58. +9 −0 vendor/plugins/sphinx/spec/fixtures/ranking_bm25.php
  59. +9 −0 vendor/plugins/sphinx/spec/fixtures/ranking_none.php
  60. +9 −0 vendor/plugins/sphinx/spec/fixtures/ranking_proximity.php
  61. +9 −0 vendor/plugins/sphinx/spec/fixtures/ranking_proximity_bm25.php
  62. +9 −0 vendor/plugins/sphinx/spec/fixtures/ranking_wordcount.php
  63. +9 −0 vendor/plugins/sphinx/spec/fixtures/retries.php
  64. +9 −0 vendor/plugins/sphinx/spec/fixtures/retries_delay.php
  65. +9 −0 vendor/plugins/sphinx/spec/fixtures/select.php
  66. +11 −0 vendor/plugins/sphinx/spec/fixtures/set_override.php
  67. +9 −0 vendor/plugins/sphinx/spec/fixtures/sort_attr_asc.php
  68. +9 −0 vendor/plugins/sphinx/spec/fixtures/sort_attr_desc.php
  69. +9 −0 vendor/plugins/sphinx/spec/fixtures/sort_expr.php
  70. +9 −0 vendor/plugins/sphinx/spec/fixtures/sort_extended.php
  71. +9 −0 vendor/plugins/sphinx/spec/fixtures/sort_relevance.php
  72. +9 −0 vendor/plugins/sphinx/spec/fixtures/sort_time_segments.php
  73. +1,269 −0 vendor/plugins/sphinx/spec/fixtures/sphinxapi.php
  74. +8 −0 vendor/plugins/sphinx/spec/fixtures/update_attributes.php
  75. +8 −0 vendor/plugins/sphinx/spec/fixtures/update_attributes_mva.php
  76. +9 −0 vendor/plugins/sphinx/spec/fixtures/weights.php
  77. +67 −0 vendor/plugins/sphinx/spec/sphinx/sphinx-id64.conf
  78. +67 −0 vendor/plugins/sphinx/spec/sphinx/sphinx.conf
  79. +86 −0 vendor/plugins/sphinx/spec/sphinx/sphinx_test.sql
  80. +3 −0 vendor/plugins/sphinx/sphinx.yml.tpl
  81. +75 −0 vendor/plugins/sphinx/tasks/sphinx.rake
  82. +27 −0 vendor/plugins/sphinx/test.rb
@@ -101,6 +101,17 @@ def broaden
redirect_to dataset_path(@dataset, :search_id => @search.id)
end
+ ########################################################################
+ # Quick search using Sphinx. If Sphinx search engine couldn't be used
+ # redirect to regular search.
+ def quick
+ query = params[:query_string]
+ engine = SphinxSearchEngine.new
+ search = engine.create_search_with_string(query)
+ engine.perform_search(search)
+ redirect_to search_path(search)
+ end
+
def show
@search = Search.find_by_id! params[:id]
@@ -1,4 +1,4 @@
- if params[:controller] != "main" && params[:controller] != "search"
#quick_search
- - form_tag searches_path, :class => "search" do
+ - form_tag quick_searches_path, :class => "search" do
= text_field_tag :query_string
View
@@ -14,7 +14,7 @@
map.register '/register', :controller => 'users', :action => 'create'
map.signup '/signup', :controller => 'users', :action => 'new'
- map.resources :searches, :member => {:broaden => :get}
+ map.resources :searches, :member => {:broaden => :get}, :collection => {:quick => :post}
map.resources :user_roles
@@ -0,0 +1,37 @@
+class SphinxSearchEngine
+ def create_search_with_string(string)
+ search = Search.new
+ search.query_string = string
+ search.search_type = "text"
+
+ query = SearchQuery.query_with_string(string, :scope=>'global', :object=>nil)
+ search.query = query
+ query.save
+ search.save
+ search
+ end
+
+ def perform_search(search)
+ # TODO. If can't use Sphinx, return false or something.
+
+ sphinx_client = Sphinx::Client.new
+
+ all_results = []
+
+ datasets = DatasetDescription.all
+ datasets.each do |dataset|
+ sphinx_client.SetLimits(0, 10)
+ results = sphinx_client.Query(search.query_string, "index_#{dataset.identifier}")
+ results['matches'].each do |r|
+ all_results << {:table_name => dataset.identifier, :record_id => r['id'], :search_query_id => search.query.id}
+ end
+ end
+
+ values = all_results.collect{|r|"('#{r[:table_name]}', #{r[:record_id]}, #{r[:search_query_id]})"}.join(",")
+
+ sql_query = "INSERT INTO search_results(table_name, record_id, search_query_id) VALUES #{values}"
+
+ DatasetDescription.connection.execute(sql_query)
+
+ end
+end
View
@@ -0,0 +1,5 @@
+namespace :index do
+ task :update_config => :environment do
+ require File.join(Rails.root, "index", "update_config.rb")
+ end
+end
@@ -0,0 +1,41 @@
+=Sphinx Client API 0.9.9-dev (r1299)
+
+This document gives an overview of what is Sphinx itself and how to use in
+within Ruby on Rails. For more information or documentation,
+please go to http://www.sphinxsearch.com
+
+==Sphinx
+
+Sphinx is a standalone full-text search engine, meant to provide fast,
+size-efficient and relevant fulltext search functions to other applications.
+Sphinx was specially designed to integrate well with SQL databases and
+scripting languages. Currently built-in data sources support fetching data
+either via direct connection to MySQL, or from an XML pipe.
+
+Simplest way to communicate with Sphinx is to use <tt>searchd</tt> -
+a daemon to search through fulltext indices from external software.
+
+==Documentation
+
+You can create the documentation by running:
+
+ rake rdoc
+
+==Latest version
+
+You can always get latest version from
+http://kpumuk.info/projects/ror-plugins/sphinx
+
+==Credits
+
+Dmytro Shteflyuk <kpumuk@kpumuk.info> http://kpumuk.info
+
+Andrew Aksyonoff http://sphinxsearch.com/
+
+Special thanks to Alexey Kovyrin <alexey@kovyrin.net> http://blog.kovyrin.net
+
+==License
+
+This library is distributed under the terms of the Ruby license.
+You can freely distribute/modify this library.
+
@@ -0,0 +1,21 @@
+require 'rake'
+require 'spec/rake/spectask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :spec
+
+desc 'Test the sphinx plugin.'
+Spec::Rake::SpecTask.new(:spec) do |t|
+ t.libs << 'lib'
+ t.pattern = 'spec/*_spec.rb'
+end
+
+desc 'Generate documentation for the sphinx plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'Sphinx Client API'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
@@ -0,0 +1 @@
+require File.dirname(__FILE__) + '/lib/sphinx'
@@ -0,0 +1,5 @@
+require 'fileutils'
+
+sphinx_config = File.dirname(__FILE__) + '/../../../config/sphinx.yml'
+FileUtils.cp File.dirname(__FILE__) + '/sphinx.yml.tpl', sphinx_config unless File.exist?(sphinx_config)
+puts IO.read(File.join(File.dirname(__FILE__), 'README'))
@@ -0,0 +1,6 @@
+require File.dirname(__FILE__) + '/sphinx/request'
+require File.dirname(__FILE__) + '/sphinx/response'
+require File.dirname(__FILE__) + '/sphinx/client'
+
+module Sphinx
+end
Oops, something went wrong.

0 comments on commit 5dbe11a

Please sign in to comment.