Skip to content

Commit 0a811a9

Browse files
miguelffkarmi
authored andcommitted
[MODEL] Update README.md with multi-model usage examples
Related: elastic#10, elastic#30, elastic#50, elastic#129, elastic#346
1 parent 9dd5ba3 commit 0a811a9

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

elasticsearch-model/README.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,12 @@ response.results.first._source.title
172172
# => "Quick brown fox"
173173
```
174174

175+
Or if we want to perform the search across different models:
176+
177+
```ruby
178+
Elasticsearch::Model.search 'fox dogs', [Article, Comment]
179+
```
180+
175181
#### Search results
176182

177183
The returned `response` object is a rich wrapper around the JSON returned from Elasticsearch,
@@ -216,7 +222,17 @@ response.records.to_a
216222
```
217223

218224
The returned object is the genuine collection of model instances returned by your database,
219-
i.e. `ActiveRecord::Relation` for ActiveRecord, or `Mongoid::Criteria` in case of MongoDB. This allows you to
225+
i.e. `ActiveRecord::Relation` for ActiveRecord, or `Mongoid::Criteria` in case of MongoDB.
226+
When the search is performed across different models, an Array of model instances is returned.
227+
228+
```ruby
229+
Elasticsearch::Model.search('fox', [Article, Comment]).records
230+
# Article Load (0.3ms) SELECT "articles".* FROM "articles" WHERE "articles"."id" IN (1)
231+
# Comment Load (0.2ms) SELECT "comments".* FROM "comments" WHERE "comments"."id" IN (1,5)
232+
# => [#<Article id: 1, title: "Quick brown fox">, #<Comment id: 1, body: "I like foxes">, #<Comment id: 5, body: "Michael J.Fox is my favorite actor">]
233+
```
234+
235+
In the case of searching a single model, this allows you to
220236
chain other methods on top of search results, as you would normally do:
221237

222238
```ruby

0 commit comments

Comments
 (0)