picky 3.6.7 → 3.6.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. data/lib/picky/backends/file/basic.rb +1 -1
  2. data/lib/picky/backends/file/json.rb +5 -1
  3. data/lib/picky/backends/file.rb +7 -0
  4. data/lib/picky/backends/memory.rb +7 -0
  5. data/lib/picky/backends/redis/basic.rb +3 -11
  6. data/lib/picky/backends/redis/directly_manipulable.rb +48 -0
  7. data/lib/picky/backends/redis/list.rb +39 -15
  8. data/lib/picky/backends/redis/string.rb +17 -9
  9. data/lib/picky/backends/redis.rb +102 -66
  10. data/lib/picky/backends/sqlite/array.rb +38 -0
  11. data/lib/picky/backends/sqlite/basic.rb +100 -0
  12. data/lib/picky/backends/sqlite/directly_manipulable.rb +42 -0
  13. data/lib/picky/backends/sqlite/value.rb +34 -0
  14. data/lib/picky/backends/sqlite.rb +14 -4
  15. data/lib/picky/bundle.rb +12 -5
  16. data/lib/picky/bundle_indexed.rb +15 -2
  17. data/lib/picky/bundle_indexing.rb +6 -5
  18. data/lib/picky/bundle_realtime.rb +22 -31
  19. data/lib/picky/categories_realtime.rb +1 -1
  20. data/lib/picky/category_indexed.rb +1 -1
  21. data/lib/picky/category_indexing.rb +7 -5
  22. data/lib/picky/category_realtime.rb +17 -5
  23. data/lib/picky/generators/strategy.rb +4 -0
  24. data/lib/picky/index_indexing.rb +1 -4
  25. data/lib/picky/index_realtime.rb +16 -6
  26. data/lib/picky/indexers/base.rb +7 -1
  27. data/lib/picky/indexes.rb +1 -0
  28. data/lib/picky/loader.rb +11 -7
  29. data/lib/picky/query/allocation.rb +1 -1
  30. data/lib/picky/query/indexes.rb +2 -2
  31. data/lib/picky/query/token.rb +1 -1
  32. data/lib/picky/search.rb +20 -8
  33. data/lib/picky/tokenizer.rb +6 -6
  34. data/lib/picky/wrappers/bundle/delegators.rb +3 -1
  35. data/spec/category_realtime_spec.rb +33 -0
  36. data/spec/functional/backends/file_spec.rb +98 -0
  37. data/spec/functional/backends/memory_spec.rb +96 -0
  38. data/spec/functional/backends/redis_spec.rb +107 -0
  39. data/spec/functional/backends/sqlite_spec.rb +104 -0
  40. data/spec/{specific → functional}/dynamic_weights_spec.rb +0 -0
  41. data/spec/{specific → functional}/exact_first_spec.rb +2 -4
  42. data/spec/functional/max_allocations_spec.rb +33 -0
  43. data/spec/{specific → functional}/realtime_spec.rb +0 -0
  44. data/spec/{specific → functional}/regression_spec.rb +0 -0
  45. data/spec/{specific → functional}/speed_spec.rb +0 -0
  46. data/spec/lib/backends/file/basic_spec.rb +1 -1
  47. data/spec/lib/backends/redis/basic_spec.rb +12 -13
  48. data/spec/lib/backends/redis/directly_manipulable_spec.rb +91 -0
  49. data/spec/lib/backends/redis/float_spec.rb +17 -17
  50. data/spec/lib/backends/redis/list_spec.rb +9 -9
  51. data/spec/lib/backends/sqlite/array_spec.rb +143 -0
  52. data/spec/lib/backends/sqlite/directly_manipulable_spec.rb +65 -0
  53. data/spec/lib/backends/sqlite/{db_spec.rb → value_spec.rb} +2 -7
  54. data/spec/lib/backends/sqlite_spec.rb +22 -20
  55. data/spec/lib/category_indexed_spec.rb +1 -1
  56. data/spec/lib/category_indexing_spec.rb +2 -2
  57. data/spec/lib/index_indexing_spec.rb +0 -7
  58. data/spec/lib/index_realtime_spec.rb +34 -0
  59. data/spec/lib/indexed/bundle_realtime_spec.rb +166 -75
  60. data/spec/lib/indexers/base_spec.rb +13 -1
  61. data/spec/lib/search_spec.rb +31 -20
  62. metadata +58 -34
  63. data/lib/picky/backends/sqlite/db.rb +0 -84
