model.rb
2.9 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
module Searchkick
module Model
def searchkick(options = {})
custom_settings = {
analysis: {
analyzer: {
searchkick_keyword: {
type: "custom",
tokenizer: "keyword",
filter: ["lowercase", "snowball"]
},
default_index: {
type: "custom",
tokenizer: "standard",
# synonym should come last, after stemming and shingle
# shingle must come before snowball
filter: ["standard", "lowercase", "asciifolding", "stop", "snowball", "searchkick_index_shingle"]
},
searchkick_search: {
type: "custom",
tokenizer: "standard",
filter: ["standard", "lowercase", "asciifolding", "stop", "snowball", "searchkick_search_shingle"]
},
searchkick_search2: {
type: "custom",
tokenizer: "standard",
filter: ["standard", "lowercase", "asciifolding", "stop", "snowball"] #, "searchkick_search_shingle"]
}
},
filter: {
searchkick_index_shingle: {
type: "shingle",
token_separator: ""
},
# lucky find http://web.archiveorange.com/archive/v/AAfXfQ17f57FcRINsof7
searchkick_search_shingle: {
type: "shingle",
token_separator: "",
output_unigrams: false,
output_unigrams_if_no_shingles: true
}
}
}
}.merge(options[:settings] || {})
synonyms = options[:synonyms] || []
if synonyms.any?
custom_settings[:analysis][:filter][:searchkick_synonym] = {
type: "synonym",
ignore_case: true,
synonyms: synonyms.map{|s| s.join(" => ") } # TODO support more than 2 synonyms on a line
}
custom_settings[:analysis][:analyzer][:default_index][:filter] << "searchkick_synonym"
custom_settings[:analysis][:analyzer][:searchkick_search][:filter].insert(-2, "searchkick_synonym")
custom_settings[:analysis][:analyzer][:searchkick_search][:filter] << "searchkick_synonym"
custom_settings[:analysis][:analyzer][:searchkick_search2][:filter] << "searchkick_synonym"
end
class_eval do
extend Searchkick::Search
extend Searchkick::Reindex
include Tire::Model::Search
include Tire::Model::Callbacks
tire do
settings custom_settings
mapping do
index_name options[:index_name] if options[:index_name]
# indexes field, analyzer: "searchkick"
if options[:conversions]
indexes :conversions, type: "nested" do
indexes :query, analyzer: "searchkick_keyword"
indexes :count, type: "integer"
end
end
end
end
end
end
end
end