Ticket #27732: searchrankvectorfieldfix.diff

File searchrankvectorfieldfix.diff, 2.2 KB (added by Matt Hoskins, 2 years ago)
  • django/contrib/postgres/search.py

    diff -r --unified a/django/contrib/postgres/search.py b/django/contrib/postgres/search.py
    a b  
    179179    _output_field = FloatField()
    180180
    181181    def __init__(self, vector, query, **extra):
    182         if not hasattr(vector, 'resolve_expression'):
    183             vector = SearchVector(vector)
    184182        if not hasattr(query, 'resolve_expression'):
    185183            query = SearchQuery(query)
    186184        weights = extra.get('weights')
     
    203201        )
    204202        return sql, extra_params + params
    205203
     204    def resolve_expression(self, query=None, allow_joins=True, reuse=None, summarize=False, for_save=False):
     205        resolved = super(SearchRank, self).resolve_expression(query, allow_joins, reuse, summarize, for_save)
     206        resolved_source_expressions = resolved.get_source_expressions()
     207        if not isinstance(resolved_source_expressions[0].output_field, SearchVectorField):
     208            vector_resolved_expression = SearchVector(self.get_source_expressions()[0]).resolve_expression(query, allow_joins, reuse, summarize)
     209            resolved.set_source_expressions([vector_resolved_expression] + resolved_source_expressions[1:])
     210        return resolved
    206211
    207212SearchVectorField.register_lookup(SearchVectorExact)
    208213
  • tests/postgres_tests/test_search.py

    diff -r --unified a/tests/postgres_tests/test_search.py b/tests/postgres_tests/test_search.py
    a b  
    112112        searched = Line.objects.filter(dialogue_search_vector=SearchQuery('cadeaux', config='french'))
    113113        self.assertSequenceEqual(searched, [self.french])
    114114
     115    def test_existing_vector_ranking(self):
     116        Line.objects.update(dialogue_search_vector=SearchVector('dialogue'))
     117        searched = Line.objects.filter(character=self.minstrel).annotate(
     118            rank=SearchRank('dialogue_search_vector', SearchQuery('brave sir robin')),
     119        ).order_by('rank')
     120        self.assertSequenceEqual(searched, [self.verse2, self.verse1, self.verse0])
     121
    115122
    116123class MultipleFieldsTest(GrailTestData, PostgreSQLTestCase):
    117124
Back to Top