class Documentation::Searchers::Elasticsearch
Public Instance Methods
delete(page)
click to toggle source
# File lib/documentation/searchers/elasticsearch.rb, line 13 def delete(page) client.delete(:index => index_name, :type => 'page', :id => page.id) rescue ::Elasticsearch::Transport::Transport::Errors::NotFound false end
index(page)
click to toggle source
# File lib/documentation/searchers/elasticsearch.rb, line 9 def index(page) client.index(:index => index_name, :type => 'page', :id => page.id, :body => page_to_hash(page)) end
reset()
click to toggle source
# File lib/documentation/searchers/elasticsearch.rb, line 19 def reset client.indices.delete(:index => index_name) end
search(query, options = {})
click to toggle source
# File lib/documentation/searchers/elasticsearch.rb, line 23 def search(query, options = {}) # Default options options[:page] ||= 1 options[:per_page] ||= 15 # Prepare our query body = { :query => { :simple_query_string => {:query => query, :fields => [:title, :content]} } } # Get the total number of results count = client.count(:index => index_name, :body => body) # Get some actual results for the requested page result = client.search(:index => index_name, :body => body.merge({ :from => (options[:page].to_i - 1) * options[:per_page].to_i, :size => options[:per_page].to_i, :highlight => { :pre_tags => ["{{{"], :post_tags => ["}}}"], :fields => {:content => {}} } })) # Create a result object to be returned search_result = Documentation::SearchResult.new search_result.page = options[:page].to_i search_result.per_page = options[:per_page].to_i search_result.total_results = count['count'].to_i search_result.query = query search_result.time = result['took'] search_result.raw_results = result['hits']['hits'].inject(Hash.new) do |hash, hit| hash[hit['_id'].to_i] = { :score => hit['_score'], :highlights => hit['highlight'] && hit['highlight']['content'] } hash end # Return it search_result rescue ::Elasticsearch::Transport::Transport::Errors::NotFound Documentation::SearchResult.new end
setup()
click to toggle source
# File lib/documentation/searchers/elasticsearch.rb, line 5 def setup require 'elasticsearch' end
Private Instance Methods
client()
click to toggle source
# File lib/documentation/searchers/elasticsearch.rb, line 89 def client @client ||= ::Elasticsearch::Client.new(options[:client] || {}) end
index_name()
click to toggle source
# File lib/documentation/searchers/elasticsearch.rb, line 71 def index_name options[:index_name] || 'pages' end
page_to_hash(page)
click to toggle source
# File lib/documentation/searchers/elasticsearch.rb, line 75 def page_to_hash(page) { :id => page.id, :title => page.title, :permalink => page.permalink, :full_permalink => page.full_permalink, :content => page.content, :created_at => page.created_at, :updated_at => page.updated_at } end