-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplication.rb
More file actions
executable file
·197 lines (166 loc) · 5.74 KB
/
application.rb
File metadata and controls
executable file
·197 lines (166 loc) · 5.74 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/env ruby
require 'sinatra'
class ARTICLESEMANTICIZER < Sinatra::Base
require File.join(File.dirname(__FILE__), 'environment')
set :haml, :format => :html5
set :public_folder, 'public'
options = {
tracker: ArticleSemanticizer::Config.google_ua_account,
domain: ArticleSemanticizer::Config.google_ua_domain
}
use Rack::GoogleAnalytics, options if environment == :production
helpers WillPaginate::Sinatra::Helpers
helpers Sinatra::ContentFor
helpers do
def paginate(collection)
options = {
inner_window: 3,
outer_window: 3,
previous_label: '«',
next_label: '»'
}
will_paginate collection, options
end
def h(text)
Rack::Utils.escape_html(text)
end
end
def sanitize_string_for_elasticsearch_string_query(str)
# Escape special characters
return if !str.present?
escaped_characters = Regexp.escape('\\+-:*()[]{}!')
str = str.gsub(/([#{escaped_characters}])/, '\\\\\1')
# Replace AND, OR, NOT (note the upper case) with lower case equivalent when surrounded by word boundaries
['and', 'or', 'not'].each do |word|
escaped_word = word.split('').map {|char| "\\#{char}" }.join('')
str = str.gsub(/\s*\b(#{word.upcase})\b\s*/, " #{escaped_word} ")
end
# Remove odd quotes
quote_count = str.count '"'
str = str.gsub(/(.*)"(.*)/, '\1\"\3') if quote_count % 2 == 1
str
end
def execute_search(type = 'article')
@results = []
searched_term = params[:q]
geo = params[:geo]
return if !(searched_term.present? || geo.present?)
sort_year = params[:sort_year]
page = (params[:page] || 1).to_i
search_size = (params[:per] || 20).to_i
clean_searched_term = sanitize_string_for_elasticsearch_string_query(searched_term)
center = params[:c] || "0,0"
radius = (params[:r] || 0).to_s + "km"
bounds = (params[:b] || "0,0,0,0").split(",").map(&:to_f) rescue [0,0,0,0]
polygon = YAML::load(params[:p] || "[[0,0]]").map{ |n| n.reverse } rescue []
body = { query: { match_all: {} } }
sort = ""
sort = "year:asc" if sort_year == 'asc'
sort = "year:desc" if sort_year == 'desc'
fields = "id,citation.content"
client = Elasticsearch::Client.new
if searched_term.present?
if searched_term.include?(":")
components = searched_term.split(":",2)
body = { query: { match: Hash[components[0], components[1]] } }
elsif (type == 'scientific')
body = {
query: {
multi_match: {
query: clean_searched_term,
type: 'best_fields',
fields: ["name", "epithet", "genus_abbrev"],
tie_breaker: 0.3
}
}
}
fields = "id,name"
elsif (type == 'vernacular')
body = { query: { match: { name: clean_searched_term } } }
fields = "id,name"
else
body = {
query: {
bool: {
should: [
match: { "citation.scientific_names" => clean_searched_term, boost: 5.0 },
match: { "abstract.scientific_names" => clean_searched_term, boost: 3.0 },
match: { "full_text.scientific_names" => clean_searched_term, boost: 2.0 },
match: { "citation.vernacular_names.name" => clean_searched_term, boost: 1.5 },
match: { "abstract.vernacular_names.name" => clean_searched_term, boost: 1.5 },
match: { "full_text.vernacular_names.name" => clean_searched_term, boost: 1.5 },
match: { "citation.content" => clean_searched_term },
match: { "abstract.content" => clean_searched_term },
match: { "full_text.content" => clean_searched_term }
]
}
}
}
end
end
if geo.present?
case geo
when 'circle'
body["filter"] = { geo_distance: { "full_text.places.location" => center, distance: radius } }
when 'rectangle'
body["filter"] = { geo_bounding_box: { "full_text.places.location" => { top_left: [bounds[1],bounds[2]], bottom_right: [bounds[3],bounds[0]] } } }
when 'polygon'
body["filter"] = { geo_polygon: { "full_text.places.location" => { points: polygon } } }
end
end
from = (page -1) * search_size
response = client.search index: ArticleSemanticizer::Config.elastic_index, type: type, fields: fields, from: from, size: search_size, sort: sort, body: body
results = response["hits"].deep_symbolize_keys
@results = WillPaginate::Collection.create(page, search_size, results[:total]) do |pager|
pager.replace results[:hits]
end
end
def get_article(id)
article = nil
client = Elasticsearch::Client.new
begin
response = client.get index: ArticleSemanticizer::Config.elastic_index, type: 'article', id: id
article = response["_source"].deep_symbolize_keys
if !ArticleSemanticizer::Config.enable_downloads
article.delete(:pdf)
article.delete(:txt)
end
rescue
end
@result = article
end
def format_names
@results.map{ |n| n[:fields][:name][0] }.sort
end
get '/' do
execute_search
haml :home
end
get '/article/:id' do
get_article(params[:id].to_i)
haml :article
end
get '/api.?:format?' do
execute_search
case params[:format]
when 'json'
@results.to_json
end
end
get '/scientific.json' do
execute_search('scientific')
format_names.to_json
end
get '/vernacular.json' do
execute_search('vernacular')
format_names.to_json
end
get '/about' do
haml :about
end
get '/main.css' do
content_type 'text/css', charset: 'utf-8'
scss :main
end
run! if app_file == $0
end