0.0
No commit activity in last 3 years
No release in over 3 years
Repository like access to elasticseach indices
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.7
~> 3.0.1
~> 10.0
~> 3.1.0
~> 2.9.3
~> 1.20.4
~> 0.8.7.6

Runtime

~> 1.0.12
 Project Readme

Build Status Code Climate

ElasticAdapter

This gem provides an implementation of the repository pattern. It is a result of some frustration I had with the elasticsearch-persistence gem. After reading Hashie Considered Harmful and some issues I had with overriding methods on a subclassed Repository I decided to give it a own try.

Installation

Add this line to your application's Gemfile:

gem 'elastic_adapter'

And then execute:

$ bundle

Or install it yourself as:

$ gem install elastic_adapter

Documentation

Documentation can be found here

Usage

First define the document type. Initialize it by passing a name and some mappings to the constructor:

mappings = {
  product: {
    properties: {
      name: {
        type: "string",
        index_analyzer: "simple",
        search_analyzer: "simple"
      },
      name_suggest: {
        type: "completion"
      },
      price: {
        type: "float",
        index: "not_analyzed"
      }
    }
  }
}

document_type = ElasticAdapter::DocumentType.new("product", mappings)

Next define the index settings and instantiate the index:

settings = { number_of_shards: 1 }


index = ElasticAdapter::Index.new(
  name: "product_index",
  url: "http://localhost:9200",
  log: true,
  settings: settings,
  document_type: document_type
)

Now you can perform actions like create the index, index documents or search for them. For a full list of feaures look into the Documentation.

# Creating an Index

response = index.create_index
response.inspect # => "{:acknowledged=>true}"
response.class # => ElasticAdapter::Response
response.success? # => true

# Add a document to the index

doc = {
  id: 1,
  name_name: "foo",
  suggest: "foo",
  price: 11.12
}

response = index.index(doc)
response.inspect # => "{:index=>\"product_index\", :type=>\"product\", :id=>\"1\", :version=>1, :created=>true}"
response.class # => ElasticAdapter::Response
response.success? # => true

# Search for documents

query = {query: {match: {name: "foo"}}}
response = index.search(query)
response.inspect # => "{:count=>1, :hits=>[{:id=>\"1\", :name=>\"foo\", :name_suggest=>\"foo\", :price=>11.12}]}"
response.class # => ElasticAdapter::Decoration::SearchResponse

Fore more usage examples look here

Testing and Development

I am using VCR to record the requests to elasticsearch and play them back while testing. In some cases it might be necessary to rerecord the requests. Because elasticsearch is a little slow and doesn't return documents for a search request that just have been indexed there are some sleep statements in the spec. To not slow down the tests those sleep statements are just executed if a RECORDING environment variable is set. I added a rake task that sets the environment variable deletes the cassetts and runs all specs with :vcr. Run rake record to rerecord the cassettes.

Contributing

  1. Fork it ( https://github.com/kbredemeier/elastic_adapter/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request