Commit b4cef7d4 by Rocky Duan

initial setup - not working yet

parent 266b9a4d
......@@ -28,10 +28,6 @@ gem 'voteable_mongo', :git => 'git@github.com:dementrock/voteable_mongo.git'
gem 'mongoid_taggable', :git => 'git@github.com:dementrock/mongoid_taggable.git'
gem 'mongoid_magic_counter_cache', :git => 'git@github.com:dementrock/mongoid-magic-counter-cache.git'
gem 'sunspot'
gem 'sunspot_solr'
gem 'sunspot_mongoid', :git => 'git@github.com:dementrock/sunspot_mongoid.git'
gem 'kaminari', :require => 'kaminari/sinatra', :git => 'git@github.com:dementrock/kaminari.git'
gem 'faker'
......@@ -39,6 +35,8 @@ gem 'faker'
gem 'rdiscount'
gem 'nokogiri'
gem 'tire'
group :test do
gem 'rspec'
gem 'rack-test', :require => "rack/test"
......
......@@ -51,15 +51,18 @@ get "#{api_prefix}/search/threads" do
else
page = (params["page"] || 1).to_i
per_page = (params["per_page"] || 20).to_i
search = CommentThread.solr_search do
fulltext(params["text"]) if params["text"]
with(:commentable_id, params["commentable_id"]) if params["commentable_id"]
with(:course_id, params["course_id"]) if params["course_id"]
with(:tags).all_of(params["tags"].split /,/) if params["tags"]
paginate :page => page, :per_page => per_page
order_by(sort_key, sort_order) if sort_key && sort_order
binding.pry
search = CommentThread.tire.search page: page, per_page: per_page, load: true do |search|
search.query do |query|
query.text(:_all, params["text"]) if params["text"]
query.term(:commentable_id, params["commentable_id"]) if params["commentable_id"]
query.term(:course_id, params["course_id"]) if params["course_id"]
query.terms(:tags, params["tags"].split(/,/), minimum_match: params["tags"].split(/,/).length) if params["tags"]
end
search.sort {|sort| sort.by sort_key, sort_order} if sort_key && sort_order
end
num_pages = [1, (search.total / per_page.to_f).ceil].max
num_pages = search.total_pages
{
collection: search.results.map{|t| t.to_hash(recursive: bool_recursive)},
num_pages: num_pages,
......
......@@ -17,27 +17,49 @@ class CommentThread < Content
field :at_position_list, type: Array, default: []
field :last_activity_at, type: Time
include Sunspot::Mongoid
searchable do
text :title, boost: 5.0, stored: true, more_like_this: true
text :body, stored: true, more_like_this: true
text :tags do
tags_array.join " "
end
include Tire::Model::Search
include Tire::Model::Callbacks
time :created_at
time :updated_at
time :last_activity_at
integer :comment_count
integer :votes_point do
votes_point
end
string :course_id
string :commentable_id
string :author_id
string :tags, multiple: true do
tags_array
end
#def to_indexed_json
# self.to_json
#end
settings :analyzer => {
:tags_analyzer => {
'type' => 'stop',
'stopwords' => [','],
#'tokenizer' => "keyword",
#'type' => "custom",
#'filter' => ['unique'],
}
}
=begin
, :properties => {
:tags_array => {
:type => "mutli_field",
:fields => {
:tags => { :type => :array, index: :analyzed},
:untouched_tags => { :type => :array, index: :not_analyzed},
}
}
}
=end
mapping do
indexes :title, type: :string, analyzer: 'i_do_not_exist'#:whitespace#:snowball#, boost: 5.0
indexes :body, type: :string, analyzer: :whitespace#:snowball
indexes :tags_in_text, type: :array, as: 'tags_array', index: :analyzed#proc {puts tags_array; tags_array}#, analyzer: :whitespace
indexes :tags, type: :string, as: 'tags_array', index: :not_analyzed#:analyzer: "lowercase_keyword"#, included_in_all: false
indexes :created_at, type: :date, included_in_all: false
indexes :updated_at, type: :date, included_in_all: false
indexes :last_activity_at, type: :date, included_in_all: false
indexes :comment_count, type: :integer, included_in_all: false
indexes :votes_point, type: :integer, as: 'votes_point', included_in_all: false
indexes :course_id, type: :string, index: :not_analyzed, incldued_in_all: false
indexes :commentable_id, type: :string, index: :not_analyzed, incldued_in_all: false
indexes :author_id, type: :string, as: 'author_id', index: :not_analyzed, incldued_in_all: false
end
belongs_to :author, class_name: "User", inverse_of: :comment_threads, index: true#, autosave: true
......@@ -57,6 +79,13 @@ class CommentThread < Content
before_create :set_last_activity_at
before_update :set_last_activity_at
def self.recreate_index
Tire.index 'comment_threads' do
delete
create
CommentThread.tire.index.import CommentThread.all
end
end
def self.new_dumb_thread(options={})
c = self.new
......@@ -102,15 +131,6 @@ class CommentThread < Content
doc
end
def self.search_text(text, commentable_id=nil)
self.solr_search do
fulltext(text)
if commentable_id
with(:commentable_id, commentable_id)
end
end
end
def self.tag_name_valid?(tag)
!!(tag =~ RE_TAG)
end
......
......@@ -17,8 +17,6 @@ describe "app" do
thread2.tags = [ai, ml, random2].join ","
thread2.save
Sunspot.commit
get "/api/v1/search/threads", tags: [ai, ml].join(",")
last_response.should be_ok
threads = parse(last_response.body)['collection']
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment