picky 0.10.0 → 0.10.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,14 +10,20 @@ module Query
10
10
  attr_writer :tokenizer
11
11
  attr_accessor :reduce_to_amount, :weights
12
12
 
13
- # Run a query on the given text, with the offset and these indexes.
13
+ # Takes:
14
+ # * A number of indexes
15
+ # * Options hash (optional) with:
16
+ # * weigher: A weigher. Query::Weigher by default.
17
+ # * tokenizer: Tokenizers::Query.default by default.
18
+ # * weights: A hash of weights, or a Query::Weights object.
14
19
  #
15
20
  def initialize *index_types
16
21
  options = Hash === index_types.last ? index_types.pop : {}
17
22
  @index_types = index_types
18
23
  @weigher = options[:weigher] || Weigher.new(index_types)
19
24
  @tokenizer = options[:tokenizer] || Tokenizers::Query.default
20
- @weights = options[:weights] || Weights.new
25
+ weights = options[:weights] || Weights.new
26
+ @weights = Hash === weights ? Weights.new(weights) : weights
21
27
  end
22
28
 
23
29
  # Convenience method.
@@ -35,11 +35,13 @@ class PickySearch < Application
35
35
  category(:isbn,
36
36
  partial: Partial::None.new) # Partial substring searching on an ISBN makes not much sense, neither does similarity.
37
37
 
38
- full_books = Query::Full.new books_index # A Full query returns ids, combinations, and counts.
39
- live_books = Query::Live.new books_index # A Live query does return all that Full returns, except ids.
38
+ query_options = { :weights => { [:title, :author] => +3, [:title] => +1 } } # +/- points for ordered combinations.
40
39
 
41
- route %r{\A/books/full\Z} => full_books # Routing is simple: url_path_regexp => query
42
- route %r{\A/books/live\Z} => live_books #
40
+ full_books = Query::Full.new books_index, query_options # A Full query returns ids, combinations, and counts.
41
+ live_books = Query::Live.new books_index, query_options # A Live query does return all that Full returns, except ids.
42
+
43
+ route %r{\A/books/full\Z} => full_books # Routing is simple: url_path_regexp => query
44
+ route %r{\A/books/live\Z} => live_books #
43
45
 
44
46
  # Note: You can pass a query multiple indexes and it will query in all of them.
45
47
 
@@ -3,6 +3,24 @@ require 'spec_helper'
3
3
 
4
4
  describe 'Query::Base' do
5
5
 
6
+ describe "weights handling" do
7
+ it "creates a default weight when no weights are given" do
8
+ query = Query::Base.new
9
+
10
+ query.weights.should be_kind_of(Query::Weights)
11
+ end
12
+ it "handles :weights options when not yet wrapped" do
13
+ query = Query::Base.new :weights => { [:a, :b] => +3 }
14
+
15
+ query.weights.should be_kind_of(Query::Weights)
16
+ end
17
+ it "handles :weights options when already wrapped" do
18
+ query = Query::Base.new :weights => Query::Weights.new([:a, :b] => +3)
19
+
20
+ query.weights.should be_kind_of(Query::Weights)
21
+ end
22
+ end
23
+
6
24
  describe "empty_results" do
7
25
  before(:each) do
8
26
  @query = Query::Full.new
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 10
8
- - 0
9
- version: 0.10.0
8
+ - 1
9
+ version: 0.10.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Florian Hanke
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-10-31 00:00:00 +02:00
17
+ date: 2010-11-02 00:00:00 +01:00
18
18
  default_executable: picky
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency