Skip to content

Persistence implementation: ActiveRecord pattern #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
67ea513
[RAILS] Changed the dependency on "elasticsearch-model" from local to…
karmi May 27, 2014
49eb1cb
Added the "elasticsearch-persistence" gem to the list of subprojects …
karmi May 27, 2014
0577d55
Added the information about `elasticsearch-persistence` to the main R…
karmi May 31, 2014
250eb41
Release 0.1.3
karmi May 31, 2014
f2ca55f
[MODEL] Added, that "raw" response from Elasticsearch is wrapped in H…
karmi Jun 3, 2014
3151027
[STORE] Added the first version of the ActiveRecord-based model persi…
karmi Apr 27, 2014
ed0a552
[STORE] Added a rudimentary support for extracting date/time from Rai…
karmi Apr 28, 2014
ff9e664
[STORE] Added `MyModel.all` finder method
karmi May 5, 2014
0c33d00
[STORE] Added missing test for Elasticsearch::Persistence::Model::Uti…
karmi May 27, 2014
2b74c05
[STORE] Added code annotations across the Persistence::Model codebase
karmi May 27, 2014
cc1c947
[STORE] Added the `find_in_batches` method for models
karmi May 27, 2014
e5546e5
[STORE] Added, that `find_in_batches` returns an Enumerator when the …
karmi May 28, 2014
074930f
[STORE] Added the `find_each` method for models
karmi May 28, 2014
634fbec
[STORE] Added a custom `inspect` methods to model
karmi May 28, 2014
4d6aade
[STORE] Added, that model can be created with a custom ID, and _index…
karmi May 31, 2014
1f74e7b
[STORE] Added a `count` method to the repository and model
karmi Jun 3, 2014
35aa57a
[STORE] Allow accessing the raw hit directly from result
karmi Jun 5, 2014
783cd1a
[STORE] Added an integration test for creating/saving models with cus…
karmi Jun 12, 2014
f01446a
[STORE] Added `Model.create_index!` and `Model.refresh_index!` method…
karmi Jun 16, 2014
1fcc0a2
[STORE] Fixed incorrect handling of Rails' form values for `Date`
karmi Jun 16, 2014
e3a4414
[STORE] Added full readme for the "active record" pattern
karmi Jun 16, 2014
1ec8514
[STORE] Added a Rails ORM generator for Elasticsearch::Persistence::M…
karmi Jun 17, 2014
2a137e0
[STORE] Added the template for generating a full Rails application wi…
karmi Jun 17, 2014
4addcfd
[STORE] Renamed the Sinatra example application folder to "notes", ad…
karmi Jun 18, 2014
5042f3f
[STORE] Added information about accessing the repository from the mod…
karmi Jun 18, 2014
b21e959
[STORE] Added info about installting/using the Persistence::Model wit…
karmi Jun 18, 2014
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Elasticsearch

