CREATE OR REPLACE FUNCTION public.search(searchNam...
# sql
s
CREATE OR REPLACE FUNCTION public.search(searchName text) RETURNS [text, text] LANGUAGE 'plpgsql' AS $$ begin SELECT logo,name FROM assets WHERE to_tsvector(name) @@ to_tsquery(searchName+':*') return name,logo; end $$; Here is the monster I created.
g
A few things I notice: use search_name not searchName or you need to use "searchName" in quotes returns text[] not returns [text,text] In postgres you need to use || for concatenate not + Then you need to return an array ( which I've never done....) but you what you have for return is not correct
s
Thanks a lot, I think I only need the array thing now. 🙂
CREATE OR REPLACE FUNCTION public.search(search_name text) RETURNS text LANGUAGE 'plpgsql' AS $$ begin return SELECT name FROM assets WHERE to_tsvector(name) @@ to_tsquery(search_name||':*'); end $$;
I tried just that, and it's still not working. 😅
g
What error? I'm not sure how that select would work with a single value return. Just to see where things are at try RETURNS setof assets and use Select * and see if you get your matching records when you run the function.
s
even i have to work with a search function using GIN pgtrigram any help from anyone who has done it??
g
@STILLWATER; Not sure what that has to do with this question and few will likely see it in this thread. Normally google search on postgres and your topic is quickest way to get info on very specific topics.
s
I did search that but my query has like a lot of where clauses and all and im still confused the way gin and indexing works
g
At this point all I can say is maybe try and be more specific in a question in the SQL section on your issue. I've not seen many here even mention trigrams. If you search up above on trigram or tsvector you will see the few conversations here on those topics.
s
Ill get my query as soon as i get access to my laptop and get specific on case