Opened 2 months ago

Last modified 3 weeks 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 Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hey!
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 django.contrib.postgres.search 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 (2)

comment:1 Changed 8 weeks ago by Tim Graham

Cc: Marc Tamlyn added

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

comment:2 Changed 3 weeks 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.

Note: See TracTickets for help on using tickets.
Back to Top