|
12 | 12 | $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) |
13 | 13 |
|
14 | 14 | require 'pry' |
15 | | -Pry.config.history.file = File.expand_path('../../tmp/elasticsearch_development.pry', __FILE__) |
16 | 15 |
|
17 | 16 | require 'logger' |
18 | 17 | require 'ansi/core' |
19 | 18 | require 'active_record' |
20 | 19 |
|
| 20 | +require 'json' |
21 | 21 | require 'elasticsearch/model' |
22 | 22 |
|
23 | 23 | ActiveRecord::Base.logger = ActiveSupport::Logger.new(STDOUT) |
|
63 | 63 | # ----- Elasticsearch client setup ---------------------------------------------------------------- |
64 | 64 |
|
65 | 65 | Elasticsearch::Model.client = Elasticsearch::Client.new log: true |
66 | | -Elasticsearch::Model.client.transport.logger.formatter = proc { |s, d, p, m| "\e[32m#{m}\n\e[0m" } |
| 66 | +Elasticsearch::Model.client.transport.logger.formatter = proc { |s, d, p, m| "\e[2m#{m}\n\e[0m" } |
67 | 67 |
|
68 | 68 | # ----- Search integration ------------------------------------------------------------------------ |
69 | 69 |
|
@@ -161,18 +161,43 @@ class Comment < ActiveRecord::Base |
161 | 161 |
|
162 | 162 | Elasticsearch::Model.client.indices.refresh index: Elasticsearch::Model::Registry.all.map(&:index_name) |
163 | 163 |
|
164 | | -puts "\n\e[1mArticles containing 'one':\e[0m", Article.search('one').records.to_a.map(&:inspect), "" |
| 164 | +# Search for a term and return records |
| 165 | +# |
| 166 | +puts "", |
| 167 | + "Articles containing 'one':".ansi(:bold), |
| 168 | + Article.search('one').records.to_a.map(&:inspect), |
| 169 | + "" |
165 | 170 |
|
166 | | -puts "\n\e[1mModels containing 'one':\e[0m", Elasticsearch::Model.search('one').records.to_a.map(&:inspect), "" |
| 171 | +puts "", |
| 172 | + "All Models containing 'one':".ansi(:bold), |
| 173 | + Elasticsearch::Model.search('one').records.to_a.map(&:inspect), |
| 174 | + "" |
167 | 175 |
|
168 | | -# Load model |
| 176 | +# Difference between `records` and `results` |
169 | 177 | # |
170 | | -article = Article.all.includes(:categories, :authors, :comments).first |
| 178 | +response = Article.search query: { match: { title: 'first' } } |
171 | 179 |
|
172 | | -# ----- Pry --------------------------------------------------------------------------------------- |
| 180 | +puts "", |
| 181 | + "Search results are wrapped in the <#{response.class}> class", |
| 182 | + "" |
| 183 | + |
| 184 | +puts "", |
| 185 | + "Access the <ActiveRecord> instances with the `#records` method:".ansi(:bold), |
| 186 | + response.records.map { |r| "* #{r.title} | Authors: #{r.authors.map(&:full_name) } | Comment count: #{r.comments.size}" }.join("\n"), |
| 187 | + "" |
173 | 188 |
|
174 | | -puts '', '-'*Pry::Terminal.width! |
| 189 | +puts "", |
| 190 | + "Access the Elasticsearch documents with the `#results` method (without touching the database):".ansi(:bold), |
| 191 | + response.results.map { |r| "* #{r.title} | Authors: #{r.authors.map(&:full_name) } | Comment count: #{r.comments.size}" }.join("\n"), |
| 192 | + "" |
| 193 | + |
| 194 | +puts "", |
| 195 | + "The indexed document:".ansi(:bold), |
| 196 | + JSON.pretty_generate(response.results.first._source.to_hash), |
| 197 | + "" |
| 198 | + |
| 199 | +# ----- Pry --------------------------------------------------------------------------------------- |
175 | 200 |
|
176 | 201 | Pry.start(binding, prompt: lambda { |obj, nest_level, _| '> ' }, |
177 | | - input: StringIO.new("article.as_indexed_json\n"), |
| 202 | + input: StringIO.new('response.records.first'), |
178 | 203 | quiet: true) |
0 commit comments