带轮胎的弹性搜索:具有多个单词的edgeNgram [英] Elasticsearch with Tire: edgeNgram with multiple words

查看:129
本文介绍了带轮胎的弹性搜索:具有多个单词的edgeNgram的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有5部电影:


  • Sans Soleil

  • Sansa

  • 所以这样

  • Sol Goode

  • 独身幸存者

  • 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>所以这个,Sol Goode,唯一幸存者>
  • So Is>所以这样

  • Sol> Sol Goode,Sole Survivor,Sans Soleil

  • "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

查询在我的search_controller中处理:

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:


  • 所以返回Sansa,Sans Soleil,所以是这样的顺序。

  • So is返回Sol Goode,Sans Soleil,Sole Survivor,So Is This / li>
  • "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.

推荐答案

我想你可能会用 match 查询设置为类型: phrase_prefix。大部分但并非全部的例子都可以起作用。

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.

使用Ngrams,您对该过程有更好的控制,但是它们有一个相当大的召回(他们通常会返回更多的数据,然后你想要),你必须打它。这是您使用多个查询条款(Sans so)观察到的奇怪行为,因为它们被有效地执行为 Sans OR so 查询。

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.

尝试使用 default_operator:AND选项(请参阅轮胎的 query_string_test.rb ),或者说 match 查询(请参阅轮胎的 match_query_test.rb )与运算符:AND选项。

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.

有一些关于自动填充,轮胎和Ngram的文章可用:

There are some articles about autocomplete, Tire and Ngrams available:

  • http://dev.af83.com/2012/01/19/autocomplete-with-tire.html
  • http://masonoise.wordpress.com/2012/08/11/elasticsearch-with-rails-and-tire/
  • http://euphonious-intuition.com/2012/08/more-complicated-mapping-in-elasticsearch/

这篇关于带轮胎的弹性搜索:具有多个单词的edgeNgram的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