You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Aug 24, 2021. It is now read-only.
Add to your Gemfile: gem 'cloudsearchable'. Run bundle or: gem install cloudsearchable.
Usage
1. Mix Cloudsearchable into your class
classCustomerincludeCloudsearchableattr_accessor:id,:customer,:name,:lock_version# This is the default index. You probably only need one.index_in_cloudsearchdo |idx|
# Fetch the customer_id field from customerliteral:customer_id,:result_enabled=>true,:search_enabled=>true,:source=>Proc.new{customer}# Map the 'name' Ruby attribute to a field called 'test_name'text:test_name,:result_enabled=>false,:search_enabled=>true,:source=>:name# uint fields can be used in result ranking functionsuint:helpfulness,:result_enabled=>true,:search_enabled=>falsedo;1234endend# A named index.index_in_cloudsearch:test_indexdo |idx|
literal:id,:search_enabled=>trueendend
Customer.search.where(customer_id: 12345)Customer.search.where(customer_id: 12345).order('-helpfulness')# orderingCustomer.search.where(customer_id: 12345).limit(10)# limit, default 100000Customer.search.where(customer_id: 12345).offset(100)# offsetCustomer.search.where(customer_id: 12345).found_count# countCustomer.search.where(customer_id: 12345).where(helpfulness: 42)# query chainCustomer.search.where(customer_id: 12345,helpfulness: 42)# query chain from hashCustomer.search.where(:category,:any,["big","small"])# multiple valuesCustomer.search.where(:customer_id,:!=,1234)# "not equal to" operatorCustomer.search.text('test')# text searchCustomer.search.text('test').where(:featured,:==,'f')# text search with other fieldsCustomer.search.where(:helpfulness,:within_range,0..123)# uint range query, string range works tooCustomer.search.where(:helpfulness,:>,123)# uint greather thanCustomer.search.where(:helpfulness,:>=,123)# uint greather than or equal toCustomer.search.where(:helpfulness,:<,123)# uint less thanCustomer.search.where(:helpfulness,:<=,123)# uint less than or equal to
These queries return a Cloudsearchable::Query, calling .to_a or .found_count will fetch the results
Customer.search.where(customer_id: 12345).each |customer|
p"#{customer.class}: #{customer.name}"end# Customer: foo# Customer: bar
domain_prefix - A name prefix string for your domains in CloudSearch. Defaults to Rails.env, or "" if Rails is undefined.
config.fatal_warnings - raises WarningInQueryResult exception on warning. Defaults to false.
config.logger - a custom logger, defaults to Rails if defined.
ActiveSupport Notifications
Requests to AWS cloudsearch are instrumented using ActiveSupport Notifications. To consume these instrumented events register a subscriber in your Application. For example, to register for getting notifications for search requests:
ActiveSupport::Notifications.subscribe('cloudsearchable.execute_query')do |*args|
event=ActiveSupport::Notifications::Event.new(*args)# Your code here ...end
cloudsearchable.post_record - Instruments record addition
cloudsearchable.delete_record - Instruments record deletion
cloudsearchable.describe_domains - Instruments request for getting domains information
Other Features
Cloudsearchable provides access the underlying AWS client objects, such as '''CloudSearch.client''' and '''class.cloudsearch_domains'''. For example here is how to drop domains associated with Customer class: