Permalink
Browse files

search url changed, dont error on empty responses

  • Loading branch information...
1 parent eafcd68 commit 53bade4969090b11c27c888f02bfa96fa41737d2 @schacon schacon committed Jul 3, 2012
Showing with 29 additions and 26 deletions.
  1. +11 −9 app/models/doc.rb
  2. +17 −15 app/models/section.rb
  3. +1 −2 config/initializers/elasticsearch.rb
View
20 app/models/doc.rb
@@ -35,15 +35,17 @@ def self.search(term, highlight = false)
resp = BONSAI.search('doc', options)
ref_hits = []
- resp['hits']['hits'].each do |hit|
- highlight = hit.has_key?('highlight') ? hit['highlight']['text'].first : nil
- name = hit["_source"]["name"]
- ref_hits << {
- :name => name,
- :score => hit["_score"],
- :highlight => highlight,
- :url => "/docs/#{name}"
- }
+ if resp
+ resp['hits']['hits'].each do |hit|
+ highlight = hit.has_key?('highlight') ? hit['highlight']['text'].first : nil
+ name = hit["_source"]["name"]
+ ref_hits << {
+ :name => name,
+ :score => hit["_score"],
+ :highlight => highlight,
+ :url => "/docs/#{name}"
+ }
+ end
end
if ref_hits.size > 0
View
32 app/models/section.rb
@@ -113,21 +113,23 @@ def self.search(term, lang = 'en', highlight = false)
resp = BONSAI.search('book', options)
ref_hits = []
- resp['hits']['hits'].each do |hit|
- name = hit["_source"]["section"]
- name = hit["_source"]["chapter"] if name.empty?
- slug = hit["_id"].gsub('---', '/')
- lang = hit["_source"]["lang"]
- meta = "Chapter " + hit["_source"]['number'] + ' : ' + hit["_source"]["chapter"]
- highlight = hit.has_key?('highlight') ? hit['highlight']['html'].first : nil
- ref_hits << {
- :name => name,
- :meta => meta,
- :score => hit["_score"],
- :highlight => highlight,
- :url => "/book/#{slug}"
- }
- end
+ if resp
+ resp['hits']['hits'].each do |hit|
+ name = hit["_source"]["section"]
+ name = hit["_source"]["chapter"] if name.empty?
+ slug = hit["_id"].gsub('---', '/')
+ lang = hit["_source"]["lang"]
+ meta = "Chapter " + hit["_source"]['number'] + ' : ' + hit["_source"]["chapter"]
+ highlight = hit.has_key?('highlight') ? hit['highlight']['html'].first : nil
+ ref_hits << {
+ :name => name,
+ :meta => meta,
+ :score => hit["_score"],
+ :highlight => highlight,
+ :url => "/book/#{slug}"
+ }
+ end
+ end
if ref_hits.size > 0
return { :category => "Book", :term => term, :matches => ref_hits }
View
3 config/initializers/elasticsearch.rb
@@ -1,9 +1,8 @@
require 'elasticsearch'
if ENV['SEARCH_INDEX_URL']
- uri = URI.parse(ENV['SEARCH_INDEX_URL'])
ELASTICSEARCH = {
- url: "#{uri.scheme}://#{uri.host}",
+ url: ENV['SEARCH_INDEX_URL'],
index_name: 'gitscm'
}
BONSAI = ElasticSearch::Index.new ELASTICSEARCH[:index_name], ELASTICSEARCH[:url]

0 comments on commit 53bade4

Please sign in to comment.