-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.rb
52 lines (39 loc) · 1.17 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
require 'json'
require 'sinatra/base'
require_relative 'models/restaurant'
require_relative 'models/category'
require_relative 'models/pref'
class Server < Sinatra::Base
get '/' do
@query = params[:q] if params[:q] && params[:q] != ''
if @query
# ES検索
res = Restaurant.search(@query)
ids = res[:ids]
# DBから取得
sanitized_query = ActiveRecord::Base.send(:sanitize_sql_array, ['field(id, ?)', ids])
@restaurants = Restaurant.where(id: ids).order(sanitized_query)
# 検索時間
@took = res[:response]['took']
# 検索Hit件数
@count = res[:response]['hits']['total']
# もしかして
@did_you_mean = res[:suggests]
# 都道府県Facet
@pref_id_facets = res[:response]['facets']['pref_id_facet']['terms']
# カテゴリFacet
@category_ids_facets = res[:response]['facets']['category_ids_facet']['terms']
else
@restaurants = Restaurant.all.limit(100)
end
erb :index
end
get '/complete.json' do
Restaurant.completion(params[:q]).map{ |term|
{
value: term['text'],
tokens: [term['text']]
}
}.to_json
end
end