Opened 7 years ago

Closed 5 years ago

#28528 closed Cleanup/optimization (fixed)

Allow combining SearchVectors with different configs.

Reported by: M1ha Shvn Owned by: Adam Boaler
Component: contrib.postgres Version: dev
Severity: Normal Keywords: SearchQueries PostgreSQL FullTextSearch
Cc: Marc Tamlyn, Paolo Melchiorre, Jaap Roes Triage Stage: Ready for checkin
Has patch: yes 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 (8)

comment:1 by Tim Graham, 7 years ago

Cc: Marc Tamlyn added

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

comment:2 by Tim Graham, 7 years ago

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 by Paolo Melchiorre, 7 years ago

Cc: Paolo Melchiorre added

comment:4 by Jaap Roes, 6 years ago

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 by Jaap Roes, 6 years ago

Cc: Jaap Roes added

comment:6 by Mariusz Felisiak, 5 years ago

Has patch: set
Needs tests: set
Owner: set to Adam Boaler
Status: newassigned
Version: 1.10master

comment:7 by Mariusz Felisiak, 5 years ago

Needs tests: unset
Summary: Allow combining SearchVectors with different configsAllow combining SearchVectors with different configs.
Triage Stage: AcceptedReady for checkin

comment:8 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

Resolution: fixed
Status: assignedclosed

In 4c6ab1f:

Fixed #28528 -- Allowed combining SearchVectors with different configs.

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