Using textacular gem, how do I match one word in a large body of text?

ruby-on-rails postgresql full-text-search

365 просмотра

2 ответа

I have a rails (4.2.3) blog app with the textacular gem with a Post model having title:string and body:text.

I create a post:

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

Now, I want the search for 'physics' to match the body, but it doesn't. If I try:

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

If I change the body to be shorter, it works:

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

I have also tried fuzzy_search instead of basic search, but it gives the same result.

How do I use textacular to search for existence of words in long text fields?

Автор: Anand Источник Размещён: 27.10.2019 11:38

Ответы (2)

1 плюс

README on Github says Model.basic_search will search only string columns of the model:

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

And for fuzzy searches README says:

Note that fuzzy searches are subject to a similarity threshold imposed by the pg_trgm module. The default is 0.3, meaning that at least 30% of the total string must match your search content.

You can set the similarity threshold to a very small value on large text fields.

Reference: Github

Автор: Hassan Akram Размещён: 01.03.2016 06:51

1 плюс

I ended up using a gin index like so, which then gives me the matches I want (with 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>    
Автор: Anand Размещён: 02.03.2016 04:28
Вопросы из категории :