Commit 4ddc0624 by Rocky Duan

fixed searching text with tag bug

parent 51bc34db
......@@ -52,10 +52,22 @@ get "#{api_prefix}/search/threads" do
page = (params["page"] || 1).to_i
per_page = (params["per_page"] || 20).to_i
tags = params["tags"].split /,/ if params["tags"]
search = CommentThread.tire.search page: page, per_page: per_page, load: true do |search|
if params["text"] or params["tags"]
#search = Tire::Search::Search.new 'comment_threads', page: page, per_page: per_page
#search.query {|query| query.text(:_all, params["text"])} if params["text"]
#search.filter :bool, :must => tags.map{|tag| {:term => {:tags_array => tag}}} if params["tags"]
#search.highlight({title: { number_of_fragments: 0 } } , {body: { number_of_fragments: 0 } }, options: { tag: "<highlight>" })
#search.query {|query| query.boolean {|boolean| tags.each {|tag| boolean.must { string "tags_array:#{tag}" }}}} if params["tags"]
search = CommentThread.tire.search page: page, per_page: per_page do |search|
if params["text"]
search.query do |query|
query.text(:_all, params["text"]) if params["text"]
query.text(:_all, params["text"])
end
search.highlight({title: { number_of_fragments: 0 } } , {body: { number_of_fragments: 0 } }, options: { tag: "<highlight>" })
end
search.filter :bool, :must => tags.map{|tag| {:term => {:tags_array => tag}}} if params["tags"]
=begin
if params["tags"]
query.boolean do |boolean|
for tag in tags
......@@ -63,16 +75,15 @@ get "#{api_prefix}/search/threads" do
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
#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)},
collection: search.results.map{|t| CommentThread.search_result_to_hash(t, recursive: bool_recursive)},
num_pages: num_pages,
page: page,
}.to_json
......
......@@ -21,8 +21,8 @@ class CommentThread < Content
include Tire::Model::Callbacks
mapping do
indexes :title, type: :string, analyzer: :snowball, boost: 5.0
indexes :body, type: :string, analyzer: :snowball
indexes :title, type: :string, analyzer: :snowball, boost: 5.0, stored: true, term_vector: :with_positions_offsets
indexes :body, type: :string, analyzer: :snowball, stored: true, term_vector: :with_positions_offsets
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
......@@ -55,11 +55,8 @@ class CommentThread < Content
before_update :set_last_activity_at
def self.recreate_index
Tire.index 'comment_threads' do
delete
create
CommentThread.tire.index.import CommentThread.all
end
Tire.index 'comment_threads' do delete; end
CommentThread.create_elastic_index
end
def self.new_dumb_thread(options={})
......@@ -74,8 +71,29 @@ class CommentThread < Content
c
end
def self.search_text_with_highlight(text)
search = tire.search do |search|
search.query { |query| query.text :_all, text }
search.highlight({title: { number_of_fragments: 0 } } , {body: { number_of_fragments: 0 } }, options: { tag: "<strong>" })
end
search.results
end
def self.search_result_to_hash(result, params={})
comment_thread = self.find(result.id)
highlight = result.highlight || {}
highlighted_body = (highlight[:body] || []).first || comment_thread.body
highlighted_title = (highlight[:title] || []).first || comment_thread.title
find(result.id).to_hash(params).merge(highlighted_body: highlighted_body, highlighted_title: highlighted_title)
end
def self.search_tags(tags)
tire.search do |search|
=begin
search.query do |query|
query.boolean do |boolean|
for tag in tags
......@@ -83,6 +101,7 @@ class CommentThread < Content
end
end
end
=end
end.results
end
......
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