Slide 26
Slide 26 text
mruby_annoy on ngx_mruby
26
class NNS
def call(env)
params = env['QUERY_STRING'].split('&')
.map {|kv| kv.split('=') }.to_h
category_id = params['category_id'].to_i
product_id = params['product_id'].to_i
limit = (params['limit'] || 10).to_i
userdata = Userdata.new "annoy_data_key"
annoy = userdata.send("category_#{category_id}")
return not_found unless annoy
nns = annoy.get_nns_by_item(product_id, limit)
[200, content_type, [nns.to_json]]
end
private
def not_found
return [404, content_type,
[{'error' => 'not_found'}.to_json]]
end
def content_type
{'Content-Type' => 'application/json;charset=utf-8'}
end
end
run NNS.new