CloudSearch
This is a simple Ruby wrapper around the Amazon's CloudSearch API. It has support for searching (with both simple and boolean queries), pagination
and documents indexing.
Installation
Add this line to your application's Gemfile:
gem "cloud_search"
And then execute:
$ bundle
Or install it yourself as:
$ gem install cloud_search
Usage
The example bellow uses the Amazon's example database called imdb-movies
:
Use your AWS CloudSearch configuration
CloudSearch.configure do |config|
config.domain_id = "pl6u4t3elu7dhsbwaqbsy3y6be"
config.domain_name = "imdb-movies"
end
Search for 'star wars' on 'imdb-movies'
searcher = CloudSearch::Searcher.new
resp = searcher.with_fields(:actor, :director, :title, :year, :text_relevance)
.with_query("star wars")
.search
Or you can search using part of the name
searcher = CloudSearch::Searcher.new
resp = searcher.with_fields(:actor, :director, :title, :year, :text_relevance)
.with_query("matri*")
.search
You can also search using boolean queries
searcher = CloudSearch::Searcher.new
resp = searcher.with_fields(:actor, :director, :title, :year, :text_relevance)
.as_boolean_query
.with_query("year:2000")
.search
You can use weighted fields in your search
searcher = CloudSearch::Searcher.new
resp = searcher.with_fields(:actor, :director, :title, :year, :text_relevance)
.with_weights(:title => 3, :actor => 2, :default_weight => 1)
.as_boolean_query
.with_query("year:2000")
.search
You can sort the result using a rank expression (previously created on your CloudSearch domain)
searcher = CloudSearch::Searcher.new
resp = searcher.with_fields(:actor, :director, :title, :year, :text_relevance)
.with_query("matrix")
.ranked_with("my_rank_expression")
If you want to rank using descending order, just prepend the expression name with a '-' sign:
resp = searcher.with_fields(:actor, :director, :title, :year, :text_relevance)
.with_query("matrix")
.ranked_with("-my_rank_expression")
Results
resp.results.each do |result|
movie = result["data"]
movie["actor"]
movie["title"]
movie["text_relevance"]
end
The results you get back are (currently) API-compatible with will_paginate:
searcher = CloudSearch::Searcher.new
resp = searcher.with_fields(:actor, :director, :title, :year, :text_relevance)
.with_query("star wars")
.with_items_per_page(30)
.at_page(10)
.search
resp.total_entries
resp.total_pages
resp.current_page
resp.offset
resp.page_size
Indexing documents
document = CloudSearch::Document.new :type => "add",
:version => 123,
:id => 680,
:lang => :en,
:fields => {:title => "Lord of the Rings"}
indexer = CloudSearch::Indexer.new
indexer << document
indexer.index
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request