basic version of search implemented

This commit is contained in:
Edin Dazdarevic
2015-03-22 16:16:52 +01:00
parent 38548e3e33
commit 9154d216a2
11 changed files with 305 additions and 11 deletions

View File

@@ -1,7 +1,17 @@
# TODO: make this private, not-public facing.
# for now we keep this here for simplicity reasons
get '/search/index' do
es_client = Elasticsearch::Client.new log: true
all_items = Item.all.to_a
# first delete the index
begin
es_client.indices.delete index: 'ribica'
rescue
logger.warn "Ribica index could not be deleted. Continuing with indexing operation..."
end
# now index items
all_items = Item.includes(sub_category: { category: :section }).all.to_a
all_items.each do |item|
es_client.index index: 'ribica', type: 'items', id: item.id, body: {
title: 'Test',
@@ -16,3 +26,39 @@ get '/search/index' do
"ok".to_json
end
get '/search' do
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 } } }
ids = results["hits"]["hits"].map do |r|
r["_id"]
end
ids_with_score = {}
results["hits"]["hits"].each do |r|
ids_with_score[r["_id"].to_i] = {:score => r["_score"], :item => nil}
end
if ids.length > 0
res = Item.where(:id => ids).to_a
# make sure we have correct relevance order, since `where in` does not guarantee order
res.each do |ii|
ids_with_score[ii.id][:item] = ii
end
final = []
ids_with_score.each do |k,v|
final << v
end
final.sort_by! {|v| -v[:score]}
final = final.map do |f|
f[:item]
end
prepare_items_for_mass_display(final)
else
[].to_json
end
end

View File

@@ -23,11 +23,11 @@ ActiveRecord::Schema.define(version: 20150321052740) do
t.datetime "updated_at", null: false
t.string "anonymous_id_string"
t.integer "delivery_destination_id"
t.boolean "confirmed"
t.boolean "packed"
t.boolean "canceled_on_check"
t.boolean "canceled_on_delivery"
t.boolean "delivered"
t.boolean "confirmed", default: false
t.boolean "packed", default: false
t.boolean "canceled_on_check", default: false
t.boolean "canceled_on_delivery", default: false
t.boolean "delivered", default: false
t.text "internal_note"
end
@@ -109,8 +109,8 @@ ActiveRecord::Schema.define(version: 20150321052740) do
t.datetime "updated_at", null: false
t.string "tags"
t.json "traits"
t.integer "supplier_id"
t.decimal "weight", precision: 5, scale: 3
t.integer "supplier_id"
t.integer "delivery_time_estimation_id"
end