Skip to content

Commit a086ded

Browse files
committed
[MODEL] Expanded the example for indexing and searching ActiveRecord associations
The example did a good job for demonstrating how to set up relationships between models and how to index the "core" model, but it didn't really demonstrated the difference between the `records` and `results` of the response. Related: #586
1 parent f8df53a commit a086ded

File tree

1 file changed

+34
-9
lines changed

1 file changed

+34
-9
lines changed

elasticsearch-model/examples/activerecord_associations.rb

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
1313

1414
require 'pry'
15-
Pry.config.history.file = File.expand_path('../../tmp/elasticsearch_development.pry', __FILE__)
1615

1716
require 'logger'
1817
require 'ansi/core'
1918
require 'active_record'
2019

20+
require 'json'
2121
require 'elasticsearch/model'
2222

2323
ActiveRecord::Base.logger = ActiveSupport::Logger.new(STDOUT)
@@ -63,7 +63,7 @@
6363
# ----- Elasticsearch client setup ----------------------------------------------------------------
6464

6565
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" }
6767

6868
# ----- Search integration ------------------------------------------------------------------------
6969

@@ -161,18 +161,43 @@ class Comment < ActiveRecord::Base
161161

162162
Elasticsearch::Model.client.indices.refresh index: Elasticsearch::Model::Registry.all.map(&:index_name)
163163

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+
""
165170

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+
""
167175

168-
# Load model
176+
# Difference between `records` and `results`
169177
#
170-
article = Article.all.includes(:categories, :authors, :comments).first
178+
response = Article.search query: { match: { title: 'first' } }
171179

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+
""
173188

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 ---------------------------------------------------------------------------------------
175200

176201
Pry.start(binding, prompt: lambda { |obj, nest_level, _| '> ' },
177-
input: StringIO.new("article.as_indexed_json\n"),
202+
input: StringIO.new('response.records.first'),
178203
quiet: true)

0 commit comments

Comments
 (0)