Opened 2 years ago

Last modified 10 months ago

#28528 new Cleanup/optimization

Allow combining SearchVectors with different configs

Reported by: M1ha Shvn Owned by:
Component: contrib.postgres Version: 1.10
Severity: Normal Keywords: SearchQueries PostgreSQL FullTextSearch
Cc: Marc Tamlyn, Paolo Melchiorre, Jaap Roes Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


I'm trying to make a fulltext search on 2 languages: russian and english.
I've created GIN index for 2 languages in PostgreSQL and select query works fine using an index:

CREATE INDEX CONCURRENTLY messages_conversationpart_body ON messages_conversationpart USING gin((to_tsvector('russian', body) || to_tsvector('english', body)));
SELECT id, body FROM messages_conversationpart WHERE (to_tsvector('russian', body) || to_tsvector('english', body)) @@ (to_tsquery('russian', 'cats') || to_tsquery('english', 'cats')) LIMIT 20;

Then I've found this article and desided to form this query using django. Here's the result:

    from import SearchVector, SearchQuery
    from messages.models import ConversationPart
    qs = ConversationPart.objects.\
            annotate(search=SearchVector('body', config='russian') + SearchVector('body', config='english')).\
            filter(search=SearchQuery(search_phrase, config='russian') | SearchQuery(search_phrase, config='english'))
    qs = qs[:20]

But attempt to execute this query returns TypeError: SearchVector can only be combined with other SearchVectors
The reason is here:

class SearchVectorCombinable(object):
    ADD = '||'

    def _combine(self, other, connector, reversed, node=None):
        if not isinstance(other, SearchVectorCombinable) or not self.config == other.config:
            raise TypeError('SearchVector can only be combined with other SearchVectors')
        if reversed:
            return CombinedSearchVector(other, connector, self, self.config)
        return CombinedSearchVector(self, connector, other, self.config)

So, what is the reason for config equality control?

Change History (5)

comment:1 Changed 2 years ago by Tim Graham

Cc: Marc Tamlyn added

Marc, can you say if that restriction could be removed?

comment:2 Changed 2 years ago by Tim Graham

Summary: Can't combine 2 SearchVector-s with different configsAllow combining SearchVectors with different configs
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

Tentatively accepting -- someone more knowledgeable than I is welcome to close it if it's infeasible.

comment:3 Changed 23 months ago by Paolo Melchiorre

Cc: Paolo Melchiorre added

comment:4 Changed 10 months ago by Jaap Roes

The same restriction is applied to SearchQueryCombinable with a more specific error message TypeError("SearchQuery configs don't match.").

I'm not sure if this restriction makes sense. In sql it seems to be allowed SELECT to_tsquery('simple', 'working') || to_tsquery('english', 'working') > 'working' | 'work'

comment:5 Changed 10 months ago by Jaap Roes

Cc: Jaap Roes added
Note: See TracTickets for help on using tickets.
Back to Top