5959 add_index ( :comments ,  :article_id ) 
6060end 
6161
62+ # ----- Elasticsearch client setup ---------------------------------------------------------------- 
63+ 64+ Elasticsearch ::Model . client  =  Elasticsearch ::Client . new  log : true 
65+ Elasticsearch ::Model . client . transport . logger . formatter  =  proc  {  |s ,  d ,  p ,  m | "\e [32m#{ m } \n \e [0m"  } 
66+ 67+ # ----- Search integration ------------------------------------------------------------------------ 
68+ 69+ module  Searchable 
70+  extend  ActiveSupport ::Concern 
71+ 72+  included  do 
73+  include  Elasticsearch ::Model 
74+  include  Elasticsearch ::Model ::Callbacks 
75+ 76+  include  Indexing 
77+  after_touch ( )  {  __elasticsearch__ . index_document  } 
78+  end 
79+ 80+  module  Indexing 
81+ 82+  # Customize the JSON serialization for Elasticsearch 
83+  def  as_indexed_json ( options = { } ) 
84+  self . as_json ( 
85+  include : {  categories : {  only : :title } , 
86+  authors : {  methods : [ :full_name ] ,  only : [ :full_name ]  } , 
87+  comments : {  only : :text  } 
88+  } ) 
89+  end 
90+  end 
91+ end 
92+ 6293# ----- Model definitions ------------------------------------------------------------------------- 
6394
6495class  Category  < ActiveRecord ::Base 
96+  include  Elasticsearch ::Model 
97+  include  Elasticsearch ::Model ::Callbacks 
98+ 6599 has_and_belongs_to_many  :articles 
66100end 
67101
@@ -81,50 +115,22 @@ class Authorship < ActiveRecord::Base
81115end 
82116
83117class  Article  < ActiveRecord ::Base 
118+  include  Searchable 
119+ 84120 has_and_belongs_to_many  :categories ,  after_add : [  lambda  {  |a , c | a . __elasticsearch__ . index_document  }  ] , 
85121 after_remove : [  lambda  {  |a , c | a . __elasticsearch__ . index_document  }  ] 
86122 has_many  :authorships 
87123 has_many  :authors ,  through : :authorships 
88124 has_many  :comments 
89125end 
90126
91- class  Article  < ActiveRecord ::Base ;  delegate  :size ,  to : :comments ,  prefix : true ;  end 
92- 93127class  Comment  < ActiveRecord ::Base 
94-  belongs_to  :article ,  touch : true 
95- end 
96- 97- # ----- Search integration ------------------------------------------------------------------------ 
98- 99- module  Searchable 
100-  extend  ActiveSupport ::Concern 
101- 102-  included  do 
103-  include  Elasticsearch ::Model 
104-  include  Elasticsearch ::Model ::Callbacks 
105- 106-  __elasticsearch__ . client  =  Elasticsearch ::Client . new  log : true 
107-  __elasticsearch__ . client . transport . logger . formatter  =  proc  {  |s ,  d ,  p ,  m | "\e [32m#{ m } \n \e [0m"  } 
108- 109-  include  Indexing 
110-  after_touch ( )  {  __elasticsearch__ . index_document  } 
111-  end 
112- 113-  module  Indexing 
128+  include  Elasticsearch ::Model 
129+  include  Elasticsearch ::Model ::Callbacks 
114130
115-  # Customize the JSON serialization for Elasticsearch 
116-  def  as_indexed_json ( options = { } ) 
117-  self . as_json ( 
118-  include : {  categories : {  only : :title } , 
119-  authors : {  methods : [ :full_name ] ,  only : [ :full_name ]  } , 
120-  comments : {  only : :text  } 
121-  } ) 
122-  end 
123-  end 
131+  belongs_to  :article ,  touch : true 
124132end 
125133
126- Article . __send__  :include ,  Searchable 
127- 128134# ----- Insert data ------------------------------------------------------------------------------- 
129135
130136# Create category 
@@ -149,14 +155,23 @@ def as_indexed_json(options={})
149155
150156# Add comment 
151157# 
152- article . comments . create  text : 'First comment' 
158+ article . comments . create  text : 'First comment for article One' 
159+ article . comments . create  text : 'Second comment for article One' 
153160
154- # Load 
161+ Elasticsearch ::Model . client . indices . refresh  index : Elasticsearch ::Model ::Registry . all . map ( &:index_name ) 
162+ 163+ puts  "\n \e [1mArticles containing 'one':\e [0m" ,  Article . search ( 'one' ) . records . to_a . map ( &:inspect ) ,  "" 
164+ 165+ puts  "\n \e [1mModels containing 'one':\e [0m" ,  Elasticsearch ::Model . search ( 'one' ) . records . to_a . map ( &:inspect ) ,  "" 
166+ 167+ # Load model 
155168# 
156169article  =  Article . all . includes ( :categories ,  :authors ,  :comments ) . first 
157170
158171# ----- Pry --------------------------------------------------------------------------------------- 
159172
173+ puts  '' ,  '-' *Pry ::Terminal . width! 
174+ 160175Pry . start ( binding ,  prompt : lambda  {  |obj ,  nest_level ,  _ | '> '  } , 
161-  input : StringIO . new ( 'puts "\n\narticle .as_indexed_json\n"; article.as_indexed_json' ) , 
176+  input : StringIO . new ( "article .as_indexed_json\n ") , 
162177 quiet : true ) 
0 commit comments