Clear ORM
Search…
Full Text Search
Full text search plugin offers full integration with tsvector capabilities of Postgresql.
It allows you to query models through the text content of one or multiple fields.
Let's assume we have a blog and want to implement full text search over title and content:
1
create_table "posts" do |t|
2
t.column :title, :string, null: false
3
t.column :content, :string, null: false
4
5
t.full_text_searchable on: [{"title", 'A'}, {"content", 'C'}]
6
end
Copied!
This migration will create a 3rd column named full_text_vector of type tsvector, a gin index, a trigger and a function to update automatically this column.
Over the on keyword, '{"title", 'A'}' means it allows search of the content of "title", with level of priority (weight) "A", which tells postgres than title content is more meaningful than the article content itself.
Now, let's build some models:
1
class Post
2
include Clear::Model
3
#...
4
5
full_text_searchable
6
end
7
8
Post.create!({title: "About poney", content: "Poney are cool"})
9
Post.create!({title: "About dog and cat", content: "Cat and dog are cool. But not as much as poney"})
10
Post.create!({title: "You won't believe: She raises her poney like as star!", content: "She's col because poney are cool"})
Copied!
Search is now easily done
1
Post.query.search("poney") # Return all the articles !
Copied!
Obviously, search call can be chained:
1
user = User.find!{ email == "[email protected]" }
2
Post.query.from_user(user).search("orm")
Copied!

Additional parameters

catalog
Select the catalog to use to build the tsquery. By default, pg_catalog.english is used.
1
# in your migration:
2
t.full_text_searchable on: [{"title", 'A'}, {"content", 'C'}], catalog: "pg_catalog.french"
3
4
# in your model
5
full_text_searchable catalog: "pg_catalog.french"
Copied!
For now, Clear doesn't offers dynamic selection of catalog (for let's say multi-lang service). If your app need this feature, do not hesitate to open an issue.
trigger_name, function_name
In migration, you can change the name generated for the trigger and the function, using theses two keys.
dest_field
The field created in the database, which will contains your ts vector. Default is full_text_vector.
1
#in your migration
2
t.full_text_searchable on: [{"title", 'A'}, {"content", 'C'}], dest_field: "tsv"
3
4
# in your model
5
full_text_searchable "tsv"
Copied!
Last modified 1yr ago
Copy link