2013-05-13 57 views
1

我正在使用ElasticSearch和Tire执行一些基本的搜索功能,但是雪球干扰分析器的基本配置让我难住了。我几乎从以下GitHub的页面代码示例:https://github.com/karmi/tire使用轮胎库进行干扰

下面是一个Ruby示例文件(红宝石1.9.3,轮胎25年8月1日):

require 'tire' 

Tire.index 'videos' do 
    delete 
    create :mappings => { 
    :video => { 
     :properties => { 
     :code    => { :type => 'string' }, 
     :description   => { :type => 'string', :analyzer => 'snowball' } 
     } 
    } 
} 
end 

videos = [ 
    { :code => '1', :description => "some fight video" }, 
    { :code => '2', :description => "a fighting video" } 
] 

Tire.index 'videos' do 
    import videos 
    refresh 
end 

s = Tire.search 'videos' do 
    query do 
     string 'description:fight' 
    end 
end 

s.results.each do |document| 
    puts "* #{document.code} - #{document.description}" 
end 

我本来期望这产生在比赛中都有记录,因为战斗和战斗具有相同的主干。然而,它只返回的第一条记录:

* 1 - some fight video 

这表明默认分析仪正在使用,而不是一个,我配置。

我知道在查询字符串中传递实际字段(ElasticSearch mapping doesn't work)并已成功运行此代码,因此我的ElasticSearch安装看起来很好。

我需要做什么来改变轮胎的这个查询(即我如何才能制止在这里工作)返回两个记录?

回答

0

我原以为这样会在比赛中产生两个记录,因为战斗和战斗有相同的主干。然而,它只返回的第一条记录:

权。 '战斗'阻止'战斗',并返回只有“战斗”的结果。战斗将做同样的事情,除非你设置你的搜索索引,否则匹配。

如果你想让它表现你所描述的方式,你可能想使你的默认索引使用边缘NGRAM分析仪,使“拼”也将匹配“打架”,并将其返回。如果您也查询“战斗”,这也将具有我认为可取的效果,即匹配“战斗”和“战斗”。

0

那么,事实证明,这是我的一个非常简单的错误。我忽略在定义视频的散列中包含“类型”。更换

videos = [ 
    { :code => '1', :description => "some fight video" }, 
    { :code => '2', :description => "a fighting video" } 
] 

videos = [ 
    { :type => 'video', :code => '1', :description => "some fight video" }, 
    { :type => 'video', :code => '2', :description => "a fighting video" } 
] 

解决了这一问题。

代码更改的影响是将正确的分析器应用于描述字段。以前,雪球分析器只能应用于导致搜索查询被阻止的搜索查询。如果我在查询语句中输入“描述:战斗”,它仍然会匹配第一个结果 - “有些战斗视频”,而不是“战斗视频”匹配。这让我意识到记录没有被正确分析。