Let's say I have 5 film titles:
- Sans Soleil
- Sansa
- So Is This
- Sol Goode
- Sole Survivor
I want to implement an auto-complete search field with this expected behavior:
- "Sans" > Sans Soleil, Sansa
- "Sans so" > Sans Soleil
- "So" > So Is This, Sol Goode, Sole Survivor
- "So Is" > So Is This
- "Sol" > Sol Goode, Sole Survivor, Sans Soleil
This use-case seems obvious and must be one utilized by many, but I just can't get it to work properly and I can't seem to find any answer or documentation to help. This is my current model:
class Film < Media
include Tire::Model::Search
include Tire::Model::Callbacks
settings :analysis => {
:filter => {
:title_ngram => {
"type" => "edgeNGram",
"min_gram" => 2,
"max_gram" => 8,
"side" => "front" }
},
:analyzer => {
:title_analyzer => {
"tokenizer" => "lowercase",
"filter" => ["title_ngram"],
"type" => "custom" }
}
} do
mapping do
indexes :title, :type => 'string', :analyzer => 'title_analyzer'
indexes :int_english_title, :type => 'string', :analyzer => 'title_analyzer'
end
end
end
And how the query is handled in my search_controller:
search = Tire.search ['books', 'films', 'shows'], :load => true, :page => 1, :per_page => 10 do |s|
s.query do |query|
query.string "title:#{params[:search]}"
end
end
@results = search.results
This produces some strange behavior:
- "Sans so" returns "Sansa, Sans Soleil, So Is This" in that order.
- "So is" returns "Sol Goode, Sans Soleil, Sole Survivor, So Is This" in that order.