73 lines
1.9 KiB
Ruby
73 lines
1.9 KiB
Ruby
module JamRuby
|
|
# not a active_record model; just a search result
|
|
class Search
|
|
attr_accessor :bands, :musicians, :fans, :recordings
|
|
|
|
def self.search(query)
|
|
# empty queries don't hit back to elasticsearch
|
|
if query.nil? || query.length == 0
|
|
return Search.new(nil)
|
|
end
|
|
|
|
s = Tire.search [User.index_name, Band.index_name], :load => true do
|
|
query { string query }
|
|
sort { by [:_score] }
|
|
from 0
|
|
size 10 # doesn't have to be hardcoded...
|
|
end
|
|
|
|
return Search.new(s)
|
|
end
|
|
|
|
# elasticsearch index settings
|
|
def self.index_settings()
|
|
return {
|
|
"analysis" => {
|
|
"analyzer" => {
|
|
"default" => {
|
|
"type" => "custom",
|
|
"tokenizer" => "lowercase",
|
|
"filter" => ["name_ngram"]
|
|
}
|
|
},
|
|
"filter" => {
|
|
"name_ngram" => {
|
|
"type" => 'edgeNGram',
|
|
"min_gram" => 2,
|
|
"max_gram" => 7,
|
|
"side" => "front"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
end
|
|
|
|
# search_results - results from a Tire search across band/user/recording
|
|
def initialize(search_results)
|
|
@bands = []
|
|
@musicians = []
|
|
@fans = []
|
|
@recordings = []
|
|
|
|
if search_results.nil?
|
|
return
|
|
end
|
|
|
|
search_results.results.each do |result|
|
|
if result.class == User
|
|
if result.musician
|
|
@musicians.push(result)
|
|
else
|
|
@fans.push(result)
|
|
end
|
|
elsif result.class == Band
|
|
@bands.push(result)
|
|
elsif result.class == Recording
|
|
@recordings.push(result)
|
|
else
|
|
raise Exception, "unknown class #{result.class} returned in search results"
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end |