|
| 1 | +[[persistence]] |
| 2 | +== Persistence |
| 3 | + |
| 4 | +The `elasticsearch-persistence` http://rubygems.org/gems/elasticsearch-persistence[Rubygem] |
| 5 | +provides persistence layer for Ruby domain objects. |
| 6 | + |
| 7 | +It supports the repository design patterns. Versions before 6.0 also supported the _active record_ design pattern. |
| 8 | + |
| 9 | +=== Repository |
| 10 | + |
| 11 | +The `Elasticsearch::Persistence::Repository` module provides an implementation of the repository pattern and allows to save, delete, find and search objects stored in Elasticsearch, as well as configure mappings and settings for the index. |
| 12 | + |
| 13 | +==== Features At a Glance |
| 14 | + |
| 15 | +* Access to the Elasticsearch client |
| 16 | +* Setting the index name, document type, and object class for deserialization |
| 17 | +* Composing mappings and settings for the index |
| 18 | +* Creating, deleting or refreshing the index |
| 19 | +* Finding or searching for documents |
| 20 | +* Providing access both to domain objects and hits for search results |
| 21 | +* Providing access to the Elasticsearch response for search results (aggregations, total, ...) |
| 22 | +* Defining the methods for serialization and deserialization |
| 23 | + |
| 24 | +[[persistence]] |
| 25 | +== Persistence |
| 26 | + |
| 27 | +The `elasticsearch-persistence` http://rubygems.org/gems/elasticsearch-persistence[Rubygem] |
| 28 | +provides persistence layer for Ruby domain objects. |
| 29 | + |
| 30 | +It supports the repository design patterns. Versions before 6.0 also supported the _active record_ design pattern. |
| 31 | + |
| 32 | +=== Repository |
| 33 | + |
| 34 | +The `Elasticsearch::Persistence::Repository` module provides an implementation of the repository pattern and allows to save, delete, find and search objects stored in Elasticsearch, as well as configure mappings and settings for the index. |
| 35 | + |
| 36 | +==== Features At a Glance |
| 37 | + |
| 38 | +* Access to the Elasticsearch client |
| 39 | +* Setting the index name, document type, and object class for deserialization |
| 40 | +* Composing mappings and settings for the index |
| 41 | +* Creating, deleting or refreshing the index |
| 42 | +* Finding or searching for documents |
| 43 | +* Providing access both to domain objects and hits for search results |
| 44 | +* Providing access to the Elasticsearch response for search results (aggregations, total, ...) |
| 45 | +* Defining the methods for serialization and deserialization |
| 46 | + |
| 47 | +==== Usage |
| 48 | + |
| 49 | +Let's have a simple plain old Ruby object (PORO): |
| 50 | + |
| 51 | +[source,ruby] |
| 52 | +------------------------------------ |
| 53 | +class Note |
| 54 | + attr_reader :attributes |
| 55 | +
|
| 56 | + def initialize(attributes={}) |
| 57 | + @attributes = attributes |
| 58 | + end |
| 59 | +
|
| 60 | + def to_hash |
| 61 | + @attributes |
| 62 | + end |
| 63 | +end |
| 64 | +------------------------------------ |
| 65 | + |
| 66 | +Let's create a default, "dumb" repository, as a first step: |
| 67 | + |
| 68 | +[source,ruby] |
| 69 | +------------------------------------ |
| 70 | +require 'elasticsearch/persistence' |
| 71 | +class MyRepository; include Elasticsearch::Persistence::Repository; end |
| 72 | +repository = MyRepository.new |
| 73 | +------------------------------------ |
| 74 | + |
| 75 | +We can save a `Note` instance into the repository... |
| 76 | + |
| 77 | +[source,ruby] |
| 78 | +------------------------------------ |
| 79 | +note = Note.new id: 1, text: 'Test' |
| 80 | +
|
| 81 | +repository.save(note) |
| 82 | +# PUT http://localhost:9200/repository/_doc/1 [status:201, request:0.210s, query:n/a] |
| 83 | +# > {"id":1,"text":"Test"} |
| 84 | +# < {"_index":"repository","_type":"note","_id":"1","_version":1,"created":true} |
| 85 | +------------------------------------ |
| 86 | + |
| 87 | +...find it... |
| 88 | + |
| 89 | +[source,ruby] |
| 90 | +------------------------------------ |
| 91 | +n = repository.find(1) |
| 92 | +# GET http://localhost:9200/repository/_doc/1 [status:200, request:0.003s, query:n/a] |
| 93 | +# < {"_index":"repository","_type":"note","_id":"1","_version":2,"found":true, "_source" : {"id":1,"text":"Test"}} |
| 94 | +=> <Note:0x007fcbfc0c4980 @attributes={"id"=>1, "text"=>"Test"}> |
| 95 | +------------------------------------` |
| 96 | +
|
| 97 | +...search for it... |
| 98 | +
|
| 99 | +[source,ruby] |
| 100 | +------------------------------------ |
| 101 | +repository.search(query: { match: { text: 'test' } }).first |
| 102 | +# GET http://localhost:9200/repository/_search [status:200, request:0.005s, query:0.002s] |
| 103 | +# > {"query":{"match":{"text":"test"}}} |
| 104 | +# < {"took":2, ... "hits":{"total":1, ... "hits":[{ ... "_source" : {"id":1,"text":"Test"}}]}} |
| 105 | +=> <Note:0x007fcbfc1c7b70 @attributes={"id"=>1, "text"=>"Test"}> |
| 106 | +------------------------------------ |
| 107 | +
|
| 108 | +...or delete it: |
| 109 | +
|
| 110 | +[source,ruby] |
| 111 | +------------------------------------ |
| 112 | +repository.delete(note) |
| 113 | +# DELETE http://localhost:9200/repository/_doc/1 [status:200, request:0.014s, query:n/a] |
| 114 | +# < {"found":true,"_index":"repository","_type":"note","_id":"1","_version":3} |
| 115 | +=> {"found"=>true, "_index"=>"repository", "_type"=>"note", "_id"=>"1", "_version"=>2} |
| 116 | +------------------------------------ |
| 117 | +
|
| 118 | +The repository module provides a number of features and facilities to configure and customize the behaviour, |
| 119 | +as well as support for extending your own, custom repository class. |
| 120 | +
|
| 121 | +Please refer to the |
| 122 | +https://github.com/elastic/elasticsearch-rails/tree/master/elasticsearch-persistence#the-repository-pattern[documentation] |
| 123 | +for more information. |
| 124 | +
|
| 125 | +Also, check out the |
| 126 | +https://github.com/elastic/elasticsearch-rails/tree/master/elasticsearch-persistence#example-application[example application] which demonstrates the usage patterns of the _repository_ approach to persistence. |
0 commit comments