soulheart 0.0.10 → 0.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/soulheart/base.rb +15 -6
- data/lib/soulheart/loader.rb +15 -11
- data/lib/soulheart/matcher.rb +34 -25
- data/lib/soulheart/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5c3a5a5f5c2e5fdce0a5270fa703ab31a72196a1
|
4
|
+
data.tar.gz: 96bad6202e435d62d9b25b32b1a7c96b333f8fe3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c8a67e56e9c167a820f0bba16d1437e5f84be7f971629f548939a17598cdbf32b07ff15542bb4d5b869448d0539634bbacc9502a781e2ce4d0c547dbdd541b89
|
7
|
+
data.tar.gz: 24d1d651a927629d63d06a1ea4d3038d056d9555d2baf6820dd4292d505d4174009495856801c69b76af9b9e17cb0d049a5d4227e32b07a4ef6a92708e5956a2
|
data/lib/soulheart/base.rb
CHANGED
@@ -8,7 +8,7 @@ module Soulheart
|
|
8
8
|
Soulheart.redis
|
9
9
|
end
|
10
10
|
|
11
|
-
def
|
11
|
+
def cache_duration
|
12
12
|
10 * 60 # Setting to 10 minutes, but making it possible to edit down the line
|
13
13
|
end
|
14
14
|
|
@@ -16,13 +16,22 @@ module Soulheart
|
|
16
16
|
ENV['RACK_ENV'] != 'test' ? 'soulheart:' : 'soulheart_test:'
|
17
17
|
end
|
18
18
|
|
19
|
+
def sorted_category_array
|
20
|
+
redis.smembers(categories_id).map { |c| normalize(c) }.uniq.sort
|
21
|
+
end
|
22
|
+
|
23
|
+
def combinatored_category_array
|
24
|
+
1.upto(sorted_category_array.size).
|
25
|
+
flat_map { |n| sorted_category_array.combination(n).
|
26
|
+
map { |el| el.join('') } }
|
27
|
+
end
|
28
|
+
|
19
29
|
def set_category_combos_array
|
20
30
|
redis.expire category_combos_id, 0
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
ar
|
31
|
+
array = combinatored_category_array
|
32
|
+
array.last.replace('all')
|
33
|
+
redis.sadd category_combos_id, array
|
34
|
+
array
|
26
35
|
end
|
27
36
|
|
28
37
|
def category_combos_id
|
data/lib/soulheart/loader.rb
CHANGED
@@ -67,7 +67,7 @@ module Soulheart
|
|
67
67
|
end
|
68
68
|
end
|
69
69
|
|
70
|
-
def
|
70
|
+
def clean_hash(item)
|
71
71
|
fail ArgumentError, 'Items must have text' unless item['text']
|
72
72
|
default_items_hash(item.delete('text'), item.delete('category'))
|
73
73
|
.tap { |i| i['data'].merge!(item.delete('data')) if item['data'] }
|
@@ -75,17 +75,21 @@ module Soulheart
|
|
75
75
|
.merge item
|
76
76
|
end
|
77
77
|
|
78
|
-
def
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
item.keys.select{ |k| !%w(category priority term aliases data).include?(k) }.each do |key|
|
83
|
-
item['data'].merge!({"#{key}" => item.delete(key)})
|
84
|
-
end
|
85
|
-
unless redis.smembers(categories_id).include?(item['category'])
|
86
|
-
redis.sadd categories_id, item['category']
|
87
|
-
end
|
78
|
+
def clean(item)
|
79
|
+
item = clean_hash(item)
|
80
|
+
item.keys.select{ |k| !%w(category priority term aliases data).include?(k) }.each do |key|
|
81
|
+
item['data'].merge!({"#{key}" => item.delete(key)})
|
88
82
|
end
|
83
|
+
unless redis.smembers(categories_id).include?(item['category'])
|
84
|
+
redis.sadd categories_id, item['category']
|
85
|
+
end
|
86
|
+
item
|
87
|
+
end
|
88
|
+
|
89
|
+
def add_item(item, category_base_id=nil, cleaned=false)
|
90
|
+
item = clean(item) unless cleaned
|
91
|
+
category_base_id ||= category_id(item['category'])
|
92
|
+
|
89
93
|
priority = (-item['priority'])
|
90
94
|
redis.pipelined do
|
91
95
|
redis.zadd(no_query_id(category_base_id), priority, item['term']) # Add to master set for queryless searches
|
data/lib/soulheart/matcher.rb
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
module Soulheart
|
2
2
|
class Matcher < Base
|
3
3
|
def initialize(params = {})
|
4
|
-
|
5
|
-
clean_opts
|
4
|
+
set_clean_opts(params)
|
6
5
|
end
|
7
6
|
|
8
7
|
attr_accessor :opts
|
@@ -17,17 +16,28 @@ module Soulheart
|
|
17
16
|
}
|
18
17
|
end
|
19
18
|
|
19
|
+
def sort_categories(categories)
|
20
|
+
return [] if categories.empty?
|
21
|
+
categories = categories.split(/,|\+/) unless categories.is_a?(Array)
|
22
|
+
categories = categories.map { |s| normalize(s) }.uniq.sort
|
23
|
+
categories = [] if categories.length == redis.scard(categories_id)
|
24
|
+
categories
|
25
|
+
end
|
26
|
+
|
20
27
|
def clean_opts
|
21
|
-
|
22
|
-
@opts['categories'] = @opts['categories'].split(/,|\+/) unless @opts['categories'].is_a?(Array)
|
23
|
-
@opts['categories'] = @opts['categories'].map { |s| normalize(s) }.uniq.sort
|
24
|
-
@opts['categories'] = [] if @opts['categories'].length == redis.scard(categories_id)
|
25
|
-
end
|
28
|
+
@opts['categories'] = sort_categories(@opts['categories'])
|
26
29
|
@opts['q'] = normalize(@opts['q']).split(' ') unless @opts['q'].is_a?(Array)
|
27
30
|
# .reject{ |i| i && i.length > 0 } .split(' ').reject{ Soulmate.stop_words.include?(w) }
|
28
31
|
@opts
|
29
32
|
end
|
30
33
|
|
34
|
+
def set_clean_opts(params)
|
35
|
+
@opts = self.class.default_params_hash.merge params
|
36
|
+
clean_opts
|
37
|
+
@cachekey = cache_id_from_opts
|
38
|
+
@cid = category_id_from_opts
|
39
|
+
end
|
40
|
+
|
31
41
|
def categories_string
|
32
42
|
@opts['categories'].empty? ? 'all' : @opts['categories'].join('')
|
33
43
|
end
|
@@ -40,32 +50,31 @@ module Soulheart
|
|
40
50
|
"#{cache_id(categories_string)}#{@opts['q'].join(':')}"
|
41
51
|
end
|
42
52
|
|
43
|
-
def interkeys_from_opts
|
53
|
+
def interkeys_from_opts
|
44
54
|
# If there isn't a query, we use a special key in redis
|
45
|
-
@opts['q'].empty? ? [no_query_id(cid)] : @opts['q'].map { |w| "#{cid}#{w}" }
|
55
|
+
@opts['q'].empty? ? [no_query_id(@cid)] : @opts['q'].map { |w| "#{@cid}#{w}" }
|
46
56
|
end
|
47
57
|
|
48
|
-
def
|
49
|
-
cachekey
|
50
|
-
|
58
|
+
def cache_it_because
|
59
|
+
redis.zinterstore(@cachekey, interkeys_from_opts)
|
60
|
+
redis.expire(@cachekey, cache_duration) # cache_duration is set in base.rb
|
61
|
+
end
|
51
62
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
63
|
+
def matching_hashes(ids)
|
64
|
+
return [] unless ids.size > 0
|
65
|
+
results = redis.hmget(results_hashes_id, *ids)
|
66
|
+
results = results.reject(&:nil?) # handle cached results for ids which have since been deleted
|
67
|
+
results.map { |r| MultiJson.decode(r) }
|
68
|
+
end
|
69
|
+
|
70
|
+
def matches
|
71
|
+
cache_it_because if !@opts['cache'] || !redis.exists(@cachekey) || redis.exists(@cachekey) == 0
|
57
72
|
offset = (@opts['page'].to_i - 1) * @opts['per_page'].to_i
|
58
73
|
limit = @opts['per_page'].to_i + offset - 1
|
59
74
|
|
60
75
|
limit = 0 if limit < 0
|
61
|
-
ids = redis.zrange(cachekey, offset, limit) # Using 'ids', even though keys are now terms
|
62
|
-
|
63
|
-
results = redis.hmget(results_hashes_id, *ids)
|
64
|
-
results = results.reject(&:nil?) # handle cached results for ids which have since been deleted
|
65
|
-
results.map { |r| MultiJson.decode(r) }
|
66
|
-
else
|
67
|
-
[]
|
68
|
-
end
|
76
|
+
ids = redis.zrange(@cachekey, offset, limit) # Using 'ids', even though keys are now terms
|
77
|
+
matching_hashes(ids)
|
69
78
|
end
|
70
79
|
end
|
71
80
|
end
|
data/lib/soulheart/version.rb
CHANGED