Opened 8 years ago
Closed 6 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 , 8 years ago
| Cc: | added |
|---|
comment:2 by , 8 years ago
| Summary: | Can't combine 2 SearchVector-s with different configs → Allow combining SearchVectors with different configs |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
| Type: | Bug → Cleanup/optimization |
Tentatively accepting -- someone more knowledgeable than I is welcome to close it if it's infeasible.
comment:3 by , 8 years ago
| Cc: | added |
|---|
comment:4 by , 7 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 , 7 years ago
| Cc: | added |
|---|
comment:6 by , 6 years ago
| Has patch: | set |
|---|---|
| Needs tests: | set |
| Owner: | set to |
| Status: | new → assigned |
| Version: | 1.10 → master |
comment:7 by , 6 years ago
| Needs tests: | unset |
|---|---|
| Summary: | Allow combining SearchVectors with different configs → Allow combining SearchVectors with different configs. |
| Triage Stage: | Accepted → Ready for checkin |
Marc, can you say if that restriction could be removed?