Commit 51bc34db by Rocky Duan

switched to elasticsearch

parent b4cef7d4
......@@ -176,6 +176,8 @@ namespace :db do
User.delete_all
Notification.delete_all
Subscription.delete_all
Tire.index 'comment_threads' do delete end
CommentThread.create_elasticsearch_index
beginning_time = Time.now
......@@ -194,11 +196,6 @@ namespace :db do
generate_comments_for("lab_1")
generate_comments_for("lab_2")
puts "reindexing solr..."
CommentThread.remove_all_from_index!
Sunspot.index!(CommentThread.all.to_a)
end_time = Time.now
puts "Number of comments generated: #{Comment.count}"
......
......@@ -51,17 +51,25 @@ get "#{api_prefix}/search/threads" do
else
page = (params["page"] || 1).to_i
per_page = (params["per_page"] || 20).to_i
binding.pry
tags = params["tags"].split /,/ if params["tags"]
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"]
if params["text"] or params["tags"]
search.query do |query|
query.text(:_all, params["text"]) if params["text"]
if params["tags"]
query.boolean do |boolean|
for tag in tags
boolean.must { string "tags_array:#{tag}" }
end
end
end
end
end
search.filter(:term, commentable_id: params["commentable_id"]) if params["commentable_id"]
search.filter(:term, course_id: params["course_id"]) if params["course_id"]
search.sort {|sort| sort.by sort_key, sort_order} if sort_key && sort_order
end
num_pages = search.total_pages
{
collection: search.results.map{|t| t.to_hash(recursive: bool_recursive)},
......
......@@ -20,36 +20,11 @@ class CommentThread < Content
include Tire::Model::Search
include Tire::Model::Callbacks
#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 :title, type: :string, analyzer: :snowball, boost: 5.0
indexes :body, type: :string, analyzer: :snowball
indexes :tags_in_text, type: :string, as: 'tags_array', index: :analyzed
indexes :tags_array, type: :string, as: 'tags_array', index: :not_analyzed, 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
......@@ -99,6 +74,18 @@ class CommentThread < Content
c
end
def self.search_tags(tags)
tire.search do |search|
search.query do |query|
query.boolean do |boolean|
for tag in tags
boolean.must { string "tags_array:#{tag}" }
end
end
end
end.results
end
def root_comments
Comment.roots.where(comment_thread_id: self.id)
end
......
......@@ -17,6 +17,8 @@ describe "app" do
thread2.tags = [ai, ml, random2].join ","
thread2.save
sleep 1
get "/api/v1/search/threads", tags: [ai, ml].join(",")
last_response.should be_ok
threads = parse(last_response.body)['collection']
......
......@@ -34,6 +34,8 @@ def init_without_subscriptions
User.delete_all
Notification.delete_all
Subscription.delete_all
Tire.index 'comment_threads' do delete end
CommentThread.create_elasticsearch_index
commentable = Commentable.new("question_1")
......@@ -105,6 +107,9 @@ def init_with_subscriptions
Notification.delete_all
Subscription.delete_all
Tire.index 'comment_threads' do delete end
CommentThread.create_elasticsearch_index
user1 = create_test_user(1)
user2 = create_test_user(2)
......
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