Changed Search to Elastic Search

This commit is contained in:
saurabhbhatia 2013-12-20 18:41:12 +08:00
parent fd24fbe082
commit 84edbdb6db
9 changed files with 83 additions and 72 deletions

12
Gemfile
View File

@ -26,10 +26,13 @@ gem "geocoder"
gem 'mini_magick'
gem 'mongoid', '> 2.1', '< 3.0.0'
gem 'mongoid_search', '~> 0.2.8'
# gem 'mongoid_search', :github => 'mauriciozaffari/mongoid_search', :branch => 'mongoid_2.x.x'
gem 'mongoid-tree', :require => 'mongoid/tree'
gem "mongo_session_store-rails3", '3.0.6'
gem 'nokogiri'
# gem 'riddle', '1.3.3'
# gem 'mongoid-sphinx', :github => 'Rulingcom/mongoid-sphinx', :require => 'mongoid_sphinx'
gem 'tire'
gem 'fb_graph'
gem 'rack-gridfs'
@ -43,11 +46,6 @@ gem 'resque-restriction'
# gem 'ruby-debug19'
gem 'rubyzip', '< 1.0.0'
gem 'sunspot_mongoid2'
# gem 'sunspot_mongo'
gem 'sunspot_solr', github: 'sunspot/sunspot', branch: 'master'
gem 'sunspot_rails', github: 'sunspot/sunspot', branch: 'master'
gem 'progress_bar'
gem 'sinatra'
@ -95,7 +93,7 @@ group :test, :development do
gem 'pry-debugger'
gem 'debugger'
gem 'faker'
gem "sunspot-rails-tester"
# gem "sunspot-rails-tester"
gem 'database_cleaner' #Strategies for cleaning databases. Can be used to ensure a clean state for testing.
gem "rspec", "~> 2.0"
gem "rspec-rails", "~> 2.0"

View File

@ -1,2 +0,0 @@
Sunspot.session = Sunspot::Rails.build_session
ActionController::Base.module_eval { include(Sunspot::Rails::RequestLifecycle) }

View File

@ -11,15 +11,15 @@ class Panel::Announcement::FrontEnd::BulletinsController < OrbitWidgetController
def search_result
if params[:search_query] == ""
@bulletins = get_bulletins_for_index
else
@search = Bulletin.solr_search do
fulltext params[:search_query]
with(:frontend_search,true)
with(:available_lang).any_of([I18n.locale.to_s])
end
# @search = Bulletin.solr_search do
# fulltext params[:search_query]
# with(:frontend_search,true)
# with(:available_lang).any_of([I18n.locale.to_s])
# end
# @bulletins = Bulletin.where(:category_id.in => params[:category_id]).and(:is_rejected=>false).and(:is_pending=>false)
search_result = @search.results.collect{|result| result.id}
@search = Bulletin.tire.search "#{params[:search_query]}"
search_result = @search.collect{|result| result.id}
@bulletins = Bulletin.all.available_for_lang(I18n.locale).can_display.any_in(_id:search_result).page( params[:page_main]).per(@page_num)
end

View File

@ -48,6 +48,18 @@ class Panel::Announcement::Widget::BulletinsController < OrbitWidgetController
# end
def search_result
if params[:search_query] == ""
redirect_to panel_announcement_front_end_search_result_path
else
@search = Bulletin.tire.search "#{params[:search_query]}"
search_result = @search.collect{|result| result.id}
@bulletins = Bulletin.all.available_for_lang(I18n.locale).can_display.any_in(_id:search_result).page( params[:page_main]).per(@page_num)
redirect_to panel_announcement_front_end_search_result_path(params[:search_result])
end
end
def bulletins_list_and_pic
if params[:widget_data_count]

View File

@ -3,9 +3,6 @@ class Bulletin
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::MultiParameterAttributes
# include Mongoid::FullTextSearch
include Mongoid::Search
include Sunspot::Mongoid2
include Impressionist::Impressionable
include OrbitCategory::Categorizable
@ -16,6 +13,9 @@ class Bulletin
include OrbitModel::TimeFrame
include OrbitTag::Taggable
include Tire::Model::Search
include Tire::Model::Callbacks
is_impressionable :counter_cache => { :column_name => :view_count }
field :title, localize: true
@ -39,33 +39,24 @@ class Bulletin
validates :title, :at_least_one => true
search_in :title, :subtitle, :text
def to_indexed_json
self.to_json
end
# search_in :title, :subtitle, :text
#before_save :clean_values, :save_bulletin_links, :save_bulletin_files
searchable do
text :titles do
title_translations.to_a.collect{|t| t[1]}
end
text :texts do
text_translations.to_a.collect{|t| Nokogiri::HTML(t[1]).text}
end
# def self.search(str, options={})
# options[:limit] = options[:limit] || 50
# # default limit: 50 (mongoDB default: 100)
string :available_lang , :multiple => true do
VALID_LOCALES.collect{|t| enabled_for_lang(t.to_sym) ? t : nil}.delete_if{|t| t.nil?}
end
# res = self.mongo_session.command({ text: self.collection.name,
# search: str}.merge(options))
# text :text do
# a = (title_translations["zh_tw"] + title_translations["en"])
# a
# # We shall now return a criteria of resulting objects!!
# self.where(:id.in => res['results'].collect {|o| o['obj']['_id']})
# end
boolean :frontend_search do
# ( !is_hidden && !is_pending && is_checked && !is_rejected )
(!is_hidden)
end
integer :view_count
string :category_id
end
def bulletin_category_with_title
self.category.title
@ -87,19 +78,19 @@ class Bulletin
published_at.strftime("%B %Y")
end
def self.search( search = nil, category_id = nil )
if category_id.to_s.size > 0 and search.to_s.size > 0
key = /#{search}/
find(:all, :conditions => {title: key, category_id: category_id}).desc( :is_top, :postdate )
elsif category_id.to_s.size > 0 and search.to_s.size < 1
find(:all, :conditions => {category_id: category_id}).desc( :is_top, :postdate )
elsif search.to_s.size > 0 and category_id.to_s.size < 1
key = /#{search}/
find(:all, :conditions => {title: key}).desc( :is_top, :postdate )
else
find(:all).desc( :is_top, :postdate)
end
end
# def self.search( search = nil, category_id = nil )
# if category_id.to_s.size > 0 and search.to_s.size > 0
# key = /#{search}/
# find(:all, :conditions => {title: key, category_id: category_id}).desc( :is_top, :postdate )
# elsif category_id.to_s.size > 0 and search.to_s.size < 1
# find(:all, :conditions => {category_id: category_id}).desc( :is_top, :postdate )
# elsif search.to_s.size > 0 and category_id.to_s.size < 1
# key = /#{search}/
# find(:all, :conditions => {title: key}).desc( :is_top, :postdate )
# else
# find(:all).desc( :is_top, :postdate)
# end
# end
def self.widget_datas( category_id = nil )

View File

@ -1,4 +1,5 @@
<%= form_tag panel_announcement_front_end_announcement_search_path, method: :get do %>
<p>Search</p>
<%= form_tag panel_announcement_widget_search_result_path, method: :get do %>
<%= hidden_field_tag :category_id, params[:category_id] %>
<%= hidden_field_tag :tag_id, params[:tag_id] %>
<p>

View File

@ -27,7 +27,7 @@ Rails.application.routes.draw do
end
namespace :front_end do
match "index_by_unit" => "bulletins#index_bulletins_by_unit",:as => :index_bulletins_by_unit
match "search_result" => "bulletins#search_result",:as=>:announcement_search
match "search_result" => "bulletins#search_result"
resources :bulletins # do
# match "preview" => "bulletins#preview_content",:as => :get_preview_content
# end
@ -39,6 +39,7 @@ Rails.application.routes.draw do
match "reload_web_links" => "bulletins#reload_web_links"
match "bulletins_side_bar" => "bulletins#bulletins_side_bar"
match "bulletins_search_block" => "bulletins#bulletins_search_block"
match "search_result" => "bulletins#search_result"
end
end
end

View File

@ -7,10 +7,12 @@ class Panel::Archive::FrontEnd::ArchiveFilesController < OrbitWidgetController
end
def get_search_result
@search = ArchiveFileCategory.solr_search do
fulltext params[:search_query]
with(:frontend_search,true)
end
# @search = ArchiveFileCategory.solr_search do
# fulltext params[:search_query]
# with(:frontend_search,true)
# end
@search = ArchiveFile.search(params[:search_query])
search_result = @search.results.collect{|result| result.id}
params[:category_id] = @search.results.collect{|result| result.id}

View File

@ -4,7 +4,6 @@ class ArchiveFile
include Mongoid::Timestamps
include Mongoid::MultiParameterAttributes
include Mongoid::Sortable
include Sunspot::Mongoid2
include OrbitCategory::Categorizable
include OrbitModel::LanguageRestrict
@ -12,6 +11,9 @@ class ArchiveFile
include OrbitModel::TimeFrame
include OrbitTag::Taggable
include Tire::Model::Search
include Tire::Model::Callbacks
# BelongsToCategory = :archive_file_category
# PAYMENT_TYPES = @site_valid_locales
@ -33,6 +35,12 @@ class ArchiveFile
after_save :save_archive_file_multiples
def to_indexed_json
self.to_json
end
# search_in :title
# searchable do
# text :titles do
# title_translations.to_a.collect{|t| t[1]}
@ -44,19 +52,19 @@ class ArchiveFile
# end
def self.search( category_id = nil )
# def self.search( category_id = nil )
if category_id.to_s.size > 0
# if category_id.to_s.size > 0
find(:all, :conditions => {archive_file_category_id: category_id}).desc( :is_top, :title )
# find(:all, :conditions => {archive_file_category_id: category_id}).desc( :is_top, :title )
else
# else
find(:all).desc( :is_top, :title)
# find(:all).desc( :is_top, :title)
end
# end
end
# end
def self.widget_datas