Skip to content

[MODEL] Add "suggest" method to get response suggest #483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions elasticsearch-model/lib/elasticsearch/model/response.rb
Original file line number Diff line number Diff line change
@@ -71,6 +71,12 @@ def shards
def aggregations
response['aggregations'] ? Hashie::Mash.new(response['aggregations']) : nil
end

# Returns a Hashie::Mash of the suggest
#
def suggest
response['suggest'] ? Hashie::Mash.new(response['suggest']) : nil
end
end
end
end
20 changes: 16 additions & 4 deletions elasticsearch-model/test/integration/active_record_basic_test.rb
Original file line number Diff line number Diff line change
@@ -22,11 +22,19 @@ class ::Article < ActiveRecord::Base

settings index: { number_of_shards: 1, number_of_replicas: 0 } do
mapping do
indexes :title, type: 'string', analyzer: 'snowball'
indexes :body, type: 'string'
indexes :created_at, type: 'date'
indexes :title, type: 'string', analyzer: 'snowball'
indexes :suggest_title, type: 'completion'
indexes :body, type: 'string'
indexes :created_at, type: 'date'
end
end

def as_indexed_json(options = {})
attributes
.symbolize_keys
.slice(:title, :body, :created_at)
.merge(suggest_title: title)
end
end

Article.delete_all
@@ -210,10 +218,14 @@ class ::Article < ActiveRecord::Base

should "allow dot access to response" do
response = Article.search query: { match: { title: { query: 'test' } } },
aggregations: { dates: { date_histogram: { field: 'created_at', interval: 'hour' } } }
aggregations: { dates: { date_histogram: { field: 'created_at', interval: 'hour' } } },
suggest: { title_suggest: { text: 'test', completion: { field: 'suggest_title' } } }

response.response.respond_to?(:aggregations)
assert_equal 2, response.response.aggregations.dates.buckets.first.doc_count

response.response.respond_to?(:suggest)
assert_equal 2, response.response.suggest.title_suggest.first.options.size
end
end

12 changes: 11 additions & 1 deletion elasticsearch-model/test/unit/response_test.rb
Original file line number Diff line number Diff line change
@@ -8,7 +8,8 @@ def self.document_type; 'bar'; end
end

RESPONSE = { 'took' => '5', 'timed_out' => false, '_shards' => {'one' => 'OK'}, 'hits' => { 'hits' => [] },
'aggregations' => {'foo' => {'bar' => 10}}}
'aggregations' => {'foo' => {'bar' => 10}},
'suggest' => {'my_suggest' => []}}

setup do
@search = Elasticsearch::Model::Searching::SearchRequest.new OriginClass, '*'
@@ -73,5 +74,14 @@ def self.document_type; 'bar'; end
assert_kind_of Hashie::Mash, response.aggregations.foo
assert_equal 10, response.aggregations.foo.bar
end

should "access the suggest" do
@search.expects(:execute!).returns(RESPONSE)

response = Elasticsearch::Model::Response::Response.new OriginClass, @search
assert_respond_to response, :suggest
assert_kind_of Hashie::Mash, response.suggest
assert_equal [], response.suggest.my_suggest
end
end
end
2 changes: 1 addition & 1 deletion elasticsearch-rails/lib/rails/templates/index.html.dsl.erb
Original file line number Diff line number Diff line change
@@ -47,7 +47,7 @@
<hr>
</div>

<% if @articles.size < 1 && (suggestions = @articles.response.response['suggest']) && suggestions.present? %>
<% if @articles.size < 1 && @articles.response.suggest.present? %>
<div class="col-md-12">
<p class="alert alert-warning">
No documents have been found.
2 changes: 1 addition & 1 deletion elasticsearch-rails/lib/rails/templates/index.html.erb
Original file line number Diff line number Diff line change
@@ -47,7 +47,7 @@
<hr>
</div>

<% if @articles.size < 1 && (suggestions = @articles.response.response['suggest']) && suggestions.present? %>
<% if @articles.size < 1 && @articles.response.suggest.present? %>
<div class="col-md-12">
<p class="alert alert-warning">
No documents have been found.
Original file line number Diff line number Diff line change
@@ -58,7 +58,7 @@ class SearchControllerTest < ActionController::TestCase
get :index, q: 'one'
assert_response :success

suggestions = assigns(:articles).response.response['suggest']
suggestions = assigns(:articles).response.suggest

assert_equal 'one', suggestions['suggest_title'][0]['text']
end
Original file line number Diff line number Diff line change
@@ -58,7 +58,7 @@ class SearchControllerTest < ActionController::TestCase
get :index, q: 'one'
assert_response :success

suggestions = assigns(:articles).response.response['suggest']
suggestions = assigns(:articles).response.suggest

assert_equal 'one', suggestions['suggest_title'][0]['text']
end