@@ -0,0 +1,34 @@
1
+ module Picky
2
+
3
+ module Backends
4
+
5
+ class SQLite
6
+
7
+ class Value < Basic
8
+
9
+ def []= key, value
10
+ db.execute 'insert or replace into key_value (key, value) values (?,?)',
11
+ key.to_s,
12
+ Yajl::Encoder.encode(value)
13
+
14
+ value
15
+ end
16
+
17
+ def [] key
18
+ res = db.execute "select value from key_value where key = ? limit 1;", key.to_s
19
+ return nil if res.empty?
20
+
21
+ Yajl::Parser.parse res.first.first
22
+ end
23
+
24
+ def delete key
25
+ db.execute "delete from key_value where key = (?)", key.to_s
26
+ end
27
+
28
+ end
29
+
30
+ end
31
+
32
+ end
33
+
34
+ end
@@ -4,8 +4,11 @@ module Picky
4
4
 
5
5
  class SQLite < Backend
6
6
 
7
+ attr_reader :self_indexed
8
+
7
9
  def initialize options = {}
8
10
  super options
11
+ @self_indexed = options[:self_indexed]
9
12
 
10
13
  require 'sqlite3'
11
14
  rescue LoadError => e
@@ -17,28 +20,35 @@ module Picky
17
20
  #
18
21
  def create_inverted bundle
19
22
  extract_lambda_or(inverted, bundle) ||
20
- DB.new(bundle.index_path(:inverted))
23
+ Array.new(bundle.index_path(:inverted), self_indexed: self_indexed)
21
24
  end
22
25
  # Returns an object that on #initial, #load returns an object that responds to:
23
26
  # [:token] # => 1.23 (a weight)
24
27
  #
25
28
  def create_weights bundle
26
29
  extract_lambda_or(weights, bundle) ||
27
- DB.new(bundle.index_path(:weights))
30
+ Value.new(bundle.index_path(:weights), self_indexed: self_indexed)
28
31
  end
29
32
  # Returns an object that on #initial, #load returns an object that responds to:
30
33
  # [:encoded] # => [:original, :original] (an array of original symbols this similarity encoded thing maps to)
31
34
  #
32
35
  def create_similarity bundle
33
36
  extract_lambda_or(similarity, bundle) ||
34
- DB.new(bundle.index_path(:similarity))
37
+ Array.new(bundle.index_path(:similarity), self_indexed: self_indexed)
35
38
  end
36
39
  # Returns an object that on #initial, #load returns an object that responds to:
37
40
  # [:key] # => value (a value for this config key)
38
41
  #
39
42
  def create_configuration bundle
40
43
  extract_lambda_or(configuration, bundle) ||
41
- DB.new(bundle.index_path(:configuration))
44
+ Value.new(bundle.index_path(:configuration), self_indexed: self_indexed)
45
+ end
46
+ # Returns an object that on #initial, #load returns an object that responds to:
47
+ # [id] # => [:sym1, :sym2]
48
+ #
49
+ def create_realtime bundle
50
+ extract_lambda_or(similarity, bundle) ||
51
+ Array.new(bundle.index_path(:realtime), self_indexed: self_indexed)
42
52
  end
43
53
 
44
54
  # Returns the result ids for the allocation.
data/lib/picky/bundle.rb CHANGED
@@ -29,6 +29,7 @@ module Picky
29
29
  :weights,
30
30
  :similarity,
31
31
  :configuration,
32
+ :realtime,
32
33
 
33
34
  :backend_inverted,
34
35
  :backend_weights,
@@ -73,12 +74,11 @@ module Picky
73
74
  def reset_backend
74
75
  # Extract specific indexes from backend.
75
76
  #
76
- # TODO Clean up all related.
77
- #
78
77
  @backend_inverted = backend.create_inverted self
