6 from __future__
import unicode_literals
8 from whoosh.analysis
import NgramFilter, StandardAnalyzer
9 from whoosh.fields
import ID, SchemaClass, TEXT
10 from whoosh.filedb.filestore
import FileStorage, RamStorage
11 from whoosh.index
import exists_in
12 from whoosh.qparser
import QueryParser
17 analyzer = StandardAnalyzer() | NgramFilter(minsize=2, maxsize=2)
22 index_path = settings.get(
'fulltext_dir')
24 storage = RamStorage()
25 index = storage.create_index(schema=WhooshSchema)
27 if not os.path.exists(index_path):
28 os.makedirs(index_path)
29 storage = FileStorage(index_path)
30 if not exists_in(index_path):
31 index = storage.create_index(schema=WhooshSchema)
33 index = storage.open_index(schema=WhooshSchema)
42 index_id = ID(stored=
True, unique=
True)
43 data = TEXT(stored=
False, analyzer=analyzer)
62 return ' '.join([unicode(getattr(self, attr))
for attr
in self.
index_fields])
78 writer.delete_by_term(
'index_id', self.
index_id())
94 ids =
_search(terms, cls.index_type())
95 return cls.query.filter(cls.id.in_(ids))
104 query =
parser(
'data', index.schema).parse(terms)
105 with index.searcher()
as searcher:
106 results = searcher.search(query)
117 for result
in results:
118 type, id = result.get(
'index_id').split(
':')
119 if type == index_type: