Skip to content

Commit c7a7ad9

Browse files
committed
[MODEL] Refactored the suggestions output wrapper
Refactored the accessor for the `suggest` part of the response introduced in elastic#483 to allow a richer interface. Added a `terms` helper method which just returns a flat array of response.suggestions.terms # => [ 'Foo', 'Bar' ] Related: rubygems/rubygems.org#1135
1 parent 4556f14 commit c7a7ad9

File tree

5 files changed

+42
-11
lines changed

5 files changed

+42
-11
lines changed

elasticsearch-model/lib/elasticsearch/model.rb

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
require 'elasticsearch/model/response/results'
3232
require 'elasticsearch/model/response/records'
3333
require 'elasticsearch/model/response/pagination'
34+
require 'elasticsearch/model/response/suggestions'
3435

3536
require 'elasticsearch/model/ext/active_record'
3637

elasticsearch-model/lib/elasticsearch/model/response.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ def aggregations
7272
response['aggregations'] ? Hashie::Mash.new(response['aggregations']) : nil
7373
end
7474

75-
# Returns a Hashie::Mash of the suggest
75+
# Returns a Hashie::Mash of the suggestions
7676
#
77-
def suggest
78-
response['suggest'] ? Hashie::Mash.new(response['suggest']) : nil
77+
def suggestions
78+
Suggestions.new(response['suggest'])
7979
end
8080
end
8181
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module Elasticsearch
2+
module Model
3+
module Response
4+
5+
class Suggestions < Hashie::Mash
6+
def terms
7+
self.to_a.map { |k,v| v.first['options'] }.flatten.map {|v| v['text']}.uniq
8+
end
9+
end
10+
11+
end
12+
end
13+
end

elasticsearch-model/test/integration/active_record_basic_test.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ class ::Article < ActiveRecord::Base
2323
settings index: { number_of_shards: 1, number_of_replicas: 0 } do
2424
mapping do
2525
indexes :title, type: 'string', analyzer: 'snowball'
26-
indexes :suggest_title, type: 'completion'
2726
indexes :body, type: 'string'
2827
indexes :created_at, type: 'date'
2928
end
@@ -219,13 +218,14 @@ def as_indexed_json(options = {})
219218
should "allow dot access to response" do
220219
response = Article.search query: { match: { title: { query: 'test' } } },
221220
aggregations: { dates: { date_histogram: { field: 'created_at', interval: 'hour' } } },
222-
suggest: { title_suggest: { text: 'test', completion: { field: 'suggest_title' } } }
221+
suggest: { text: 'tezt', title: { term: { field: 'title', suggest_mode: 'always' } } }
223222

224223
response.response.respond_to?(:aggregations)
225-
assert_equal 2, response.response.aggregations.dates.buckets.first.doc_count
224+
assert_equal 2, response.aggregations.dates.buckets.first.doc_count
226225

227226
response.response.respond_to?(:suggest)
228-
assert_equal 2, response.response.suggest.title_suggest.first.options.size
227+
assert_equal 1, response.suggestions.title.first.options.size
228+
assert_equal ['test'], response.suggestions.terms
229229
end
230230
end
231231

elasticsearch-model/test/unit/response_test.rb

+21-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def self.document_type; 'bar'; end
99

1010
RESPONSE = { 'took' => '5', 'timed_out' => false, '_shards' => {'one' => 'OK'}, 'hits' => { 'hits' => [] },
1111
'aggregations' => {'foo' => {'bar' => 10}},
12-
'suggest' => {'my_suggest' => []}}
12+
'suggest' => {'my_suggest' => [ { 'text' => 'foo', 'options' => [ { 'text' => 'Foo', 'score' => 2.0 }, { 'text' => 'Bar', 'score' => 1.0 } ] } ]}}
1313

1414
setup do
1515
@search = Elasticsearch::Model::Searching::SearchRequest.new OriginClass, '*'
@@ -79,9 +79,26 @@ def self.document_type; 'bar'; end
7979
@search.expects(:execute!).returns(RESPONSE)
8080

8181
response = Elasticsearch::Model::Response::Response.new OriginClass, @search
82-
assert_respond_to response, :suggest
83-
assert_kind_of Hashie::Mash, response.suggest
84-
assert_equal [], response.suggest.my_suggest
82+
83+
assert_respond_to response, :suggestions
84+
assert_kind_of Hashie::Mash, response.suggestions
85+
assert_equal 'Foo', response.suggestions.my_suggest.first.options.first.text
86+
end
87+
88+
should "return array of terms from the suggestions" do
89+
@search.expects(:execute!).returns(RESPONSE)
90+
response = Elasticsearch::Model::Response::Response.new OriginClass, @search
91+
92+
assert_not_empty response.suggestions
93+
assert_equal [ 'Foo', 'Bar' ], response.suggestions.terms
94+
end
95+
96+
should "return empty array as suggest terms when there are no suggestions" do
97+
@search.expects(:execute!).returns({})
98+
response = Elasticsearch::Model::Response::Response.new OriginClass, @search
99+
100+
assert_empty response.suggestions
101+
assert_equal [], response.suggestions.terms
85102
end
86103
end
87104
end

0 commit comments

Comments
 (0)