79
78
  @backend_weights = backend.create_weights self
80
79
  @backend_similarity = backend.create_similarity self
81
80
  @backend_configuration = backend.create_configuration self
81
+ @backend_realtime = backend.create_realtime self
82
82
 
83
83
  # Initial indexes.
84
84
  #
@@ -89,8 +89,7 @@ module Picky
89
89
  @weights = @weights_strategy.saved?? @backend_weights.initial : @weights_strategy
90
90
  @similarity = @backend_similarity.initial
91
91
  @configuration = @backend_configuration.initial
92
-
93
- @realtime_mapping = {} # id -> ary of syms. TODO Always instantiate? d
92
+ @realtime = @backend_realtime.initial
94
93
  end
95
94
 
96
95
  # "Empties" the index(es) by getting a new empty
@@ -101,6 +100,7 @@ module Picky
101
100
  empty_weights
102
101
  empty_similarity
103
102
  empty_configuration
103
+ empty_realtime
104
104
  end
105
105
  def empty_inverted
106
106
  @inverted = @backend_inverted.empty
@@ -116,6 +116,9 @@ module Picky
116
116
  def empty_configuration
117
117
  @configuration = @backend_configuration.empty
118
118
  end
119
+ def empty_realtime
120
+ @realtime = @backend_realtime.empty
121
+ end
119
122
 
120
123
  # Get a list of similar texts.
121
124
  #
@@ -124,7 +127,10 @@ module Picky
124
127
  def similar text
125
128
  code = similarity_strategy.encoded text
126
129
  similar_codes = code && @similarity[code]
127
- similar_codes.delete text if similar_codes
130
+ if similar_codes
131
+ similar_codes = similar_codes.dup # TODO
132
+ similar_codes.delete text
133
+ end
128
134
  similar_codes || []
129
135
  end
130
136
 
@@ -158,6 +164,7 @@ module Picky
158
164
  @backend_weights.delete if @backend_weights.respond_to?(:delete) && @weights_strategy.saved?
159
165
  @backend_similarity.delete if @backend_similarity.respond_to? :delete
160
166
  @backend_configuration.delete if @backend_configuration.respond_to? :delete
167
+ @backend_realtime.delete if @backend_realtime.respond_to? :delete
161
168
  end
162
169
 
163
170
  def to_s
@@ -16,12 +16,13 @@ module Picky
16
16
  #
17
17
  class Bundle
18
18
 
19
- attr_reader :realtime_mapping
20
-
21
19
  # Get the ids for the given symbol.
22
20
  #
23
21
  # Returns a (potentially empty) array of ids.
24
22
  #
23
+ # TODO Empty string ok, or does it need to be from the backend?
24
+ # Should the backend always return an empty array? (Probably no)
25
+ #
25
26
  def ids sym_or_string
26
27
  @inverted[sym_or_string] || []
27
28
  end
@@ -52,6 +53,7 @@ module Picky
52
53
  load_weights
53
54
  load_similarity
54
55
  load_configuration
56
+ load_realtime
55
57
  end
56
58
 
57
59
  # Loads the core index.
@@ -76,6 +78,11 @@ module Picky
76
78
  def load_configuration
77
79
  self.configuration = @backend_configuration.load
78
80
  end
81
+ # Loads the realtime mapping.
82
+ #
83
+ def load_realtime
84
+ self.realtime = @backend_realtime.load
85
+ end
79
86
 
80
87
  # Clears all indexes.
81
88
  #
@@ -84,6 +91,7 @@ module Picky
84
91
  clear_weights
85
92
  clear_similarity
86
93
  clear_configuration
94
+ clear_realtime
87
95
  end
88
96
 
89
97
  # Clears the core index.
@@ -106,6 +114,11 @@ module Picky
106
114
  def clear_configuration
107
115
  configuration.clear
108
116
  end
117
+ # Clears the realtime mapping.
118
+ #
119
+ def clear_realtime
120
+ realtime.clear
121
+ end
109
122
 
110
123
  end
111
124
 
@@ -28,11 +28,6 @@ module Picky
28
28
  #
29
29
  class Bundle
