Elasticsearch Wrapper, with Query & Mapping Builders

HOW TO instantiate a repository

repository = Elasticated::Repository.new # pointing to localhost
repository = Elasticated::Repository.new host: 'http://user:[email protected]:9200' # pointing to some secured server
repository = Elasticated::Repository.new host: 'myhost', index: 'myindex', type: 'mytype' # pointing to some type of some index

HOW TO build a query

query = Query.build do
  filter do
    equal :first_name, 'Pablo'
  conditions do
    between :age, 20, 25
    must_not do
      wildcard :second_name, 'Santiago*'
    should do
      gt :age, 23
      lt :age, 22
    minimum_should_match 1
  post do
    equal :city, 'CABA'
  aggregations do
    group :register_number, size: 10
  from 5
  size 15
query.build # see the result

HOW TO execute a search

repository = Repository.new
query = Query.new
repository.execute_count query
repository.execute_search query
repository.execute_aggregations query
repository.execute_aggregated_search query
repository.delete_by query
repository.exists? query

HOW TO build a document and index it

document = Document.create do |doc|
  doc.id = 'my_unique_id'
  doc.index = 'myindex'
  doc.type = 'mytype'
  doc.source = { user: 'Pablo', some_field: 'some_value' }
repository = Repository.new
repository.index_document document

HOW TO build a document and (partially) update it

repository = Repository.new
repository.update_document 'my_unique_id', type: 'mytype', index: 'myindex', source: { new_field: 'New data' }

HOW TO get a document (or multiple documents) by id

repository = Repository.new
document = repository.get_document 'my_unique_id', type: 'mytype', index: 'myindex'
documents = repository.get_documents ['id1', 'id2', 'id3'], type: 'mytype', index: 'myindex'

HOW TO delete a document by id

repository = Repository.new
repository.delete_document 'my_unique_id', type: 'mytype', index: 'myindex'

HOW TO execute a bulk request

repository = Repository.new
document = Document.create id: 'my_id', type: 'my_type', index: 'my_index', source: { name: 'Pablo' }
bulk_request = repository.prepare_bulk do
  index_document d
  delete_document 'my_id', type: 'my_type', index: 'my_index'
  upsert_document 'my_id', type: 'my_type', index: 'my_index', source: { name: 'Santiago' }
  update_document 'my_id', type: 'my_type', index: 'my_index', source: { age: 24 }
  create_document d # this one will return error individually
response = repository.execute_bulk bulk_request

HOW TO start a resumable scroll

repository = Elasticated::Repository.new
query = Elasticated::Query.new
# prepare the 'search' object
search = repository.prepare_search query, index: 'my_index', type: 'my_type'
# fetch the first page of results
results = search.start
# get the scroll_id, and use it again later
scroll_id = search.scroll_id # also results.scroll_id is valid

HOW TO resume a scroll

repository = Elasticated::Repository.new
scroll_id = '...' # the scroll_id returned by the 'search' object
# prepare the 'search' object
search = repository.restore_search scroll_id
# fetch the next page of results
results = search.fetch
# fetch all pages until the search ends
results.append search.fetch until search.completed?

HOW TO build a mapping

mapping = Elasticated::Mapping.build do
  type :content do
    date :date
    string :user
    analyzed_string :user_alias
    nested :user_purchases do
      long :purchase_id
      string :items
    object :user_info do
      string :address

HOW TO configure the gem

Elasticated.configure do |config|
  config.logger = Elasticated::Loggers::DefaultLogger.new
  config.scroll_expiration_time = '3m'
  config.scroll_page_size = 500
  config.search_page_size = 1000
  config.transport_options.request_timeout = 5*60

HOW TO configure some specific repository

repository = Repository.new
repository.logger = Elasticated::Loggers::DefaultLogger.new
repository.scroll_expiration_time = '3m'
repository.scroll_page_size = 500
repository.search_page_size = 1000
# transport_options cannot be setted for a single repository