You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Right now the only way to update a VectorField is to write out a literal tsvector, e.g. 'a:1 fat:2 cat:3 sat:4 on:5 a:6 mat:7 and:8 ate:9 a:10 fat:11 rat:12' or 'a:1A fat:2B,4C cat:5D'. While this is obviously very flexible, most of the time I just want to call to_tsvector.
In [1]: fromsearch.modelsimportSearchTestIn [2]: search_test=SearchTest()
In [3]: search_test.search_index='swim swimming swam'In [4]: search_test.save()
In [5]: search_test=SearchTest.objects.get(id=search_test.id) # Reload model instanceIn [6]: search_test.search_indexOut[6]: "'swam' 'swim' 'swimming'"# The string was literally inserted as a ts vector# I would rather it be converted and stemmed: "'swam':3 'swim':1,2"
I think that inserting a string as a literal tsvector is a fine default, but I still needed to be able to call to_tsvector. I got around that by creating a special python object and registering an adapter with psycopg2.
# search/tsvector.pyfrompsycopg2.extensionsimportadapt, AsIsclassTsVector(object):
""" Represents a call to to_tsvector at the database level. Use: TsVector('swim swimming swam'), TsVector('simple', 'swim swimming swam') TsVector('english', 'swim swimming swam') """def__init__(self, *args):
assertlen(args) in (1, 2), "Arguments should be TsVector([ config regconfig, ] document text)"iflen(args) ==1:
self.config=Noneself.document=args[0]
else:
self.config=args[0]
self.document=args[1]
defadapt_tsvector(tsvector):
""" Adapts TsVector object for use in DB. """iftsvector.configisNone:
returnAsIs("to_tsvector(%s)"%adapt(tsvector.document))
else:
returnAsIs("to_tsvector(%s, %s)"% (adapt(tsvector.config), adapt(tsvector.document)))
Put this somewhere that it'll only get executed once. With Django 1.7, an AppConfig is a pretty natural place to put it.
# search/apps.pyfromdjango.appsimportAppConfigfrompsycopg2.extensionsimportregister_adapterfromsearch.tsvectorimportTsVector, adapt_tsvectorclassSearchConfig(AppConfig):
name='search'verbose_name="Search"defready(self):
# Register the TsVector classregister_adapter(TsVector, adapt_tsvector)
Example use:
In [1]: fromsearch.modelsimportSearchTestIn [2]: fromsearch.tsvectorimportTsVectorIn [3]: search_test=SearchTest()
In [4]: search_test.search_index=TsVector('english', 'swim swimming swam')
In [5]: search_test.save()
In [6]: search_test=SearchTest.objects.get(id=search_test.id)
In [7]: search_test.search_indexOut[7]: "'swam':3 'swim':1,2"
Thoughts?
The text was updated successfully, but these errors were encountered:
Right now the only way to update a VectorField is to write out a literal
tsvector
, e.g.'a:1 fat:2 cat:3 sat:4 on:5 a:6 mat:7 and:8 ate:9 a:10 fat:11 rat:12'
or'a:1A fat:2B,4C cat:5D'
. While this is obviously very flexible, most of the time I just want to callto_tsvector
.Here's a quick overview of the problem:
I think that inserting a string as a literal tsvector is a fine default, but I still needed to be able to call
to_tsvector
. I got around that by creating a special python object and registering an adapter with psycopg2.Put this somewhere that it'll only get executed once. With Django 1.7, an
AppConfig
is a pretty natural place to put it.Example use:
Thoughts?
The text was updated successfully, but these errors were encountered: