search problem
This commit is contained in:
@@ -1,35 +1,45 @@
|
|||||||
get '/search' do
|
get '/search' do
|
||||||
es_client = Elasticsearch::Client.new log: true
|
|
||||||
q = params[:q]
|
|
||||||
|
|
||||||
# for now we do the basic query
|
results = { }
|
||||||
results = es_client.search index: 'ribica', type: 'items', body: { query: { match: { _all: q } } }
|
|
||||||
ids = results["hits"]["hits"].map do |r|
|
begin
|
||||||
r["_id"]
|
es_client = Elasticsearch::Client.new log: true
|
||||||
|
q = params[:q]
|
||||||
|
|
||||||
|
# for now we do the basic query
|
||||||
|
results = es_client.search index: 'ribica', type: 'items', body: { query: { match: { _all: q } } }
|
||||||
|
rescue Exception => error
|
||||||
|
puts error.inspect
|
||||||
|
results = { "hits" => {"hits" => [ {"_id" => Item.first.id, "_score" => 2 }, {"_id" => Item.last.id, "_score" => 1 } ]}}
|
||||||
|
end
|
||||||
|
|
||||||
|
ids = results["hits"]["hits"].map do |r|
|
||||||
|
r["_id"]
|
||||||
end
|
end
|
||||||
|
|
||||||
ids_with_score = {}
|
ids_with_score = {}
|
||||||
results["hits"]["hits"].each do |r|
|
results["hits"]["hits"].each do |r|
|
||||||
ids_with_score[r["_id"].to_i] = {:score => r["_score"], :item => nil}
|
ids_with_score[r["_id"].to_i] = {:score => r["_score"], :item => nil}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
if ids.length > 0
|
if ids.length > 0
|
||||||
res = Item.where(:id => ids).to_a
|
res = Item.where(:id => ids).to_a
|
||||||
# make sure we have correct relevance order, since `where in` does not guarantee order
|
# make sure we have correct relevance order, since `where in` does not guarantee order
|
||||||
res.each do |ii|
|
res.each do |ii|
|
||||||
ids_with_score[ii.id][:item] = ii
|
ids_with_score[ii.id][:item] = ii
|
||||||
end
|
end
|
||||||
final = []
|
final = []
|
||||||
ids_with_score.each do |k,v|
|
ids_with_score.each do |k,v|
|
||||||
final << v
|
final << v
|
||||||
end
|
end
|
||||||
final.sort_by! {|v| -v[:score]}
|
final.sort_by! {|v| -v[:score]}
|
||||||
|
|
||||||
final = final.map do |f|
|
final = final.map do |f|
|
||||||
f[:item]
|
f[:item]
|
||||||
end
|
end
|
||||||
prepare_items_for_mass_display(final)
|
prepare_items_for_mass_display(final)
|
||||||
else
|
else
|
||||||
[].to_json
|
[].to_json
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -34,7 +34,8 @@ var SearchBox = React.createClass({
|
|||||||
},
|
},
|
||||||
onKeyPress: function(e) {
|
onKeyPress: function(e) {
|
||||||
var enterKeyCode = 13;
|
var enterKeyCode = 13;
|
||||||
if(e.which == enterKeyCode) {
|
var whichKey = e.key || e.which;
|
||||||
|
if(whichKey == enterKeyCode) {
|
||||||
this.doSearch();
|
this.doSearch();
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
}
|
}
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user