cloud_search 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +10 -0
- data/lib/cloud_search/searcher.rb +14 -0
- data/lib/cloud_search/version.rb +1 -1
- data/spec/cloud_search/searcher_spec.rb +18 -0
- metadata +4 -4
data/README.md
CHANGED
@@ -56,6 +56,16 @@ resp = searcher.with_fields(:actor, :director, :title, :year, :text_relevanc
|
|
56
56
|
.search
|
57
57
|
```
|
58
58
|
|
59
|
+
### You can use weighted fields in your search
|
60
|
+
``` ruby
|
61
|
+
searcher = CloudSearch::Searcher.new
|
62
|
+
resp = searcher.with_fields(:actor, :director, :title, :year, :text_relevance)
|
63
|
+
.with_weights(:title => 3, :actor => 2, :default_weight => 1)
|
64
|
+
.as_boolean_query
|
65
|
+
.with_query("year:2000")
|
66
|
+
.search
|
67
|
+
```
|
68
|
+
|
59
69
|
## Results
|
60
70
|
``` ruby
|
61
71
|
resp.results.each do |result|
|
@@ -4,6 +4,8 @@ module CloudSearch
|
|
4
4
|
class Searcher
|
5
5
|
include ConfigurationChecking
|
6
6
|
|
7
|
+
attr_reader :weights
|
8
|
+
|
7
9
|
def initialize
|
8
10
|
@response = SearchResponse.new
|
9
11
|
@filters = []
|
@@ -22,6 +24,11 @@ module CloudSearch
|
|
22
24
|
self
|
23
25
|
end
|
24
26
|
|
27
|
+
def with_weights(weights)
|
28
|
+
@weights = weights
|
29
|
+
self
|
30
|
+
end
|
31
|
+
|
25
32
|
def with_filter(filter)
|
26
33
|
@filters << filter
|
27
34
|
self
|
@@ -76,6 +83,7 @@ module CloudSearch
|
|
76
83
|
u.concat("?#{query_parameter}=#{query}&size=#{items_per_page}&start=#{start}")
|
77
84
|
u.concat("&return-fields=#{URI.escape(@fields.join(","))}") if @fields && @fields.any?
|
78
85
|
u.concat("&#{filter_expression}") if @filters.any?
|
86
|
+
u.concat("&#{weighted_fields_expression}") if @weights and !@weights.empty?
|
79
87
|
end
|
80
88
|
end
|
81
89
|
|
@@ -88,6 +96,12 @@ module CloudSearch
|
|
88
96
|
def filter_expression
|
89
97
|
@filters.join("&")
|
90
98
|
end
|
99
|
+
|
100
|
+
def weighted_fields_expression
|
101
|
+
weights_json = JSON.unparse(@weights)
|
102
|
+
expression = "cs.text_relevance(#{weights_json})"
|
103
|
+
URI.escape expression
|
104
|
+
end
|
91
105
|
end
|
92
106
|
end
|
93
107
|
|
data/lib/cloud_search/version.rb
CHANGED
@@ -41,6 +41,24 @@ describe CloudSearch::Searcher do
|
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
44
|
+
describe "#with_weights" do
|
45
|
+
before do
|
46
|
+
subject.with_query("star").with_weights(:title => 3, :description => 1, :default_weight => 0.5)
|
47
|
+
end
|
48
|
+
|
49
|
+
it "adds the informed weighted fields to the searcher object" do
|
50
|
+
subject.weights.should == {:title => 3, :description => 1, :default_weight => 0.5}
|
51
|
+
end
|
52
|
+
|
53
|
+
it "adds the weighted fields to the search url" do
|
54
|
+
subject.url.should == "#{url_prefix}q=star&size=10&start=0&cs.text_relevance(%7B%22title%22:3,%22description%22:1,%22default_weight%22:0.5%7D)"
|
55
|
+
end
|
56
|
+
|
57
|
+
it "returns the searcher object" do
|
58
|
+
subject.with_weights(:title => 3).should == subject
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
44
62
|
describe "#as_boolean_query" do
|
45
63
|
it "sets the query mode to 'boolean'" do
|
46
64
|
subject.as_boolean_query
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cloud_search
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-11-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: pry
|
@@ -177,7 +177,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
177
177
|
version: '0'
|
178
178
|
segments:
|
179
179
|
- 0
|
180
|
-
hash:
|
180
|
+
hash: 1856628248014365830
|
181
181
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
182
182
|
none: false
|
183
183
|
requirements:
|
@@ -186,7 +186,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
186
186
|
version: '0'
|
187
187
|
segments:
|
188
188
|
- 0
|
189
|
-
hash:
|
189
|
+
hash: 1856628248014365830
|
190
190
|
requirements: []
|
191
191
|
rubyforge_project:
|
192
192
|
rubygems_version: 1.8.24
|