I've been trying to implement some kind of reverse full text search where
I store keywords in the database and I want to query models whose keywords
would match a given piece of text.
Here's a simplified model of what I'm working with:
{{{#!python
class SavedSearch(models.Model):
keywords = models.TextField()
def __str__(self):
return self.keywords
}}}
I've managed to achieve what I want in the case of the default search
configuration using annotation and wrapping things with `Value` or `Cast`:
{{{#!python
# This works
search_query = Cast('keywords', output_field=SearchQueryField())
search_vector = SearchVector(Value("lorem ipsum ...",
output_field=TextField()))
qs =
SavedSearch.objects.annotate(search=search_vector).filter(search=search_query)
}}}
But if I want to use a custom search configuration, things don't work
anymore:
{{{#!python
# This doesn't work (can't adapt type 'F')
search_query = SearchQuery(F('keywords'), config='english',
search_type='plain')
search_vector = SearchVector(Value("lorem ipsum ...",
output_field=TextField()))
qs =
SavedSearch.objects.annotate(search=search_vector).filter(search=search_query)
}}}
I'm not very familiar with the inner workings of `Lookup` objects but I
did some digging and I think I came up with a fix which involved fixing
two separate issues:
1) `SearchQuery` doesn't currently support anyting other than plain values
(`str`). Fixing this required changing both `resolve_expression()` and
`as_sql()`.
2) the `__search` lookup doesn't support things like `F` objects because
of it assumes that any value with a `resolve_expression` method must be a
`SearchQuery` object.
--
Ticket URL: <https://code.djangoproject.com/ticket/31340>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Comment (by Baptiste Mispelon):
Pull request here: https://github.com/django/django/pull/12525
--
Ticket URL: <https://code.djangoproject.com/ticket/31340#comment:1>
* stage: Unreviewed => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/31340#comment:2>
* owner: (none) => Baptiste Mispelon
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/31340#comment:3>
Comment (by Mariusz Felisiak <felisiak.mariusz@…>):
In [changeset:"dd704c670543edc4672cc6114fe3d8da0dfbed7a" dd704c6]:
{{{
#!CommitTicketReference repository=""
revision="dd704c670543edc4672cc6114fe3d8da0dfbed7a"
Refs #31340 -- Simplified SearchQuery by making it subclass Func.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/31340#comment:4>
* version: 3.0 => master
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/31340#comment:5>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"3baf92cf8230ad3a932986170fd07c8feae7ff2f" 3baf92cf]:
{{{
#!CommitTicketReference repository=""
revision="3baf92cf8230ad3a932986170fd07c8feae7ff2f"
Fixed #31340 -- Allowed query expressions in SearchQuery.value and
__search lookup.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/31340#comment:6>