/
app.rb
446 lines (392 loc) · 15 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# encoding: utf-8
#
# Stresstest using
# ab -kc 5 -t 5 http://127.0.0.1:4567/csv?query=t
#
require 'sinatra/base'
require File.expand_path '../../lib/picky', __FILE__
class ChangingItem
attr_reader :id, :name
def initialize id, name
@id, @name = id, name
end
end
class BookSearch < Sinatra::Application
include Picky
extend Picky::Sinatra
indexing substitutes_characters_with: Picky::CharacterSubstituters::WestEuropean.new,
removes_characters: /[^äöüa-zA-Z0-9\s\/\-\_\:\"\&\|]/i,
stopwords: /\b(and|the|or|on|of|in|is|to|from|as|at|an)\b/i,
splits_text_on: /[\s\/\-\_\:\"\&\/]/,
normalizes_words: [[/\$(\w+)/i, '\1 dollars']],
rejects_token_if: lambda { |token| token.blank? || token == 'Amistad' },
case_sensitive: false
searching substitutes_characters_with: Picky::CharacterSubstituters::WestEuropean.new,
removes_characters: /[^ïôåñëäöüa-zA-Z0-9\s\/\-\_\,\&\.\"\~\*\:]/i,
stopwords: /\b(and|the|or|on|of|in|is|to|from|as|at|an)\b/i,
splits_text_on: /[\s\/\&\/]/,
case_sensitive: true,
maximum_tokens: 5
books_index = Index.new :books do
source Sources::DB.new('SELECT id, title, author, year FROM books', file: 'db.yml')
category :id
category :title,
qualifiers: [:t, :title, :titre],
partial: Partial::Substring.new(:from => 1),
similarity: Similarity::DoubleMetaphone.new(2)
category :author, partial: Partial::Substring.new(:from => -2)
category :year, qualifiers: [:y, :year, :annee]
result_identifier 'boooookies'
end
class Book < ActiveRecord::Base; end
Book.establish_connection YAML.load(File.open('db.yml'))
book_each_index = Index.new :book_each do
key_format :to_s
source Book.order('title ASC')
category :id
category :title,
qualifiers: [:t, :title, :titre],
partial: Partial::Substring.new(:from => 1),
similarity: Similarity::DoubleMetaphone.new(2)
category :author, partial: Partial::Substring.new(:from => -2)
category :year, qualifiers: [:y, :year, :annee]
end
isbn_index = Index.new :isbn do
source Sources::DB.new("SELECT id, isbn FROM books", :file => 'db.yml')
category :isbn, :qualifiers => [:i, :isbn]
end
class EachRSSItemProxy
def each &block
require 'rss'
require 'open-uri'
rss_feed = "http://florianhanke.com/blog/atom.xml"
rss_content = ""
open rss_feed do |f|
rss_content = f.read
end
rss = RSS::Parser.parse rss_content, true
rss.items.each &block
rescue
# Don't call block, no data.
end
end
rss_index = Index.new :rss do
source EachRSSItemProxy.new
key_format :to_s
category :title
# etc...
end
# Breaking example to test the nice error message.
#
# breaking = Index.new :isbn, Sources::DB.new("SELECT id, isbn FROM books", :file => 'db.yml') do
# category :isbn, :qualifiers => [:i, :isbn]
# end
# Fake ISBN class to demonstrate that #each indexing is working.
#
class ISBN
@@id = 1
attr_reader :id, :isbn
def initialize isbn
@id = @@id += 1
@isbn = isbn
end
end
isbn_each_index = Index.new :isbn_each do
source [ISBN.new('ABC'), ISBN.new('DEF')]
category :isbn, :qualifiers => [:i, :isbn], :key_format => :to_s
end
mgeo_index = Index.new :memory_geo do
source Sources::CSV.new(:location, :north, :east, file: 'data/ch.csv', col_sep: ',')
category :location
ranged_category :north1, 0.008, precision: 3, from: :north
ranged_category :east1, 0.008, precision: 3, from: :east
end
real_geo_index = Index.new :real_geo do
source Sources::CSV.new(:location, :north, :east, file: 'data/ch.csv', col_sep: ',')
category :location, partial: Partial::Substring.new(from: 1)
geo_categories :north, :east, 1, precision: 3
end
iphone_locations = Index.new :iphone do
source Sources::CSV.new(
:mcc,
:mnc,
:lac,
:ci,
:timestamp,
:latitude,
:longitude,
:horizontal_accuracy,
:altitude,
:vertical_accuracy,
:speed,
:course,
:confidence,
file: 'data/iphone_locations.csv'
)
ranged_category :timestamp, 86_400, precision: 5, qualifiers: [:ts, :timestamp]
geo_categories :latitude, :longitude, 25, precision: 3
end
Index.new :underscore_regression do
source Sources::CSV.new(:location, file: 'data/ch.csv')
category :some_place, :from => :location
end
# rgeo_index = Index.new :redis_geo, Sources::CSV.new(:location, :north, :east, file: 'data/ch.csv', col_sep: ',')
# rgeo_index.define_category :location
# rgeo_index.define_map_location(:north1, 1, precision: 3, from: :north)
# .define_map_location(:east1, 1, precision: 3, from: :east)
csv_test_index = Index.new :csv_test do
source Sources::CSV.new(:title,:author,:isbn,:year,:publisher,:subjects, file: 'data/books.csv')
category :title,
qualifiers: [:t, :title, :titre],
partial: Partial::Substring.new(from: 1),
similarity: Similarity::DoubleMetaphone.new(2)
category :author,
qualifiers: [:a, :author, :auteur],
partial: Partial::Substring.new(from: -2)
category :year,
qualifiers: [:y, :year, :annee],
partial: Partial::None.new
category :publisher, qualifiers: [:p, :publisher]
category :subjects, qualifiers: [:s, :subject]
result_identifier :Books
end
indexing_index = Index.new(:special_indexing) do
source Sources::CSV.new(:title, file: 'data/books.csv')
indexing removes_characters: /[^äöüd-zD-Z0-9\s\/\-\"\&\.]/i, # a-c, A-C are removed
splits_text_on: /[\s\/\-\"\&\/]/
category :title,
qualifiers: [:t, :title, :titre],
partial: Partial::Substring.new(from: 1),
similarity: Similarity::DoubleMetaphone.new(2)
end
redis_index = Index.new(:redis) do
backend Backends::Redis.new
source Sources::CSV.new(:title, :author, :isbn, :year, :publisher, :subjects, file: 'data/books.csv')
category :title,
qualifiers: [:t, :title, :titre],
partial: Partial::Substring.new(from: 1),
similarity: Similarity::DoubleMetaphone.new(2)
category :author,
qualifiers: [:a, :author, :auteur],
partial: Partial::Substring.new(from: -2)
category :year,
qualifiers: [:y, :year, :annee],
partial: Partial::None.new
category :publisher, qualifiers: [:p, :publisher]
category :subjects, qualifiers: [:s, :subject]
end
sym_keys_index = Index.new :symbol_keys do
source Sources::CSV.new(:text, file: "data/#{PICKY_ENVIRONMENT}/symbol_keys.csv", key_format: 'strip')
category :text, partial: Partial::Substring.new(from: 1)
end
memory_changing_index = Index.new(:memory_changing) do
source [
ChangingItem.new("1", 'first entry'),
ChangingItem.new("2", 'second entry'),
ChangingItem.new("3", 'third entry')
]
category :name
end
redis_changing_index = Index.new(:redis_changing) do
backend Backends::Redis.new
source [
ChangingItem.new("1", 'first entry'),
ChangingItem.new("2", 'second entry'),
ChangingItem.new("3", 'third entry')
]
category :name
end
file_index = Picky::Index.new(:file) do
backend Picky::Backends::File.new
source [
ChangingItem.new("1", 'first entry'),
ChangingItem.new("2", 'second entry'),
ChangingItem.new("3", 'third entry')
]
category :name,
partial: Picky::Partial::Infix.new(min: -3)
end
japanese_index = Picky::Index.new(:japanese) do
source Picky::Sources::CSV.new(:japanese, :german, :file => "data/japanese.tab", :col_sep => "\t")
indexing :removes_characters => /[^\p{Han}\p{Katakana}\p{Hiragana}\s;]/,
:stopwords => /\b(and|the|of|it|in|for)\b/i,
:splits_text_on => /[\s;]/
category :japanese,
:partial => Picky::Partial::Substring.new(from: 1)
end
BackendModel = Struct.new :id, :name
# To test the interface definition.
#
class InternalBackendInterfaceTester
def initialize
@hash = {}
end
def [] key
@hash[key]
end
def []= key, value
@hash[key] = value
end
def dump_json hash
end
end
backends_index = Picky::Index.new(:backends) do
source [
BackendModel.new(1, "Memory"),
BackendModel.new(2, "Redis")
]
backend Picky::Backends::Memory.new(
inverted: ->(bundle) do
Picky::Backends::Memory::JSON.new(bundle.index_path(:inverted))
end,
weights: Picky::Backends::Memory::JSON.new(
"#{PICKY_ROOT}/index/#{PICKY_ENVIRONMENT}/funky_weights_path",
empty: InternalBackendInterfaceTester.new,
initial: InternalBackendInterfaceTester.new
)
)
category :name
end
# This checks that we can use a funky customized tokenizer.
#
NonStringDataSource = Struct.new :id, :nonstring
class NonStringTokenizer < Picky::Tokenizer
def tokenize nonstring
[nonstring.map(&:to_sym)]
end
end
nonstring_data_index = Picky::Index.new(:nonstring) do
source {
[
NonStringDataSource.new(1, ['gaga', :blabla, 'haha']),
NonStringDataSource.new(2, [:meow, 'moo', :bang, 'zap'])
]
}
indexing NonStringTokenizer.new
category :nonstring
end
PartialItem = Struct.new :id, :substring, :postfix, :infix, :none
partial_index = Picky::Index.new(:partial) do
source do
[
PartialItem.new(1, "octopussy", "octopussy", "octopussy", "octopussy"),
PartialItem.new(2, "abracadabra", "abracadabra", "abracadabra", "abracadabra")
]
end
category :substring, partial: Picky::Partial::Substring.new(from: -5, to: -3)
category :postfix, partial: Picky::Partial::Postfix.new(from: -5)
category :infix, partial: Picky::Partial::Infix.new
category :none, partial: Picky::Partial::None.new
end
# This just tests indexing.
#
WeightsItem = Struct.new :id, :logarithmic, :constant_default, :constant, :dynamic
Picky::Index.new(:weights) do
source do
[
WeightsItem.new(1, "octopussy", "octopussy", "octopussy", "octopussy"),
WeightsItem.new(2, "abracadabra", "abracadabra", "abracadabra", "abracadabra")
]
end
category :logarithmic, weights: Picky::Weights::Logarithmic.new
category :constant_default, weights: Picky::Weights::Constant.new
category :constant, weights: Picky::Weights::Constant.new(3.14)
category :dynamic, weights: Picky::Weights::Dynamic.new { |token| token.size }
end
weights = {
[:author] => +6,
[:title, :author] => +5,
[:author, :year] => +2
}
# This looks horrible – but usually you have it only once or twice.
# It's flexible.
#
books_search = Search.new books_index, isbn_index do boost weights end
get %r{\A/books\Z} do
books_search.search(params[:query], params[:ids] || 20, params[:offset] || 0).to_json
end
books_ignoring_search = Search.new books_index, isbn_index do
boost weights
ignore_unassigned_tokens true
end
get %r{\A/books_ignoring\Z} do
books_ignoring_search.search(params[:query], params[:ids] || 20, params[:offset] || 0).to_json
end
book_each_search = Search.new book_each_index do
boost weights
ignore :title
end
get %r{\A/book_each\Z} do
book_each_search.search(params[:query], params[:ids] || 20, params[:offset] || 0).to_json
end
redis_search = Search.new redis_index do boost weights end
get %r{\A/redis\Z} do
redis_search.search(params[:query], params[:ids] || 20, params[:offset] || 0).to_json
end
memory_changing_search = Search.new memory_changing_index
get %r{\A/memory_changing\Z} do
memory_changing_search.search(params[:query], params[:ids] || 20, params[:offset] || 0).to_json
end
redis_changing_search = Search.new redis_changing_index
get %r{\A/redis_changing\Z} do
redis_changing_search.search(params[:query], params[:ids] || 20, params[:offset] || 0).to_json
end
csv_test_search = Search.new csv_test_index do boost weights end
get %r{\A/csv\Z} do
csv_test_search.search(params[:query], params[:ids] || 20, params[:offset] || 0).to_json
end
isbn_search = Search.new isbn_index
get %r{\A/isbn\Z} do
isbn_search.search(params[:query], params[:ids] || 20, params[:offset] || 0).to_json
end
sym_keys_search = Search.new sym_keys_index
get %r{\A/sym\Z} do
sym_keys_search.search(params[:query], params[:ids] || 20, params[:offset] || 0).to_json
end
real_geo_search = Search.new real_geo_index
get %r{\A/geo\Z} do
real_geo_search.search(params[:query], params[:ids] || 20, params[:offset] || 0).to_json
end
mgeo_search = Search.new mgeo_index
get %r{\A/simple_geo\Z} do
mgeo_search.search(params[:query], params[:ids] || 20, params[:offset] || 0).to_json
end
iphone_search = Search.new iphone_locations
get %r{\A/iphone\Z} do
iphone_search.search(params[:query], params[:ids] || 20, params[:offset] || 0).to_json
end
indexing_search = Search.new indexing_index
get %r{\A/indexing\Z} do
indexing_search.search(params[:query], params[:ids] || 20, params[:offset] || 0).to_json
end
file_search = Search.new file_index
get %r{\A/file\Z} do
file_search.search(params[:query], params[:ids] || 20, params[:offset] || 0).to_json
end
japanese_search = Search.new japanese_index do
searching removes_characters: /[^\p{Han}\p{Katakana}\p{Hiragana}\"\~\*\:\,]/i, # a-zA-Z0-9\s\/\-\_\&\.
stopwords: /\b(and|the|of|it|in|for)\b/i,
splits_text_on: /[\s\/\-\&]+/
end
get %r{\A/japanese\Z} do
japanese_search.search(params[:query], params[:ids] || 20, params[:offset] || 0).to_json
end
backends_search = Search.new backends_index do
searching case_sensitive: false
end
get %r{\A/backends\Z} do
backends_search.search(params[:query], params[:ids] || 20, params[:offset] || 0).to_json
end
nonstring_search = Search.new nonstring_data_index
get %r{\A/nonstring\Z} do
nonstring_search.search(params[:query], params[:ids] || 20, params[:offset] || 0).to_json
end
partial_search = Search.new partial_index
get %r{\A/partial\Z} do
partial_search.search(params[:query], params[:ids] || 20, params[:offset] || 0).to_json
end
all_search = Search.new books_index, csv_test_index, isbn_index, mgeo_index do boost weights end
get %r{\A/all\Z} do
all_search.search(params[:query], params[:ids] || 20, params[:offset] || 0).to_json
end
end