Slides for my introduction to using Elasticsearch with Ruby/Rails, held at Tokyo Rubyist Meetup in October 2015.
In it I'm giving a short overview on how to use Elasticsearch using the official Elasticsearch Gems with a Ruby on Rails project. Starting with a simple example, expanding to multilanguage, multiobject indexes.
Also I'm shortly discussing integration testing and production hosting.
26. Query DSL
• query: { <query_type>: <arguments> }
• valid arguments depend on query type
• "Filtered Query" takes a query and a filter
• "Simple Query String Query" does not allow
nested queries
28. Query DSL
• filter: { <filter_type>: <arguments> }
• valid arguments depend on filter type
• "And filter" takes an array of filters
• "Range filter" takes a property and lt(e), gt(e)
• "Term filter" takes a property and a value
29. Match Query
Multi Match Query
Bool Query
Boosting Query
Common Terms Query
Constant Score Query
Dis Max Query
Filtered Query
Fuzzy Like This Query
Fuzzy Like This Field Query
Function Score Query
Fuzzy Query
GeoShape Query
Has Child Query
Has Parent Query
Ids Query
Indices Query
Match All Query
More Like This Query
Nested Query
Prefix Query
Query String Query
Simple Query String Query
Range Query
Regexp Query
Span First Query
Span Multi Term Query
Span Near Query
Span Not Query
Span Or Query
Span Term Query
Term Query
Terms Query
Top Children Query
Wildcard Query
Minimum Should Match
Multi Term Query Rewrite
Template Query
30. And Filter
Bool Filter
Exists Filter
Geo Bounding Box Filter
Geo Distance Filter
Geo Distance Range Filter
Geo Polygon Filter
GeoShape Filter
Geohash Cell Filter
Has Child Filter
Has Parent Filter
Ids Filter
Indices Filter
Limit Filter
Match All Filter
Missing Filter
Nested Filter
Not Filter
Or Filter
Prefix Filter
Query Filter
Range Filter
Regexp Filter
Script Filter
Term Filter
Terms Filter
Type Filter
45. Production Ready?
• use elastic.co/found or AWS ES
• use two instances for redundancy
• elasticsearch could go away
• usually only impacts search
• keep impact at a minimum
46. class Event < ActiveRecord::Base
include Elasticsearch::Model
after_save do
IndexerJob.perform_later(
'update', self.class.name, self.id)
end
after_destroy do
IndexerJob.perform_later(
'delete', self.class.name, self.id)
end
...
47. class IndexerJob < ActiveJob::Base
queue_as :default
def perform(action, record_type, record_id)
record_class = record_type.constantize
record_data = {
index: record_class.index_name,
type: record_class.document_type,
id: record_id
}
client = record_class.__elasticsearch__.client
case action.to_s
when 'update'
record = record_class.find(record_id)
client.index record_data.merge(body: record.as_indexed_json)
when 'delete'
client.delete record_data.merge(ignore: 404)
end
end
end
https://gist.github.com/mreinsch/acb2f6c58891e5cd4f13