Let's say I have 5 film titles:
假设我有5个电影标题:
- Sans Soleil
- Sans苏蕾
- 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
- "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_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.
- "Sans so"返回"Sansa, Sans Soleil, so Is This Is " in that order "。
- "So is" returns "Sol Goode, Sans Soleil, Sole Survivor, So Is This" in that order.
- “所以,”索尔·古德(Sol Goode),桑索尔,唯一的幸存者,也是如此。
2 个解决方案
#1
4
I think you might achieve what you want with the match
query set to type:"phrase_prefix"
. Most, but not all, of your examples would work.
我认为您可以通过将匹配查询设置为:“phrase_prefix”来实现您想要的结果。大多数,但不是全部,你的例子都会起作用。
With Ngrams, you have much finer control over the process, but they have a rather big recall (they usually return more data then you want), and you have to fight it. That's the "strange behaviour" you observe with multiple query terms ("Sans so"), because they are effectively executed as a Sans OR so
query.
使用ngram,您可以更好地控制这个过程,但是他们有很大的回忆(他们通常会返回比您想要的更多的数据),您必须与之斗争。这就是使用多个查询术语(“Sans so”)观察到的“奇怪行为”,因为它们被有效地作为一个san查询执行。
Try using the default_operator: "AND"
option (see Tire's query_string_test.rb), or rather the match
query (see Tire's match_query_test.rb) with the operator: "AND"
option.
尝试使用default_operator:“和”选项(参见Tire的query_string_test.rb),或者更确切地说,使用操作符“和”选项进行匹配查询(参见Tire的match_query_test.rb)。
There are some articles about autocomplete, Tire and Ngrams available:
有一些关于自动补全,轮胎和Ngrams的文章:
- http://dev.af83.com/2012/01/19/autocomplete-with-tire.html
- http://dev.af83.com/2012/01/19/autocomplete-with-tire.html
- http://masonoise.wordpress.com/2012/08/11/elasticsearch-with-rails-and-tire/
- http://masonoise.wordpress.com/2012/08/11/elasticsearch-with-rails-and-tire/
- http://euphonious-intuition.com/2012/08/more-complicated-mapping-in-elasticsearch/
- http://euphonious-intuition.com/2012/08/more-complicated-mapping-in-elasticsearch/
#2
0
Try following
尝试后
search = Tire.search ['books', 'films', 'shows'], :load => true, :page => 1, :per_page => 10 do |s|
s.query do |q|
q.boolean do |b|
b.must {|m| m.string params[:search]}
end
end
end
#1
4
I think you might achieve what you want with the match
query set to type:"phrase_prefix"
. Most, but not all, of your examples would work.
我认为您可以通过将匹配查询设置为:“phrase_prefix”来实现您想要的结果。大多数,但不是全部,你的例子都会起作用。
With Ngrams, you have much finer control over the process, but they have a rather big recall (they usually return more data then you want), and you have to fight it. That's the "strange behaviour" you observe with multiple query terms ("Sans so"), because they are effectively executed as a Sans OR so
query.
使用ngram,您可以更好地控制这个过程,但是他们有很大的回忆(他们通常会返回比您想要的更多的数据),您必须与之斗争。这就是使用多个查询术语(“Sans so”)观察到的“奇怪行为”,因为它们被有效地作为一个san查询执行。
Try using the default_operator: "AND"
option (see Tire's query_string_test.rb), or rather the match
query (see Tire's match_query_test.rb) with the operator: "AND"
option.
尝试使用default_operator:“和”选项(参见Tire的query_string_test.rb),或者更确切地说,使用操作符“和”选项进行匹配查询(参见Tire的match_query_test.rb)。
There are some articles about autocomplete, Tire and Ngrams available:
有一些关于自动补全,轮胎和Ngrams的文章:
- http://dev.af83.com/2012/01/19/autocomplete-with-tire.html
- http://dev.af83.com/2012/01/19/autocomplete-with-tire.html
- http://masonoise.wordpress.com/2012/08/11/elasticsearch-with-rails-and-tire/
- http://masonoise.wordpress.com/2012/08/11/elasticsearch-with-rails-and-tire/
- http://euphonious-intuition.com/2012/08/more-complicated-mapping-in-elasticsearch/
- http://euphonious-intuition.com/2012/08/more-complicated-mapping-in-elasticsearch/
#2
0
Try following
尝试后
search = Tire.search ['books', 'films', 'shows'], :load => true, :page => 1, :per_page => 10 do |s|
s.query do |q|
q.boolean do |b|
b.must {|m| m.string params[:search]}
end
end
end