Skip to content

Commit 77ce500

Browse files
committed
[RAILS] Updated the example application to work with Elasticsearch 2.x
Closes elastic#521
1 parent 17016b3 commit 77ce500

File tree

4 files changed

+48
-49
lines changed

4 files changed

+48
-49
lines changed

elasticsearch-rails/lib/rails/templates/03-expert.rb

+4
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@ class Article < ActiveRecord::Base
189189
get 'https://raw.githubusercontent.com/elastic/elasticsearch-rails/master/elasticsearch-rails/lib/rails/templates/indexer.rb',
190190
'app/workers/indexer.rb'
191191

192+
insert_into_file "test/test_helper.rb",
193+
"require 'sidekiq/testing'\n\n",
194+
before: "class ActiveSupport::TestCase\n"
195+
192196
git add: "Gemfile* app/workers/"
193197
git commit: "-m 'Added a Sidekiq indexer\n\nRun:\n\n $ bundle exec sidekiq --queue elasticsearch --verbose\n\nSee http://sidekiq.org'"
194198

elasticsearch-rails/lib/rails/templates/index.html.erb

+11-11
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@
6868
<p class="panel-heading"><%= link_to 'All Sections &rarr;'.html_safe, search_path(params.except(:controller, :action).merge(c: nil))%></p>
6969

7070
<div class="list-group">
71-
<% @articles.response.response['facets']['categories']['terms'].each do |c| %>
71+
<% @articles.response.response['aggregations']['categories']['categories']['buckets'].each do |c| %>
7272
<%=
73-
link_to search_path(params.except(:controller, :action).merge(c: c['term'])),
74-
class: "list-group-item#{' active' if params[:c] == c['term']}" do
75-
c['term'].titleize.html_safe + content_tag(:small, c['count'], class: 'badge').html_safe
73+
link_to search_path(params.except(:controller, :action).merge(c: c['key'])),
74+
class: "list-group-item#{' active' if params[:c] == c['key']}" do
75+
c['key'].titleize.html_safe + content_tag(:small, c['doc_count'], class: 'badge').html_safe
7676
end
7777
%>
7878
<% end %>
@@ -83,11 +83,11 @@
8383
<p class="panel-heading"><%= link_to 'All Authors &rarr;'.html_safe, search_path(params.except(:controller, :action).merge(a: nil))%></p>
8484

8585
<div class="list-group">
86-
<% @articles.response.response['facets']['authors']['terms'].each do |a| %>
86+
<% @articles.response.response['aggregations']['authors']['authors']['buckets'].each do |a| %>
8787
<%=
88-
link_to search_path(params.except(:controller, :action).merge(a: a['term'])),
89-
class: "list-group-item#{' active' if params[:a] == a['term']}" do
90-
a['term'].titleize.html_safe + content_tag(:small, a['count'], class: 'badge').html_safe
88+
link_to search_path(params.except(:controller, :action).merge(a: a['key'])),
89+
class: "list-group-item#{' active' if params[:a] == a['key']}" do
90+
a['key'].titleize.html_safe + content_tag(:small, a['doc_count'], class: 'badge').html_safe
9191
end
9292
%>
9393
<% end %>
@@ -98,16 +98,16 @@
9898
<p class="panel-heading"><%= link_to 'Any Date &rarr;'.html_safe, search_path(params.except(:controller, :action).merge(w: nil))%></p>
9999

100100
<div class="list-group">
101-
<% @articles.response.response['facets']['published']['entries'].each do |w| %>
101+
<% @articles.response.response['aggregations']['published']['published']['buckets'].each do |w| %>
102102
<%=
103-
__start = Time.at(w['time']/1000)
103+
__start = Time.at(w['key']/1000)
104104
__end = __start.end_of_week
105105
__date = __start.to_date.to_s(:iso)
106106

107107
link_to search_path(params.except(:controller, :action).merge(w: __date)),
108108
class: "list-group-item#{' active' if params[:w] == __date}" do
109109
"#{__start.to_date.to_s(:short)} &mdash; #{__end.to_date.to_s(:short)}".html_safe + \
110-
content_tag(:small, w['count'], class: 'badge').html_safe
110+
content_tag(:small, w['doc_count'], class: 'badge').html_safe
111111
end
112112
%>
113113
<% end %>

elasticsearch-rails/lib/rails/templates/search_controller_test.rb

+17-16
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class SearchControllerTest < ActionController::TestCase
3030

3131
Article.find_by_title('Article Three').comments.create body: 'One'
3232

33-
Sidekiq::Queue.new("elasticsearch").clear
33+
Sidekiq::Worker.clear_all
3434

3535
Article.__elasticsearch__.import force: true
3636
Article.__elasticsearch__.refresh_index!
@@ -45,6 +45,7 @@ class SearchControllerTest < ActionController::TestCase
4545
test "should return search results in comments" do
4646
get :index, q: 'one', comments: 'y'
4747
assert_response :success
48+
4849
assert_equal 4, assigns(:articles).size
4950
end
5051

@@ -67,15 +68,15 @@ class SearchControllerTest < ActionController::TestCase
6768
get :index, q: 'one'
6869
assert_response :success
6970

