Permalink
Browse files

improving the search

  • Loading branch information...
1 parent 66dbedf commit 02d60219b9f06681ad09ecb09c88769e30ffb0f5 @bry4n bry4n committed Jul 12, 2012
Showing with 12 additions and 6 deletions.
  1. +12 −6 lib/searchable.rb
View
@@ -9,7 +9,7 @@ def self.search(keywords, options = {})
search_type = (class_name == "section" ? "book" : "doc")
type_name = (search_type == "book" ? "section" : "name")
category_name = (search_type == "book" ? "Book" : "Reference")
-
+ format = (search_type == "book" ? "html" : "text")
query_options = {
"size" => 10,
"query" => {
@@ -21,15 +21,15 @@ def self.search(keywords, options = {})
"highlight" => {
"pre_tags" => ["[highlight]"],
"post_tags" => ["[xhighlight]"],
- "fields" => { "html" => { "fragment_size" => 200 } }
+ "fields" => { format => { "fragment_size" => 200 } }
}
}
lang_options = {"must" => [{ "term" => { "lang" => options[:lang] } }]}
query_options["query"]["bool"].merge!(lang_options) if options[:lang].present?
keywords.split(/\s|\-/).each do |keyword|
query_options['query']['bool']['should'] << { "prefix" => { type_name => { "value" => keyword, "boost" => 12.0 } } }
- query_options['query']['bool']['should'] << { "term" => { "html" => keyword } }
+ query_options['query']['bool']['should'] << { "term" => { format => keyword } }
end
search = Tire::Search::Search.new(ELASTIC_SEARCH_INDEX, :type => search_type, :payload => query_options) rescue nil
@@ -39,13 +39,19 @@ def self.search(keywords, options = {})
results.each do |result|
name = result.section || result.chapter || result.name
slug = result.id.gsub('---','/')
- ref_hits << {
+ highlight = if search_type == 'book'
+ result.highlight.html.first rescue nil
+ else
+ result.highlight.text.first rescue nil
+ end
+ hit = {
:name => name,
- :meta => result.meta,
:score => result._score,
- :highlight => result.highlight,
+ :highlight => highlight,
:url => (search_type == "book" ? "/book/#{slug}" : "/docs/#{name}")
}
+ hit.merge!({:meta => result.meta}) if search_type == "book"
+ ref_hits << hit
end
if ref_hits.size > 0
return {:category => category_name, :term => keywords, :matches => ref_hits}

0 comments on commit 02d6021

Please sign in to comment.