30
30
 
31
- # When indexing, clear only clears the inverted index.
32
- #
33
- delegate :clear,
34
- :to => :inverted
35
-
36
31
  # Saves the indexes in a dump file.
37
32
  #
38
33
  def dump
@@ -40,6 +35,7 @@ module Picky
40
35
  dump_similarity
41
36
  dump_weights
42
37
  dump_configuration
38
+ dump_realtime
43
39
  end
44
40
  # Dumps the core index.
45
41
  #
@@ -63,6 +59,11 @@ module Picky
63
59
  def dump_configuration
64
60
  @backend_configuration.dump @configuration
65
61
  end
62
+ # Dumps the realtime.
63
+ #
64
+ def dump_realtime
65
+ @backend_realtime.dump @realtime
66
+ end
66
67
 
67
68
  end
68
69
 
@@ -7,36 +7,31 @@ module Picky
7
7
  def remove id
8
8
  # Is it anywhere?
9
9
  #
10
- syms = @realtime_mapping[id]
10
+ syms = @realtime[id]
11
11
  return unless syms
12
12
 
13
13
  syms.each do |sym|
14
14
  ids = @inverted[sym]
15
15
  ids.delete id
16
16
 
17
- encoded = self.similarity_strategy.encoded sym
18
-
19
17
  if ids.empty?
20
- @inverted.delete sym
21
- @weights.delete sym
18
+ @inverted.delete sym
19
+ @weights.delete sym
22
20
  # Since no element uses this sym anymore, we can delete the similarity for it.
23
21
  # TODO Not really. Since multiple syms can point to the same encoded.
24
- @similarity.delete encoded
22
+ @similarity.delete self.similarity_strategy.encoded(sym)
25
23
  else
26
24
  @weights[sym] = self.weights_strategy.weight_for ids.size
27
25
  end
28
26
  end
29
27
 
30
- @realtime_mapping.delete id
28
+ @realtime.delete id
31
29
  end
32
30
 
33
31
  # Returns a reference to the array where the id has been added.
34
32
  #
35
33
  def add id, str_or_sym, where = :unshift
36
- ary = @inverted[str_or_sym]
37
-
38
- str_or_syms = @realtime_mapping[id]
39
- str_or_syms = (@realtime_mapping[id] = []) unless str_or_syms # TODO Nicefy.
34
+ str_or_syms = @realtime[id] || (@realtime[id] = [])
40
35
 
41
36
  # Inverted.
42
37
  #
@@ -46,7 +41,10 @@ module Picky
46
41
  ids.send where, id
47
42
  else
48
43
  str_or_syms << str_or_sym
49
- ids = @inverted[str_or_sym] ||= []
44
+
45
+ # TODO Introduce a new method?
46
+ #
47
+ ids = @inverted[str_or_sym] || (@inverted[str_or_sym] = []) # Ensures that we get an extended Array
50
48
  ids.send where, id
51
49
  end
52
50
 
@@ -69,13 +67,12 @@ module Picky
69
67
  #
70
68
  def add_similarity str_or_sym, where = :unshift
71
69
  if encoded = self.similarity_strategy.encoded(str_or_sym)
72
- similarity = @similarity[encoded] ||= []
73
- if similarity.include? str_or_sym
74
- similarity.delete str_or_sym # Not completely correct, as others will also be affected, but meh.
75
- similarity.send where, str_or_sym #
76
- else
77
- similarity.send where, str_or_sym
78
- end
70
+ similarity = @similarity[encoded] || (@similarity[encoded] = []) # Ensures that we get an extended Array
71
+
72
+ # Not completely correct, as others will also be affected, but meh.
73
+ #
74
+ similarity.delete str_or_sym if similarity.include? str_or_sym
75
+ similarity.send where, str_or_sym
79
76
  end
80
77
  end
81
78
 
@@ -87,29 +84,23 @@ module Picky
87
84
  end
88
85
  end
89
86
 
90
- # Clears the realtime mapping.
91
- #
92
- def clear_realtime_mapping
93
- @realtime_mapping.clear
94
- end
95
-
96
87
  # Builds the realtime mapping.
