-
Notifications
You must be signed in to change notification settings - Fork 0
/
searchable.rb
50 lines (45 loc) · 1.16 KB
/
searchable.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
module Searchable
extend ActiveSupport::Concern
included do
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
end
def self.included(m)
def m.fuzzy_search(query, fuzziness=1, size=25)
self.__elasticsearch__.search(
query: {
match: {
_all: {
query: query,
fuzziness: fuzziness
}
}
},
size: size
)
end
def m.personalize_search(query)
self.__elasticsearch__.search(query)
end
def m.fuzzy_suggest(query, suggest, fuzziness=1, size=25)
self.__elasticsearch__.client.suggest(
index: self.index_name,
body: {
suggestions: {
text: query,
completion: {
field: suggest,
size: size,
fuzzy: {
fuzziness: fuzziness
}
}
}
}
)
end
def m.suggest(query, suggest, size=25)
self.fuzzy_suggest(query, suggest, 0, size)
end
end
end