Skip to content

Commit 109c155

Browse files
committed
[MODEL] Updated the "completion suggester" example
Let's use an embedded field instead of a suffixed property for the `suggest` document property. Also added an example of search with suggest, including source filtering. Related: #690
1 parent 1088670 commit 109c155

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

elasticsearch-model/examples/activerecord_mapping_completion.rb

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ class Article < ActiveRecord::Base
1818
include Elasticsearch::Model::Callbacks
1919

2020
mapping do
21-
indexes :title, type: 'text'
22-
indexes :title_suggest, type: 'completion'
21+
indexes :title, type: 'text' do
22+
indexes :suggest, type: 'completion'
23+
end
2324
indexes :url, type: 'keyword'
2425
end
2526

@@ -56,12 +57,28 @@ def as_indexed_json(options={})
5657
body: {
5758
articles: {
5859
text: 'foo',
59-
completion: { field: 'title_suggest', size: 25 }
60+
completion: { field: 'title.suggest' }
6061
}
6162
};
6263

6364
puts "Article suggest:".ansi(:bold),
6465
response_2['articles'].first['options'].map { |d| "#{d['text']} -> #{d['_source']['url']}" }.
6566
inspect.ansi(:bold, :green)
6667

68+
response_3 = Article.search \
69+
query: {
70+
match: { title: 'foo' }
71+
},
72+
suggest: {
73+
articles: {
74+
text: 'foo',
75+
completion: { field: 'title.suggest' }
76+
}
77+
},
78+
_source: ['title', 'url']
79+
80+
puts "Article search with suggest:".ansi(:bold),
81+
response_3.response['suggest']['articles'].first['options'].map { |d| "#{d['text']} -> #{d['_source']['url']}" }.
82+
inspect.ansi(:bold, :blue)
83+
6784
require 'pry'; binding.pry;

0 commit comments

Comments
 (0)