97
88
  #
98
89
  # Note: Experimental feature.
99
90
  #
100
91
  # TODO Subset of #add. Rewrite.
101
92
  #
102
- def build_realtime_mapping
103
- clear_realtime_mapping
93
+ def build_realtime
94
+ clear_realtime
104
95
  @inverted.each_pair do |str_or_sym, ids|
105
96
  ids.each do |id|
106
- str_or_syms = @realtime_mapping[id]
107
- str_or_syms = (@realtime_mapping[id] = []) unless str_or_syms # TODO Nicefy.
108
- @realtime_mapping[id] << str_or_sym unless str_or_syms.include? str_or_sym
97
+ str_or_syms = @realtime[id]
98
+ str_or_syms = (@realtime[id] = []) unless str_or_syms # TODO Nicefy.
99
+ @realtime[id] << str_or_sym unless str_or_syms.include? str_or_sym
109
100
  end
110
101
  end
111
102
  end
112
103
 
113
104
  end
114
105
 
115
- end
106
+ end
@@ -5,7 +5,7 @@ module Picky
5
5
  each_delegate :remove,
6
6
  :add,
7
7
  :replace,
8
- :clear_realtime_mapping,
8
+ :clear_realtime,
9
9
  :build_realtime_mapping,
10
10
  :to => :categories
11
11
 
@@ -8,7 +8,7 @@ module Picky
8
8
  #
9
9
  def load
10
10
  timed_exclaim %Q{"#{identifier}": Loading index from cache.}
11
- clear_realtime_mapping # TODO What to do?
11
+ clear_realtime # TODO What to do?
12
12
  exact.load
13
13
  partial.load
14
14
  end
@@ -51,7 +51,7 @@ module Picky
51
51
  empty
52
52
  retrieve
53
53
  dump
54
- clear_realtime_mapping # TODO To call or not to call, that is the question.
54
+ clear_realtime # TODO To call or not to call, that is the question.
55
55
  end
56
56
 
57
57
  # Retrieves the prepared index data into the indexes and
@@ -107,12 +107,14 @@ module Picky
107
107
  @tokenizer || @index.tokenizer
108
108
  end
109
109
 
110
- # Deletes the caches.
110
+ # Clears the caches.
111
+ #
112
+ # TODO Think about the semantics of clear.
113
+ # Is a delete even needed or is it clear+dump?
111
114
  #
112
115
  def clear
113
- timed_exclaim "Deleting #{identifier}."
114
- exact.delete
115
- partial.delete
116
+ exact.clear
117
+ partial.clear
116
118
  end
117
119
 
118
120
  end
@@ -27,6 +27,18 @@ module Picky
27
27
  add object, where
28
28
  end
29
29
 
30
+ # Add at the end.
31
+ #
32
+ def << thing
33
+ add thing, __method__
34
+ end
35
+
36
+ # Add at the beginning.
37
+ #
38
+ def unshift thing
39
+ add thing, __method__
40
+ end
41
+
30
42
  # For the given id, adds the list of
31
43
  # strings to the index for the given id.
32
44
  #
@@ -47,16 +59,16 @@ module Picky
47
59
 
48
60
  # Clears the realtime mapping.
49
61
  #
50
- def clear_realtime_mapping
51
- exact.clear_realtime_mapping
52
- partial.clear_realtime_mapping
62
+ def clear_realtime
63
+ exact.clear_realtime
64
+ partial.clear_realtime
53
65
  end
54
66
 
55
67
  # Builds the realtime mapping.
56
68
  #
57
69
  def build_realtime_mapping
58
- exact.build_realtime_mapping
59
- partial.build_realtime_mapping
70
+ exact.build_realtime
71
+ partial.build_realtime
60
72
  end
61
73
 
62
74
  end
@@ -13,6 +13,10 @@ module Picky
13
13
  true
14
14
  end
15
15
 
16
+ def to_s
17
+ self.class
18
+ end
19
+
16
20
  end
17
21
 
18
22
  end
@@ -87,7 +87,7 @@ module Picky
87
87
  #
88
88
  def source some_source = nil, &block
89
89
  some_source ||= block
