cloud_search 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +34 -24
- data/lib/cloud_search/searcher.rb +18 -2
- data/lib/cloud_search/version.rb +1 -1
- data/spec/cloud_search/searcher_spec.rb +26 -5
- metadata +1 -1
data/README.md
CHANGED
@@ -2,7 +2,8 @@
|
|
2
2
|
|
3
3
|
# CloudSearch
|
4
4
|
|
5
|
-
|
5
|
+
This is a simple Ruby wrapper around the Amazon's CloudSearch API. It has support for searching (with both simple and boolean queries), pagination
|
6
|
+
and documents indexing.
|
6
7
|
|
7
8
|
## Installation
|
8
9
|
|
@@ -22,7 +23,7 @@ Or install it yourself as:
|
|
22
23
|
|
23
24
|
The example bellow uses the Amazon's example database called `imdb-movies`:
|
24
25
|
|
25
|
-
|
26
|
+
### Use your AWS CloudSearch configuration
|
26
27
|
``` ruby
|
27
28
|
CloudSearch.configure do |config|
|
28
29
|
config.domain_id = "pl6u4t3elu7dhsbwaqbsy3y6be"
|
@@ -30,23 +31,31 @@ CloudSearch.configure do |config|
|
|
30
31
|
end
|
31
32
|
```
|
32
33
|
|
33
|
-
|
34
|
+
### Search for 'star wars' on 'imdb-movies'
|
34
35
|
``` ruby
|
35
|
-
|
36
|
-
resp
|
37
|
-
|
38
|
-
|
36
|
+
searcher = CloudSearch::Searcher.new
|
37
|
+
resp = searcher.with_fields(:actor, :director, :title, :year, :text_relevance)
|
38
|
+
.with_query("star wars")
|
39
|
+
.search
|
39
40
|
```
|
40
41
|
|
41
|
-
|
42
|
+
### Or you can search using part of the name
|
42
43
|
``` ruby
|
43
|
-
|
44
|
-
resp
|
45
|
-
|
46
|
-
|
44
|
+
searcher = CloudSearch::Searcher.new
|
45
|
+
resp = searcher.with_fields(:actor, :director, :title, :year, :text_relevance)
|
46
|
+
.with_query("matri*")
|
47
|
+
.search
|
47
48
|
```
|
48
49
|
|
49
|
-
|
50
|
+
### You can also search using boolean queries
|
51
|
+
``` ruby
|
52
|
+
searcher = CloudSearch::Searcher.new
|
53
|
+
resp = searcher.with_fields(:actor, :director, :title, :year, :text_relevance)
|
54
|
+
.with_boolean_query("year:2000")
|
55
|
+
.search
|
56
|
+
```
|
57
|
+
|
58
|
+
## Results
|
50
59
|
``` ruby
|
51
60
|
resp.results.each do |result|
|
52
61
|
movie = result["data"]
|
@@ -66,14 +75,14 @@ end
|
|
66
75
|
## Pagination
|
67
76
|
|
68
77
|
The results you get back are (currently) API-compatible with will\_paginate:
|
69
|
-
|
78
|
+
|
70
79
|
``` ruby
|
71
|
-
|
72
|
-
resp
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
80
|
+
searcher = CloudSearch::Searcher.new
|
81
|
+
resp = searcher.with_fields(:actor, :director, :title, :year, :text_relevance)
|
82
|
+
.with_query("star wars")
|
83
|
+
.with_items_per_page(30)
|
84
|
+
.at_page(10)
|
85
|
+
.search
|
77
86
|
|
78
87
|
resp.total_entries #=> 5000
|
79
88
|
resp.total_pages #=> 167
|
@@ -85,13 +94,14 @@ resp.page_size #=> 30
|
|
85
94
|
## Indexing documents
|
86
95
|
|
87
96
|
``` ruby
|
88
|
-
document = CloudSearch::Document.new :type
|
97
|
+
document = CloudSearch::Document.new :type => "add", # or "delete"
|
89
98
|
:version => 123,
|
90
|
-
:id
|
91
|
-
:
|
99
|
+
:id => 680,
|
100
|
+
:lang => :en,
|
101
|
+
:fields => {:title => "Lord of the Rings"}
|
92
102
|
|
93
103
|
indexer = CloudSearch::Indexer.new
|
94
|
-
indexer << document # add as many documents as you want
|
104
|
+
indexer << document # add as many documents as you want (CloudSearch currently sets a limit of 5MB per documents batch)
|
95
105
|
indexer.index
|
96
106
|
```
|
97
107
|
|
@@ -25,16 +25,26 @@ module CloudSearch
|
|
25
25
|
response.items_per_page = items_per_page
|
26
26
|
response
|
27
27
|
end
|
28
|
-
|
28
|
+
|
29
29
|
def with_query(query)
|
30
30
|
@query = query
|
31
31
|
self
|
32
32
|
end
|
33
33
|
|
34
|
+
def with_boolean_query(query)
|
35
|
+
@boolean = true
|
36
|
+
with_query query
|
37
|
+
self
|
38
|
+
end
|
39
|
+
|
34
40
|
def query
|
35
41
|
@query or ''
|
36
42
|
end
|
37
43
|
|
44
|
+
def boolean_query?
|
45
|
+
!!@boolean
|
46
|
+
end
|
47
|
+
|
38
48
|
def with_fields(*fields)
|
39
49
|
@fields = fields
|
40
50
|
self
|
@@ -65,10 +75,16 @@ module CloudSearch
|
|
65
75
|
|
66
76
|
def url
|
67
77
|
"#{CloudSearch.config.search_url}/search".tap do |u|
|
68
|
-
u.concat("
|
78
|
+
u.concat("?#{query_parameter}=#{CGI.escape(query)}&size=#{items_per_page}&start=#{start}")
|
69
79
|
u.concat("&return-fields=#{CGI.escape(@fields.join(","))}") unless @fields.nil? or @fields.empty?
|
70
80
|
end
|
71
81
|
end
|
82
|
+
|
83
|
+
private
|
84
|
+
|
85
|
+
def query_parameter
|
86
|
+
boolean_query? ? "bq" : "q"
|
87
|
+
end
|
72
88
|
end
|
73
89
|
end
|
74
90
|
|
data/lib/cloud_search/version.rb
CHANGED
@@ -3,6 +3,10 @@ require "spec_helper"
|
|
3
3
|
describe CloudSearch::Searcher do
|
4
4
|
subject { described_class.new }
|
5
5
|
|
6
|
+
let(:url_prefix) do
|
7
|
+
"#{CloudSearch.config.search_url}/search?"
|
8
|
+
end
|
9
|
+
|
6
10
|
describe "#query" do
|
7
11
|
it "returns default query" do
|
8
12
|
subject.query.should == ""
|
@@ -25,6 +29,27 @@ describe CloudSearch::Searcher do
|
|
25
29
|
end
|
26
30
|
end
|
27
31
|
|
32
|
+
describe "#with_boolean_query" do
|
33
|
+
it "sets the query mode to 'boolean'" do
|
34
|
+
subject.with_boolean_query("year:2000")
|
35
|
+
subject.should be_boolean_query
|
36
|
+
end
|
37
|
+
|
38
|
+
it "returns the searcher instance" do
|
39
|
+
subject.with_boolean_query("year:2000").should == subject
|
40
|
+
end
|
41
|
+
|
42
|
+
it "sets the query term" do
|
43
|
+
subject.with_boolean_query("year:2000")
|
44
|
+
subject.query.should == "year:2000"
|
45
|
+
end
|
46
|
+
|
47
|
+
it "uses 'bq' to specify the query in the URL" do
|
48
|
+
subject.with_boolean_query("year:2000")
|
49
|
+
subject.url.should == "#{url_prefix}bq=year%3A2000&size=10&start=0"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
28
53
|
describe "#with_fields" do
|
29
54
|
it "returns #{described_class} instance" do
|
30
55
|
subject.with_fields(:foo).should == subject
|
@@ -42,7 +67,7 @@ describe CloudSearch::Searcher do
|
|
42
67
|
|
43
68
|
it "returns default items per page when it's tried to set nil value" do
|
44
69
|
subject.with_items_per_page(nil)
|
45
|
-
subject.items_per_page.should == 10
|
70
|
+
subject.items_per_page.should == 10
|
46
71
|
end
|
47
72
|
end
|
48
73
|
|
@@ -91,10 +116,6 @@ describe CloudSearch::Searcher do
|
|
91
116
|
end
|
92
117
|
|
93
118
|
describe "#url" do
|
94
|
-
let(:url_prefix) do
|
95
|
-
"#{CloudSearch.config.search_url}/search?"
|
96
|
-
end
|
97
|
-
|
98
119
|
it "returns default cloud search url" do
|
99
120
|
subject.url.should == "#{url_prefix}q=&size=10&start=0"
|
100
121
|
end
|