70-
facets = assigns(:articles).response.response['facets']
71+
aggregations = assigns(:articles).response.response['aggregations']
7172

72-
assert_equal 2, facets['categories']['terms'].size
73-
assert_equal 2, facets['authors']['terms'].size
74-
assert_equal 2, facets['published']['entries'].size
73+
assert_equal 2, aggregations['categories']['categories']['buckets'].size
74+
assert_equal 2, aggregations['authors']['authors']['buckets'].size
75+
assert_equal 2, aggregations['published']['published']['buckets'].size
7576

76-
assert_equal 'One', facets['categories']['terms'][0]['term']
77-
assert_equal 'John Smith', facets['authors']['terms'][0]['term']
78-
assert_equal 1425254400000, facets['published']['entries'][0]['time']
77+
assert_equal 'One', aggregations['categories']['categories']['buckets'][0]['key']
78+
assert_equal 'John Smith', aggregations['authors']['authors']['buckets'][0]['key']
79+
assert_equal 1425254400000, aggregations['published']['published']['buckets'][0]['key']
7980
end
8081

8182
test "should sort on the published date" do
@@ -104,13 +105,13 @@ class SearchControllerTest < ActionController::TestCase
104105

105106
assert_equal 2, assigns(:articles).size
106107

107-
facets = assigns(:articles).response.response['facets']
108+
aggregations = assigns(:articles).response.response['aggregations']
108109

109-
assert_equal 1, facets['authors']['terms'].size
110-
assert_equal 1, facets['published']['entries'].size
110+
assert_equal 1, aggregations['authors']['authors']['buckets'].size
111+
assert_equal 1, aggregations['published']['published']['buckets'].size
111112

112113
# Do NOT filter the category facet
113-
assert_equal 2, facets['categories']['terms'].size
114+
assert_equal 2, aggregations['categories']['categories']['buckets'].size
114115
end
115116

116117
test "should filter search results and the category and published date facets when user selects a category" do
@@ -119,12 +120,12 @@ class SearchControllerTest < ActionController::TestCase
119120

120121
assert_equal 1, assigns(:articles).size
121122

122-
facets = assigns(:articles).response.response['facets']
123+
aggregations = assigns(:articles).response.response['aggregations']
123124

124-
assert_equal 1, facets['categories']['terms'].size
125-
assert_equal 1, facets['published']['entries'].size
125+
assert_equal 1, aggregations['categories']['categories']['buckets'].size
126+
assert_equal 1, aggregations['published']['published']['buckets'].size
126127

127128
# Do NOT filter the authors facet
128-
assert_equal 2, facets['authors']['terms'].size
129+
assert_equal 2, aggregations['authors']['authors']['buckets'].size
129130
end
130131
end

elasticsearch-rails/lib/rails/templates/searchable.rb

+16-22
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,14 @@ def as_indexed_json(options={})
7171
#
7272
def self.search(query, options={})
7373

74-
# Prefill and set the filters (top-level `filter` and `facet_filter` elements)
74+
# Prefill and set the filters (top-level `post_filter` and aggregation `filter` elements)
7575
#
7676
__set_filters = lambda do |key, f|
77+
@search_definition[:post_filter][:and] ||= []
78+
@search_definition[:post_filter][:and] |= [f]
7779

78-
@search_definition[:filter][:and] ||= []
79-
@search_definition[:filter][:and] |= [f]
80-
81-
@search_definition[:facets][key.to_sym][:facet_filter][:and] ||= []
82-
@search_definition[:facets][key.to_sym][:facet_filter][:and] |= [f]
80+
@search_definition[:aggregations][key.to_sym][:filter][:bool][:must] ||= []
81+
@search_definition[:aggregations][key.to_sym][:filter][:bool][:must] |= [f]
8382
end
8483

8584
@search_definition = {
@@ -95,27 +94,22 @@ def self.search(query, options={})
9594
}
9695
},
9796

98-
filter: {},
97+
post_filter: {},
9998

100-
facets: {
99+
aggregations: {
101100
categories: {
102-
terms: {
103-
field: 'categories'
104-
},
105-
facet_filter: {}
101+
filter: { bool: { must: [ match_all: {} ] } },
102+
aggregations: { categories: { terms: { field: 'categories' } } }
106103
},
107104
authors: {
108-
terms: {
109-
field: 'authors.full_name.raw'
110-
},
111-
facet_filter: {}
105+
filter: { bool: { must: [ match_all: {} ] } },
106+
aggregations: { authors: { terms: { field: 'authors.full_name.raw' } } }
112107
},
113108
published: {
114-
date_histogram: {
115-
field: 'published_on',
116-
interval: 'week'
117-
},
118-
facet_filter: {}
109+
filter: { bool: { must: [ match_all: {} ] } },
110+
aggregations: {
111+
published: { date_histogram: { field: 'published_on', interval: 'week' } }
112+
}
119113
}
120114
}
121115
}
@@ -174,7 +168,7 @@ def self.search(query, options={})
174168
query: {
175169
multi_match: {
176170
query: query,
177-
fields: ['body'],
171+
fields: ['comments.body'],
178172
operator: 'and'
179173
}
180174
}

0 commit comments

Comments
 (0)