|
33 | 33 |
|
34 | 34 | create_table :authors do |t|
|
35 | 35 | t.string :first_name, :last_name
|
| 36 | + t.string :department |
36 | 37 | t.timestamps null: false
|
37 | 38 | end
|
38 | 39 |
|
@@ -84,7 +85,7 @@ module Indexing
|
84 | 85 | def as_indexed_json(options={})
|
85 | 86 | self.as_json(
|
86 | 87 | include: { categories: { only: :title},
|
87 |
| - authors: { methods: [:full_name], only: [:full_name] }, |
| 88 | + authors: { methods: [:full_name, :department], only: [:full_name, :department] }, |
88 | 89 | comments: { only: :text }
|
89 | 90 | })
|
90 | 91 | end
|
@@ -140,7 +141,7 @@ class Comment < ActiveRecord::Base
|
140 | 141 |
|
141 | 142 | # Create author
|
142 | 143 | #
|
143 |
| -author = Author.create first_name: 'John', last_name: 'Smith' |
| 144 | +author = Author.create first_name: 'John', last_name: 'Smith', department: 'Business' |
144 | 145 |
|
145 | 146 | # Create article
|
146 | 147 |
|
@@ -192,7 +193,16 @@ class Comment < ActiveRecord::Base
|
192 | 193 | ""
|
193 | 194 |
|
194 | 195 | puts "",
|
195 |
| - "The indexed document:".ansi(:bold), |
| 196 | + "The whole indexed document (according to `Article#as_indexed_json`):".ansi(:bold), |
| 197 | + JSON.pretty_generate(response.results.first._source.to_hash), |
| 198 | + "" |
| 199 | + |
| 200 | +# Retrieve only selected fields from Elasticsearch |
| 201 | +# |
| 202 | +response = Article.search query: { match: { title: 'first' } }, _source: ['title', 'authors.full_name'] |
| 203 | + |
| 204 | +puts "", |
| 205 | + "Retrieve only selected fields from Elasticsearch:".ansi(:bold), |
196 | 206 | JSON.pretty_generate(response.results.first._source.to_hash),
|
197 | 207 | ""
|
198 | 208 |
|
|
0 commit comments