Anand
Anand

Reputation: 3760

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

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"
> my_post.save!
> 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?

Upvotes: 1

Views: 496

Answers (2)

Anand
Anand

Reputation: 3760

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>    

Upvotes: 1

Hassan Akram
Hassan Akram

Reputation: 652

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

Upvotes: 1

Related Questions