#31340: Improve expression support for __search lookup and SearchQuery
-------------------------------------+-------------------------------------
     Reporter:  Baptiste Mispelon    |                    Owner:  Baptiste
                                     |  Mispelon
         Type:  New feature          |                   Status:  closed
    Component:  contrib.postgres     |                  Version:  dev
     Severity:  Normal               |               Resolution:  fixed
     Keywords:                       |             Triage Stage:  Ready for
                                     |  checkin
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Comment (by David):

 Does the current implementation works well with custom SQL functions which
 returns {{{tsquery}}} and does not use any field which is already a
 {{{tsquery}}}?

 In one of my project I have created a custom SQL function which takes a
 list of integers (the IDs of some row in a specific table) and builds a
 {{{tsquery}}}. I tought that using the following it would work well while
 it raises an exception

 {{{#!python

 class TheModel(models.Model):
    searchable = SearchVectorField()


 TheModel.objects.filter(searchable=models.Func(123, 345,
 function='my_tsquery_func', output_field=SearchQueryField())
 # django.db.utils.ProgrammingError: function plainto_tsquery(tsquery) does
 not exist
 }}}

 Because the SQL produced is  and this produces an exception.

 {{{#!sql
 SELECT id, searchable
 FROM myapp_themodel
 WHERE searchable @@ plainto_tsquery(my_tsquery_func(123, 345));
 }}}

 To add more context the {{{my_tsquery_func}}} looks like the following
 (the goal is to combine {{{tsvector}}}s into a {{{tsquery}}} to build a
 similarity algorithm):

 {{{#!sql
 CREATE OR REPLACE FUNCTION my_tsquery_func(first_id bigint, second_id
 bigint) RETURNS tsquery AS $$
 SELECT
     to_tsquery(
         array_to_string(
             tsvector_to_array(
                 (SELECT  searchable FROM myapp_themodel WHERE id =
 first_id LIMIT 1)
                || (SELECT  searchable FROM myapp_themodel WHERE id =
 second_id LIMIT 1)
             ),
             ' | '
         )
     );
 $$ LANGUAGE sql PARALLEL SAFE STABLE;
 }}}
-- 
Ticket URL: <https://code.djangoproject.com/ticket/31340#comment:7>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/01070196fded6f60-831a0a9c-02b3-498c-8af7-105a3a7a35c4-000000%40eu-central-1.amazonses.com.

Reply via email to