Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Suggestions: Small edits to the relevant chapter in `tire-dsl.rb` doc…

…umentation

Closes #916
Closes #894
Closes #857
Closes #777
  • Loading branch information...
commit 2be700e5a60ce92125d52a0c8eac69ca0e504a8a 1 parent 37eb80f
@karmi authored
Showing with 39 additions and 31 deletions.
  1. +39 −31 examples/tire-dsl.rb
View
70 examples/tire-dsl.rb
@@ -821,46 +821,48 @@ def self.search
#
# _Elasticsearch_
# [suggest](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-suggesters.html)
-# feature suggests similar looking terms based on a provided text by using a suggester.
-# You can easily specify either the term or phrase suggester in the Tire dsl.
-# Additionally you can use the completion suggest to get blazing fast completions of user inputs.
+# feature suggests similar terms based on user input.
+# You can specify either the `term` or `phrase` suggester in the Tire DSL, or
+# use the `completion` suggester to get fast completions of user inputs, suitable
+# for auto-complete and instant search features.
-# Suggestion API is available either as standalone call or embedded into search request.
+# Suggestion API is available either as standalone method or part of the search request.
-# To get search suggestions while doing a search just add a call to the suggest api
+# To get search suggestions while doing a search, call the suggest API
#
s = Tire.search 'articles' do
- # To define a suggest using the term suggester, first provide an arbitrary name for the suggest.
+ # To define a suggest using the term suggester, first provide a custom name for the suggest.
#
- suggest :term_suggest1 do
- # Specify the text you want to correct
+ suggest :suggest_title do
+ # Specify the input text.
#
text 'thrree blind mice'
- # Next, specify the suggester field that you want to use and any options.
+ # Then, define the field you want to use for suggestions and any options.
#
term :title, size: 3, sort: 'frequency'
end
- # To define a suggest using the phrase suggest, start with the arbitrary name.
- suggest :phrase_suggest1 do
- # Specify the text you want to correct
+ # To define a suggest using the `phrase` suggest, use a different name.
+ suggest :phrase_suggest_title do
+ # Specify the text input text.
#
text 'thrree blind mice'
- # Then specify the suggester field that you want to use and any options.
+ # Again, define the field you want to use for suggestions and any options.
#
phrase :title, size: 3 do
- # If you want to specify the smoothing model, you can along with any options you want.
+ # Optinally, configure the `smoothing` option...
#
smoothing :stupid_backoff, discount: 0.5
- # You can also specify one or more candidate generators and any options for them.
+ # ...or the `generator` option.
generator :title, min_word_len: 1
end
end
end
-# The results will be available in the suggestions field (which is iterable)
+# The results will be available in the `suggestions` property (which is iterable)
+#
s.results.suggestions.each do |name, options|
puts "Suggestion returned for #{name}:\n"
options.each do |option|
@@ -868,15 +870,16 @@ def self.search
end
end
-# You can also use helper methods available in suggestions results to get only suggested
-# terms or phrases
+# You can also use helper methods available in suggestions results to get only
+# the suggested terms or phrases.
#
-puts "Available corrections for term_suggest1: #{s.results.suggestions.texts(:term_suggest1).join(', ')}"
+puts "Available corrections for suggest_title: #{s.results.suggestions.texts(:suggest_title).join(', ')}"
-# You can use the standalone API to achieve the same result, i.e.
+# You can use the standalone API to achieve the same result:
+#
s = Tire.suggest('articles') do
- # Notice that for standalone api the block method is suggestion rather than suggest
+ # Notice that for standalone API, the block method is `suggestion` rather than `suggest`:
#
suggestion :term_suggest do
text 'thrree'
@@ -892,14 +895,13 @@ def self.search
#### Completion
# In order to use _Elasticsearch_ completion you'll need to update your mappings to provide a field
-# with completion type. [Blog post](http://www.elasticsearch.org/blog/you-complete-me/)
+# with completion type. The example is adapted from this
+# [blog post](http://www.elasticsearch.org/blog/you-complete-me/).
#
-
-# For example given an index named _hotel_
index = Tire.index('hotels') do
delete
- # Notice the type completion for the field _name_suggest_
+ # Notice the type completion for the field _name_suggest_:
#
create :mappings => {
:hotel => {
@@ -911,7 +913,8 @@ def self.search
}
}
- # And some basic documents into this index
+ # Let's add some documents into this index:
+ #
import([
{:id => '1', :type => 'hotel', :name => 'Mercure Hotel Munich', :city => 'Munich', :name_suggest => 'Mercure Hotel Munich'},
{:id => '2', :type => 'hotel', :name => 'Hotel Monaco', :city => 'Munich', :name_suggest => 'Hotel Monaco'},
@@ -920,7 +923,8 @@ def self.search
end
-# We can ask for all hotels starting with a given prefix with this query
+# We can ask for all hotels starting with a given prefix (such as "m") with this query:
+#
s = Tire.suggest('hotels') do
suggestion 'complete' do
text 'm'
@@ -928,7 +932,8 @@ def self.search
end
end
-# And retrieve results as above with the same object
+# And retrieve results as above with the same object:
+#
puts "There are #{s.results.texts.size} hotels starting with m:"
s.results.texts.each do |hotel|
puts "* #{hotel}"
@@ -937,14 +942,16 @@ def self.search
# You can use some advanced features of completion such as multiple inputs and unified output for
# the same document.
-# If you add a document which has inputs and output values for the suggest field
+# If you add a document which has inputs and output values for the suggest field:
+#
index.store({:id => '1', :type => 'hotel', :name => 'Mercure Hotel Munich', :city => 'Munich',
:name_suggest => {:input => ['Mercure Hotel Munich', 'Mercure Munich'], :output => 'Hotel Mercure'}})
index.store({:id => '2', :type => 'hotel', :name => 'Hotel Monaco', :city => 'Munich',
:name_suggest => {:input => ['Monaco Munich', 'Hotel Monaco'], :output => 'Hotel Monaco'}})
index.refresh
-# a completion request with the same input as above
+# ... a completion request with the same input as above ...
+#
s = Tire.suggest('hotels') do
suggestion 'complete' do
text 'm'
@@ -952,7 +959,8 @@ def self.search
end
end
-# will match multiple inputs for the same document and have unified output in results
+# ... will match multiple inputs for the same document and return unified output in results:
+#
puts "There are #{s.results.texts.size} hotels starting with m:"
s.results.texts.each do |hotel|
puts "* #{hotel}"
Please sign in to comment.
Something went wrong with that request. Please try again.