90
- some_source ? define_source(some_source) : (@source && extract_source || warn_no_source)
90
+ some_source ? define_source(some_source) : (@source && extract_source)
91
91
  end
92
92
  # Extract the actual source if it is wrapped in a time
93
93
  # capsule, i.e. a block/lambda.
@@ -101,9 +101,6 @@ module Picky
101
101
  check_source source
102
102
  @source = source
103
103
  end
104
- def warn_no_source
105
- warn "No source given for index #{name}."
106
- end
107
104
  def check_source source # :nodoc:
108
105
  raise ArgumentError.new(<<-SOURCE
109
106
 
@@ -4,15 +4,25 @@ module Picky
4
4
  #
5
5
  class Index
6
6
 
7
- # TODO Rake troubles?
8
- #
9
- delegate :remove,
10
- :add,
11
- :replace,
12
- :clear_realtime_mapping,
7
+ delegate :remove, # aka "delete".
8
+ :add, # aka "insert".
9
+ :replace, # aka "insert or update".
10
+ :clear_realtime,
13
11
  :build_realtime_mapping,
14
12
  :to => :categories
15
13
 
14
+ # Add at the end.
15
+ #
16
+ def << thing
17
+ add thing, __method__
18
+ end
19
+
20
+ # Add at the beginning (calls add).
21
+ #
22
+ def unshift thing
23
+ add thing, __method__
24
+ end
25
+
16
26
  end
17
27
 
18
28
  end
@@ -10,7 +10,8 @@ module Picky
10
10
 
11
11
  attr_reader :index_or_category
12
12
 
13
- delegate :source, :to => :index_or_category
13
+ delegate :source,
14
+ :to => :index_or_category
14
15
 
15
16
  def initialize index_or_category
16
17
  @index_or_category = index_or_category
@@ -19,6 +20,7 @@ module Picky
19
20
  # Starts the indexing process.
20
21
  #
21
22
  def index categories
23
+ check_source
22
24
  categories.empty
23
25
  process categories do |file|
24
26
  notify_finished file
@@ -26,6 +28,10 @@ module Picky
26
28
  categories.cache
27
29
  end
28
30
 
31
+ def check_source # :nodoc:
32
+ raise "Trying to index without a source for #{@index_or_category.name}." unless source
33
+ end
34
+
29
35
  def notify_finished file
30
36
  timed_exclaim %Q{"#{@index_or_category.identifier}": Tokenized -> #{file.path.gsub("#{PICKY_ROOT}/", '')}.}
31
37
  end
data/lib/picky/indexes.rb CHANGED
@@ -33,6 +33,7 @@ module Picky
33
33
  end
34
34
 
35
35
  instance_delegate :clear,
36
+ :clear_indexes,
36
37
  :register,
37
38
  :reindex,
38
39
  :[],
data/lib/picky/loader.rb CHANGED
@@ -112,12 +112,6 @@ module Picky
112
112
  load_relative 'backends/helpers/file'
113
113
  load_relative 'backends/backend'
114
114
 
115
- load_relative 'backends/redis'
116
- load_relative 'backends/redis/basic'
117
- load_relative 'backends/redis/list'
118
- load_relative 'backends/redis/string'
119
- load_relative 'backends/redis/float'
120
-
121
115
  load_relative 'backends/memory'
122
116
  load_relative 'backends/memory/basic'
123
117
  load_relative 'backends/memory/text'
@@ -128,8 +122,18 @@ module Picky
128
122
  load_relative 'backends/file/basic'
129
123
  load_relative 'backends/file/json'
130
124
 
125
+ load_relative 'backends/redis'
126
+ load_relative 'backends/redis/directly_manipulable'
127
+ load_relative 'backends/redis/basic'
128
+ load_relative 'backends/redis/list'
129
+ load_relative 'backends/redis/string'
130
+ load_relative 'backends/redis/float'
131
+
131
132
  load_relative 'backends/sqlite'
132
- load_relative 'backends/sqlite/db'
133
+ load_relative 'backends/sqlite/directly_manipulable'
134
+ load_relative 'backends/sqlite/basic'
135
+ load_relative 'backends/sqlite/array'
136
+ load_relative 'backends/sqlite/value'
133
137
 
134
138
  # Indexing and Indexed things.
135
139
  #
@@ -18,7 +18,7 @@ module Picky
18
18
  def initialize index, combinations
19
19
  @combinations = combinations
20
20
  @result_identifier = index.result_identifier # TODO Make cleverer.
21
- @backend = index.backend # TODO Make cleverer.
21
+ @backend = index.backend # TODO Make cleverer. Use inverted?
22
22
  end
23
23
 
24
24
  def hash
@@ -54,7 +54,7 @@ module Picky
54
54
 
55
55
  # Returns a number of prepared (sorted, reduced etc.) allocations for the given tokens.
56
56
  #
57
- def prepared_allocations_for tokens, weights = {}
57
+ def prepared_allocations_for tokens, weights = {}, amount = nil
58
58
  allocations = allocations_for tokens
59
59
 
60
60
  # Remove double allocations.
@@ -72,7 +72,7 @@ module Picky
72
72
 
73
73
  # Reduce the amount of allocations.
74
74
  #
75
- # allocations.reduce_to some_amount
75
+ allocations.reduce_to amount if amount
76
76
 
77
77
  # Remove categories from allocations.
78
78
  #
@@ -132,7 +132,7 @@ module Picky
132
132
  # (We avoid a StopIteration exception. Which of both is less evil?)
133
133
  #
134
134
  def generate_similarity_for bundle
135
- bundle.similar(@text).dup || []
135
+ bundle.similar(@text) || []
136
136
  end
137
137
 
138
138
  # Splits text into a qualifier and text.
data/lib/picky/search.rb CHANGED
@@ -49,6 +49,8 @@ module Picky
49
49
  self
50
50
  end
51
51
 
52
+ # Defines tokenizer options or the tokenizer itself.
53
+ #
52
54
  # Examples:
53
55
  # search = Search.new(index1, index2, index3) do
54
56
  # searching removes_characters: /[^a-z]/,
@@ -67,6 +69,17 @@ module Picky
67
69
  end
68
70
  end
69
71
 
72
+ # Sets the max amount of allocations to calculate.
73
+ #
74
+ # Examples:
75
+ # search = Search.new(index1, index2, index3) do
76
+ # max_allocations 10
77
+ # end
78
+ #
79
+ def max_allocations amount = nil
80
+ amount ? @max_allocations = amount : @max_allocations
81
+ end
82
+
70
83
  # Examples:
71
84
  # search = Search.new(books_index, dvd_index, mp3_index) do
72
85
  # boost [:author, :title] => +3,
@@ -161,7 +174,7 @@ module Picky
161
174
  # Note: Internal method, use #search to search.
162
175
  #
163
176
  def execute tokens, ids, offset, original_text = nil
164
- Results.from original_text, ids, offset, sorted_allocations(tokens)
177
+ Results.from original_text, ids, offset, sorted_allocations(tokens, @max_allocations)
165
178
  end
166
179
 
167
180
  # Delegates the tokenizing to the query tokenizer.
@@ -182,19 +195,18 @@ module Picky
182
195
 
183
196
  # Gets sorted allocations for the tokens.
184
197
  #
185
- def sorted_allocations tokens # :nodoc:
186
- indexes.prepared_allocations_for tokens, weights
198
+ def sorted_allocations tokens, amount = nil # :nodoc:
199
+ indexes.prepared_allocations_for tokens, weights, amount
187
200
  end
188
201
 
189
202
  # Display some nice information for the user.
190
203
  #
191
204
  def to_s
192
205
  s = "#{self.class}("
193
- unless @indexes.indexes.empty?
194
- s << @indexes.indexes.map(&:name).join(', ')
195
- s << ", "
196
- end
197
- s << "weights: #{@weights}"
206
+ ary = []
207
+ ary << @indexes.indexes.map(&:name).join(', ') unless @indexes.indexes.empty?
208
+ ary << "weights: #{@weights}" if @weights
209
+ s << ary.join(', ')
198
210
  s << ")"
199
211
  s
200
212
  end