cloud_search 0.0.2 → 0.0.3
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.
- data/Gemfile +9 -0
- data/README.md +31 -10
- data/cloud_search.gemspec +0 -3
- data/lib/cloud_search/search_response.rb +33 -1
- data/lib/cloud_search/searcher.rb +34 -8
- data/lib/cloud_search/version.rb +1 -1
- data/spec/cloud_search/search_response_spec.rb +42 -0
- data/spec/cloud_search/searcher_spec.rb +114 -1
- data/spec/fixtures/vcr_cassettes/search/request/full.yml +1 -1
- metadata +4 -51
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -22,30 +22,33 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
The example bellow uses the Amazon's example database called `imdb-movies`:
|
24
24
|
|
25
|
-
```ruby
|
26
25
|
# Use your AWS CloudSearch configuration
|
26
|
+
``` ruby
|
27
27
|
CloudSearch.configure do |config|
|
28
28
|
config.domain_id = "pl6u4t3elu7dhsbwaqbsy3y6be"
|
29
29
|
config.domain_name = "imdb-movies"
|
30
30
|
end
|
31
|
+
```
|
31
32
|
|
32
33
|
# Search for 'star wars' on 'imdb-movies'
|
34
|
+
``` ruby
|
33
35
|
search = CloudSearch::Search.new
|
34
36
|
resp = search.with_fields(:actor, :director, :title, :year, :text_relevance)
|
35
|
-
.
|
36
|
-
.
|
37
|
+
.with_query("star wars")
|
38
|
+
.search
|
39
|
+
```
|
37
40
|
|
38
41
|
# Or you can search using part of the name
|
42
|
+
``` ruby
|
39
43
|
search = CloudSearch::Search.new
|
40
44
|
resp = search.with_fields(:actor, :director, :title, :year, :text_relevance)
|
41
|
-
.
|
42
|
-
.
|
43
|
-
|
44
|
-
# Number of results
|
45
|
-
resp.hits
|
45
|
+
.with_query("matri*")
|
46
|
+
.search
|
47
|
+
```
|
46
48
|
|
47
49
|
# Results
|
48
|
-
|
50
|
+
``` ruby
|
51
|
+
resp.results.each do |result|
|
49
52
|
movie = result["data"]
|
50
53
|
|
51
54
|
# List of actors on the movie
|
@@ -60,6 +63,25 @@ res.results.each do |result|
|
|
60
63
|
end
|
61
64
|
```
|
62
65
|
|
66
|
+
## Pagination
|
67
|
+
|
68
|
+
The results you get back are (currently) API-compatible with will\_paginate:
|
69
|
+
|
70
|
+
``` ruby
|
71
|
+
search = CloudSearch::Search.new
|
72
|
+
resp = search.with_fields(:actor, :director, :title, :year, :text_relevance)
|
73
|
+
.with_query("star wars")
|
74
|
+
.with_items_per_page(30)
|
75
|
+
.at_pate(10)
|
76
|
+
.search
|
77
|
+
|
78
|
+
resp.total_entries #=> 5000
|
79
|
+
resp.total_pages #=> 167
|
80
|
+
resp.current_page #=> 10
|
81
|
+
resp.offset #=> 300
|
82
|
+
resp.page_size #=> 30
|
83
|
+
```
|
84
|
+
|
63
85
|
## Indexing documents
|
64
86
|
|
65
87
|
``` ruby
|
@@ -73,7 +95,6 @@ indexer << document # add as many documents as you want
|
|
73
95
|
indexer.index
|
74
96
|
```
|
75
97
|
|
76
|
-
|
77
98
|
## Contributing
|
78
99
|
|
79
100
|
1. Fork it
|
data/cloud_search.gemspec
CHANGED
@@ -24,9 +24,6 @@ Gem::Specification.new do |gem|
|
|
24
24
|
gem.add_development_dependency "simplecov" , "~> 0.6"
|
25
25
|
gem.add_development_dependency "vcr" , "~> 2.2"
|
26
26
|
gem.add_development_dependency "webmock"
|
27
|
-
gem.add_development_dependency "guard"
|
28
|
-
gem.add_development_dependency "guard-rspec"
|
29
|
-
gem.add_development_dependency "rb-fsevent"
|
30
27
|
|
31
28
|
gem.add_dependency "em-http-request" , "~> 1.0"
|
32
29
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module CloudSearch
|
2
2
|
class SearchResponse
|
3
|
-
|
3
|
+
attr_reader :body
|
4
4
|
attr_accessor :http_code
|
5
5
|
|
6
6
|
def results
|
@@ -15,7 +15,39 @@ module CloudSearch
|
|
15
15
|
hits >= 1
|
16
16
|
end
|
17
17
|
|
18
|
+
def body=(body)
|
19
|
+
@body = body
|
20
|
+
calculate_pages
|
21
|
+
@body
|
22
|
+
end
|
23
|
+
|
24
|
+
def items_per_page
|
25
|
+
@items_per_page or 10
|
26
|
+
end
|
27
|
+
|
28
|
+
alias :page_size :items_per_page
|
29
|
+
alias :total_entries :hits
|
30
|
+
|
31
|
+
attr_writer :items_per_page
|
32
|
+
attr_reader :current_page
|
33
|
+
attr_reader :total_pages
|
34
|
+
|
35
|
+
def offset
|
36
|
+
return 0 unless found?
|
37
|
+
(@current_page - 1) * items_per_page
|
38
|
+
end
|
39
|
+
|
18
40
|
private
|
41
|
+
def calculate_pages
|
42
|
+
num_full_pages = hits / items_per_page
|
43
|
+
@total_pages = hits % items_per_page > 0 ? num_full_pages + 1 : num_full_pages
|
44
|
+
@total_pages = 1 if @total_pages == 0
|
45
|
+
|
46
|
+
start = (_hits and _hits["start"]) || 0
|
47
|
+
@current_page = (start / items_per_page) + 1
|
48
|
+
@current_page = @total_pages if @current_page > @total_pages
|
49
|
+
end
|
50
|
+
|
19
51
|
def _hits
|
20
52
|
body and body['hits']
|
21
53
|
end
|
@@ -22,26 +22,52 @@ module CloudSearch
|
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
|
+
response.items_per_page = items_per_page
|
25
26
|
response
|
26
27
|
end
|
27
|
-
|
28
|
-
def query
|
29
|
-
@query =
|
28
|
+
|
29
|
+
def with_query(query)
|
30
|
+
@query = query
|
30
31
|
self
|
31
32
|
end
|
32
33
|
|
34
|
+
def query
|
35
|
+
@query or ''
|
36
|
+
end
|
37
|
+
|
33
38
|
def with_fields(*fields)
|
34
39
|
@fields = fields
|
35
40
|
self
|
36
41
|
end
|
37
42
|
|
38
|
-
|
43
|
+
def with_items_per_page(items_per_page)
|
44
|
+
@items_per_page = items_per_page
|
45
|
+
self
|
46
|
+
end
|
47
|
+
|
48
|
+
def items_per_page
|
49
|
+
@items_per_page or 10
|
50
|
+
end
|
51
|
+
|
52
|
+
def at_page(page)
|
53
|
+
@page_number = (page && page < 1) ? 1 : page
|
54
|
+
self
|
55
|
+
end
|
56
|
+
|
57
|
+
def page_number
|
58
|
+
@page_number or 1
|
59
|
+
end
|
60
|
+
|
61
|
+
def start
|
62
|
+
return 0 if page_number <= 1
|
63
|
+
(items_per_page * page_number) - 1
|
64
|
+
end
|
39
65
|
|
40
66
|
def url
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
67
|
+
"#{CloudSearch.config.search_url}/search".tap do |u|
|
68
|
+
u.concat("?q=#{CGI.escape(query)}&size=#{items_per_page}&start=#{start}")
|
69
|
+
u.concat("&return-fields=#{CGI.escape(@fields.join(","))}") unless @fields.nil? or @fields.empty?
|
70
|
+
end
|
45
71
|
end
|
46
72
|
end
|
47
73
|
end
|
data/lib/cloud_search/version.rb
CHANGED
@@ -27,11 +27,35 @@ describe CloudSearch::SearchResponse do
|
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
|
+
describe "#total_entries" do
|
31
|
+
it "returns same value from hits" do
|
32
|
+
subject.hits.should == subject.total_entries
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
30
36
|
describe "#found?" do
|
31
37
|
it "returns true when found documents" do
|
32
38
|
subject.should be_found
|
33
39
|
end
|
34
40
|
end
|
41
|
+
|
42
|
+
describe "#items_per_page" do
|
43
|
+
it "returns items per page" do
|
44
|
+
subject.items_per_page.should == 10
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe "#page_size" do
|
49
|
+
it "returns number of items per page" do
|
50
|
+
subject.items_per_page.should == subject.items_per_page
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe "#offset" do
|
55
|
+
it "returns offset" do
|
56
|
+
subject.offset.should == 0
|
57
|
+
end
|
58
|
+
end
|
35
59
|
end
|
36
60
|
|
37
61
|
context "when there aren't results" do
|
@@ -56,5 +80,23 @@ describe CloudSearch::SearchResponse do
|
|
56
80
|
subject.should_not be_found
|
57
81
|
end
|
58
82
|
end
|
83
|
+
|
84
|
+
describe "#items_per_page" do
|
85
|
+
it "returns items per page" do
|
86
|
+
subject.items_per_page.should == 10
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
describe "#page_size" do
|
91
|
+
it "returns number of items per page" do
|
92
|
+
subject.items_per_page.should == subject.items_per_page
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
describe "#offset" do
|
97
|
+
it "returns offset" do
|
98
|
+
subject.offset.should == 0
|
99
|
+
end
|
100
|
+
end
|
59
101
|
end
|
60
102
|
end
|
@@ -3,11 +3,124 @@ require "spec_helper"
|
|
3
3
|
describe CloudSearch::Searcher do
|
4
4
|
subject { described_class.new }
|
5
5
|
|
6
|
+
describe "#query" do
|
7
|
+
it "returns default query" do
|
8
|
+
subject.query.should == ""
|
9
|
+
end
|
10
|
+
|
11
|
+
it "returns default query when it's tried to set nil value" do
|
12
|
+
subject.with_query(nil)
|
13
|
+
subject.query.should == ""
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "#with_query" do
|
18
|
+
it "returns #{described_class} instance" do
|
19
|
+
subject.with_query("foo").should == subject
|
20
|
+
end
|
21
|
+
|
22
|
+
it "setup query" do
|
23
|
+
subject.with_query("foo")
|
24
|
+
subject.query.should == "foo"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "#with_fields" do
|
29
|
+
it "returns #{described_class} instance" do
|
30
|
+
subject.with_fields(:foo).should == subject
|
31
|
+
end
|
32
|
+
|
33
|
+
it "setup more thane one value" do
|
34
|
+
subject.with_fields(:foo, :bar, :foobar)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe "#items_per_page" do
|
39
|
+
it "returns default items_per_page" do
|
40
|
+
subject.items_per_page.should == 10
|
41
|
+
end
|
42
|
+
|
43
|
+
it "returns default items per page when it's tried to set nil value" do
|
44
|
+
subject.with_items_per_page(nil)
|
45
|
+
subject.items_per_page.should == 10
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe "#with_items_per_page" do
|
50
|
+
it "returns #{described_class} instance" do
|
51
|
+
subject.with_items_per_page(nil).should == subject
|
52
|
+
end
|
53
|
+
|
54
|
+
it "setup items per page" do
|
55
|
+
subject.with_items_per_page(100)
|
56
|
+
subject.items_per_page.should == 100
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe "#page_number" do
|
61
|
+
it "returns default page number" do
|
62
|
+
subject.page_number.should == 1
|
63
|
+
end
|
64
|
+
|
65
|
+
it "returns default page number when it's tried to set nil value" do
|
66
|
+
subject.at_page(nil)
|
67
|
+
subject.page_number.should == 1
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe "#at_page" do
|
72
|
+
it "returns #{described_class} instance" do
|
73
|
+
subject.at_page(1).should == subject
|
74
|
+
end
|
75
|
+
|
76
|
+
it "setup page number" do
|
77
|
+
subject.at_page(2)
|
78
|
+
subject.page_number.should == 2
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
describe "#start" do
|
83
|
+
it "returns default start index number to search" do
|
84
|
+
subject.start.should == 0
|
85
|
+
end
|
86
|
+
|
87
|
+
it "returns start index 19 for page 2" do
|
88
|
+
subject.at_page(2)
|
89
|
+
subject.start.should == 19
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
describe "#url" do
|
94
|
+
let(:url_prefix) do
|
95
|
+
"#{CloudSearch.config.search_url}/search?"
|
96
|
+
end
|
97
|
+
|
98
|
+
it "returns default cloud search url" do
|
99
|
+
subject.url.should == "#{url_prefix}q=&size=10&start=0"
|
100
|
+
end
|
101
|
+
|
102
|
+
it "returns cloud search url with foo query" do
|
103
|
+
subject.with_query("foo").url.should == "#{url_prefix}q=foo&size=10&start=0"
|
104
|
+
end
|
105
|
+
|
106
|
+
it "returns cloud search url with size equals 20" do
|
107
|
+
subject.with_items_per_page(20).url.should == "#{url_prefix}q=&size=20&start=0"
|
108
|
+
end
|
109
|
+
|
110
|
+
it "returns cloud search url with start at 19" do
|
111
|
+
subject.at_page(2).url.should == "#{url_prefix}q=&size=10&start=19"
|
112
|
+
end
|
113
|
+
|
114
|
+
it "returns cloud search url with foo and bar fields" do
|
115
|
+
subject.with_fields(:foo, :bar).url.should == "#{url_prefix}q=&size=10&start=0&return-fields=foo%2Cbar"
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
6
119
|
describe "#search" do
|
7
120
|
before do
|
8
121
|
subject
|
9
122
|
.with_fields(:actor, :director, :title, :year, :text_relevance)
|
10
|
-
.
|
123
|
+
.with_query("star wars")
|
11
124
|
end
|
12
125
|
|
13
126
|
around { |example| VCR.use_cassette "search/request/full", &example }
|
@@ -2,7 +2,7 @@
|
|
2
2
|
http_interactions:
|
3
3
|
- request:
|
4
4
|
method: get
|
5
|
-
uri: http://search-imdb-movies-pl6u4t3elu7dhsbwaqbsy3y6be.us-east-1.cloudsearch.amazonaws.com/2011-02-01/search?q=star%20wars&return-fields=actor,director,title,year,text_relevance
|
5
|
+
uri: http://search-imdb-movies-pl6u4t3elu7dhsbwaqbsy3y6be.us-east-1.cloudsearch.amazonaws.com/2011-02-01/search?q=star%20wars&return-fields=actor,director,title,year,text_relevance&size=10&start=0
|
6
6
|
body:
|
7
7
|
encoding: US-ASCII
|
8
8
|
string: ''
|
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.0.
|
4
|
+
version: 0.0.3
|
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-10-
|
12
|
+
date: 2012-10-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: pry
|
@@ -107,54 +107,6 @@ dependencies:
|
|
107
107
|
- - ! '>='
|
108
108
|
- !ruby/object:Gem::Version
|
109
109
|
version: '0'
|
110
|
-
- !ruby/object:Gem::Dependency
|
111
|
-
name: guard
|
112
|
-
requirement: !ruby/object:Gem::Requirement
|
113
|
-
none: false
|
114
|
-
requirements:
|
115
|
-
- - ! '>='
|
116
|
-
- !ruby/object:Gem::Version
|
117
|
-
version: '0'
|
118
|
-
type: :development
|
119
|
-
prerelease: false
|
120
|
-
version_requirements: !ruby/object:Gem::Requirement
|
121
|
-
none: false
|
122
|
-
requirements:
|
123
|
-
- - ! '>='
|
124
|
-
- !ruby/object:Gem::Version
|
125
|
-
version: '0'
|
126
|
-
- !ruby/object:Gem::Dependency
|
127
|
-
name: guard-rspec
|
128
|
-
requirement: !ruby/object:Gem::Requirement
|
129
|
-
none: false
|
130
|
-
requirements:
|
131
|
-
- - ! '>='
|
132
|
-
- !ruby/object:Gem::Version
|
133
|
-
version: '0'
|
134
|
-
type: :development
|
135
|
-
prerelease: false
|
136
|
-
version_requirements: !ruby/object:Gem::Requirement
|
137
|
-
none: false
|
138
|
-
requirements:
|
139
|
-
- - ! '>='
|
140
|
-
- !ruby/object:Gem::Version
|
141
|
-
version: '0'
|
142
|
-
- !ruby/object:Gem::Dependency
|
143
|
-
name: rb-fsevent
|
144
|
-
requirement: !ruby/object:Gem::Requirement
|
145
|
-
none: false
|
146
|
-
requirements:
|
147
|
-
- - ! '>='
|
148
|
-
- !ruby/object:Gem::Version
|
149
|
-
version: '0'
|
150
|
-
type: :development
|
151
|
-
prerelease: false
|
152
|
-
version_requirements: !ruby/object:Gem::Requirement
|
153
|
-
none: false
|
154
|
-
requirements:
|
155
|
-
- - ! '>='
|
156
|
-
- !ruby/object:Gem::Version
|
157
|
-
version: '0'
|
158
110
|
- !ruby/object:Gem::Dependency
|
159
111
|
name: em-http-request
|
160
112
|
requirement: !ruby/object:Gem::Requirement
|
@@ -228,7 +180,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
228
180
|
version: '0'
|
229
181
|
requirements: []
|
230
182
|
rubyforge_project:
|
231
|
-
rubygems_version: 1.8.
|
183
|
+
rubygems_version: 1.8.23
|
232
184
|
signing_key:
|
233
185
|
specification_version: 3
|
234
186
|
summary: A wraper to Amazon CloudSearch's API
|
@@ -246,3 +198,4 @@ test_files:
|
|
246
198
|
- spec/fixtures/vcr_cassettes/search/request/full.yml
|
247
199
|
- spec/spec_helper.rb
|
248
200
|
- spec/support/vcr.rb
|
201
|
+
has_rdoc:
|