#28852: Search in admin can't use index when several fields are in 
`search_fields`
------------------------------------------------+------------------------
               Reporter:  Jonathan Sundqvist    |          Owner:  nobody
                   Type:  Cleanup/optimization  |         Status:  new
              Component:  contrib.admin         |        Version:  master
               Severity:  Normal                |       Keywords:  search
           Triage Stage:  Unreviewed            |      Has patch:  0
    Needs documentation:  0                     |    Needs tests:  0
Patch needs improvement:  0                     |  Easy pickings:  0
                  UI/UX:  0                     |
------------------------------------------------+------------------------
 There are a couple of issues with the current implementation of search. If
 you have several fields like the following example.

 {{{
     class MyAdmin(model.ModelAdmin):
         search_fields = ['=field_1', '@field_2']
 }}}

 You'll never be able to use any of the indexes that may exist on those
 fields, and if you were to remove `field_1` you would still not be able to
 use any index on the full text search index for `field_2`.

 If you have two or more fields in search fields you'll end up with a where
 statement that uses OR, and the index won't be used. In the case of a full
 text index you need to define the index with the appropriate config for
 the ts_vector. (See https://www.postgresql.org/docs/current/static
 /textsearch-tables.html#TEXTSEARCH-TABLES-INDEX).

 The current implementation uses `__search`
 
(https://github.com/django/django/blob/master/django/contrib/admin/options.py#L945)
 which ignores a specific config. So the index won't be used during the
 query.

 To accomodate a config for a full text search I would suggest using
 `@english@field_2`. Then you could split on '@' and use a SearchVector and
 SearchQuery with the config `english`.

 To make it possible to use index when several fields are defined in
 `search_fields`, instead of using OR. Make several querysets and do a
 union on all them. That way it will execute the query using the index.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/28852>
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 post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/053.4ed4a49c9b7c607ab016f63d584ede76%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to