U
U
un1t2017-05-11 12:32:15
PostgreSQL
un1t, 2017-05-11 12:32:15

How to simplify a query in PostgreSQL?

I have a query like:

SELECT id,
       name,
       ts_rank_cd(to_tsvector('russian', name), query)
FROM catalog_product,
     to_tsquery('russian', 'телевизор') query
WHERE to_tsvector('russian', name) @@ query;

It confuses me that the "to_tsvector('russian', name)" part is duplicated here.
In fact, this is a simplified example, in a real query there is much more complicated in this place, something like
setweight( coalesce( to_tsvector('russian', name),''),'A') || ' ' || 
setweight( coalesce( to_tsvector('russian', array_to_string(items, ' ')),''),'B')

I don’t want to copy-paste such a footcloth twice.
If there was just a table, then a field with a calculated vector could be added to the table. In my case, this cannot be done, because. I select not from the table, and from other request in which set of tables are joined.
Is it possible to do without copypasta?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question