This repository contains ActiveModel, ActiveRecord and Ruby on Rails integrations for
[Elasticsearch](http://elasticsearch.org):
This repository contains various Ruby and Rails integrations for [Elasticsearch](http://elasticsearch.org):

* ActiveModel integration with adapters for ActiveRecord and Mongoid
* _Repository Pattern_ based persistence layer for Ruby objects
* Enumerable-based wrapper for search results
* ActiveRecord::Relation-based wrapper for returning search results as records
* Convenience model methods such as `search`, `mapping`, `import`, etc
Expand Down Expand Up @@ -41,13 +41,16 @@ or install it from a source code checkout:

## Usage

This project is split into two separate gems:
This project is split into three separate gems:

* [**`elasticsearch-model`**](https://github.com/elasticsearch/elasticsearch-rails/tree/master/elasticsearch-model),
which contains model-related features such as setting up indices, `search` method, pagination, etc
which contains search integration for Ruby/Rails models such as ActiveRecord::Base and Mongoid,

* [**`elasticsearch-persistence`**](https://github.com/elasticsearch/elasticsearch-rails/tree/master/elasticsearch-persistence),
which provides standalone persistence layer for Ruby/Rails objects and models

* [**`elasticsearch-rails`**](https://github.com/elasticsearch/elasticsearch-rails/tree/master/elasticsearch-rails),
which contains features for Ruby on Rails applications
which contains various features for Ruby on Rails applications

Example of a basic integration into an ActiveRecord-based model:

Expand All @@ -64,20 +67,43 @@ Article.import
@articles = Article.search('foobar').records
```

Example of using Elasticsearch as a repository for a Ruby model:

```ruby
require 'virtus'
class Article
include Virtus.model
attribute :title, String
end

require 'elasticsearch/persistence'
repository = Elasticsearch::Persistence::Repository.new

repository.save Article.new(title: 'Test')
# POST http://localhost:9200/repository/article [status:201, request:0.760s, query:n/a]
# => {"_index"=>"repository", "_type"=>"article", "_id"=>"Ak75E0U9Q96T5Y999_39NA", ...}
```

You can generate a fully working Ruby on Rails application with a single command:

```bash
rails new searchapp --skip --skip-bundle --template https://raw.github.com/elasticsearch/elasticsearch-rails/master/elasticsearch-rails/lib/rails/templates/01-basic.rb
```

Please refer to each library documentation for detailed information and examples.
**Please refer to each library documentation for detailed information and examples.**

### Model

* [[README]](https://github.com/elasticsearch/elasticsearch-rails/blob/master/elasticsearch-model/README.md)
* [[Documentation]](http://rubydoc.info/gems/elasticsearch-model/)
* [[Test Suite]](https://github.com/elasticsearch/elasticsearch-rails/blob/master/elasticsearch-model/test)

### Persistence

* [[README]](https://github.com/elasticsearch/elasticsearch-rails/blob/master/elasticsearch-persistence/README.md)
* [[Documentation]](http://rubydoc.info/gems/elasticsearch-persistence/)
* [[Test Suite]](https://github.com/elasticsearch/elasticsearch-rails/blob/master/elasticsearch-persistence/test)

### Rails

* [[README]](https://github.com/elasticsearch/elasticsearch-rails/blob/master/elasticsearch-rails/README.md)
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'pathname'

subprojects = %w| elasticsearch-model elasticsearch-rails |
subprojects = %w| elasticsearch-model elasticsearch-rails elasticsearch-persistence |

__current__ = Pathname( File.expand_path('..', __FILE__) )

Expand Down
2 changes: 2 additions & 0 deletions elasticsearch-model/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
## 0.1.3

## 0.1.2

* Properly delegate existence methods like `result.foo?` to `result._source.foo`
Expand Down
4 changes: 3 additions & 1 deletion elasticsearch-model/lib/elasticsearch/model/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ def initialize(klass, search, options={})
# @return [Hash]
#
def response
@response ||= search.execute!
@response ||= begin
Hashie::Mash.new(search.execute!)
end
end

# Returns the collection of "hits" from Elasticsearch
Expand Down
2 changes: 1 addition & 1 deletion elasticsearch-model/lib/elasticsearch/model/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Elasticsearch
module Model
VERSION = "0.1.2"
VERSION = "0.1.3"
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,14 @@ class ::Article < ActiveRecord::Base
assert_equal 'Testing Coding', response.records.order('title DESC').first.title
end
end

should "allow dot access to response" do
response = Article.search query: { match: { title: { query: 'test' } } },
aggregations: { dates: { date_histogram: { field: 'created_at', interval: 'hour' } } }

response.response.respond_to?(:aggregations)
assert_equal 2, response.response.aggregations.dates.buckets.first.doc_count
end
end

end
Expand Down
10 changes: 10 additions & 0 deletions elasticsearch-model/test/unit/response_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ def self.document_type; 'bar'; end
assert_equal 'OK', response.shards.one
end

should "wrap the raw Hash response in Hashie::Mash" do
@search = Elasticsearch::Model::Searching::SearchRequest.new OriginClass, '*'
@search.stubs(:execute!).returns({'hits' => { 'hits' => [] }, 'aggregations' => { 'dates' => 'FOO' }})

response = Elasticsearch::Model::Response::Response.new OriginClass, @search

assert_respond_to response.response, :aggregations
assert_equal 'FOO', response.response.aggregations.dates
end

should "load and access the results" do
@search.expects(:execute!).returns(RESPONSE)

Expand Down
7 changes: 7 additions & 0 deletions elasticsearch-persistence/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## 0.1.3

* Released the "elasticsearch-persistence" Rubygem

## 0.0.1

* Initial infrastructure for the gem
Loading