2016-03-01 15 views
0

我有一個與Post模型和body:text與文本寶石的導軌(4.2.3)博客應用程序。使用textacular gem,我如何匹配大量文本中的一個單詞?

創建帖子:

> my_post = Post.create! title: 'my post', body: 'this post is about physics and ...' 

現在,我要爲「物理」相匹配的身體進行搜索,但事實並非如此。如果我嘗試:

> Post.basic_search('physics').first #=> nil 

如果我改變身體更短,它的工作原理:

> my_post.body = "about physics" 
> my_post.save! 
> Post.basic_search('physics').first #=> <my_post> 

我也試過fuzzy_search,而不是基本的搜索,但它給出了相同的結果。

如何使用textacular在長文本字段中搜索單詞的存在?在Github上

回答

0

自述說Model.basic_search將搜索模式的唯一string列:

Game.basic_search('Sonic') # will search through the model's :string columns 
Game.basic_search(title: 'Mario', system: 'Nintendo') 

併爲fuzzy searches自述說:

注意,模糊搜索是受到強加的相似性閾值pg_trgm模塊。默認值爲0.3,表示總字符串中至少有30%必須與您的搜索內容匹配。

您可以在大文本字段上將相似性閾值設置爲非常小的值。

參考:Github

+0

basic_search似乎與所有stringish下地幹活(全文收錄)。設置fuzzy_search閾值是非常不明確的,因爲我不知道字段長度是多少,所以任何猜測在較低的閾值只是一個猜測。如果我設置的閾值太低,那麼即使完全不相關的記錄着陸匹配。 – Anand

0

最後我用杜松子酒指數像這樣,然後給我我想要的比賽(與basic_search:

# in a migration 
execute "create index on posts using gin(to_tsvector('english', body));" 

# console query 
> my_post = Post.create! title: 'my post', body: 'this post is about physics and ...' 
> Post.basic_search('physics').first #=> <my_post>  
相關問題