问题
Trying to implement postgresql full text search example by following these steps;
I have created a table:
CREATE TABLE posts
(
id serial primary key,
content varchar(255),
tags varchar(255),
title varchar(255)
);
Altered the table to have a column of type TSVECTOR
ALTER TABLE posts ADD COLUMN searchtext TSVECTOR;
Created a trigger to store data in searchtext column:
CREATE TRIGGER ts_searchtext BEFORE INSERT OR UPDATE ON posts FOR EACH ROW EXECUTE PROCEDURE tsvector_update_trigger('searchtext', 'english', 'title', 'content', 'tags')
I have inserted a row:
insert into posts(content,tags,title) values('once upon a time I had a fat friend','friend, fat','Fat Boy');
Value under searchtext column is:
"'boy':2 'fat':1,10,13 'friend':11,12 'time':6 'upon':4"
Now, when I run query:
SELECT * FROM posts where searchtext @@ to_tsquery('english','fot');
It returns 0 rows.
So, basically I am looking for a solution where "fot" should have been matched against "fat" or we can say looking for result based on matched letters out of word.
or if we run:
SELECT * FROM posts where searchtext @@ to_tsquery('english','frend');
It should match against "friend". As per documentation of postgresql I think it will be done with the help of dictionaries. But now sure which .dict and .affix file should I use. Can anyone help me with more understanding on this.
回答1:
Try with plainto_tsquery function:
SELECT * FROM posts WHERE searchtext @@ plainto_tsquery('english frend');
来源:https://stackoverflow.com/questions/41034698/postgresql-full-text-search-for-similar-words