From 629c5845568d8332d5106d42bad580306ad8cba9 Mon Sep 17 00:00:00 2001 From: Elena ``of Valhalla'' Grandi Date: Wed, 28 Oct 2020 14:25:51 +0100 Subject: Let the type loaders know how to index a field --- lesana/collection.py | 36 +++--------------------------------- lesana/types.py | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 33 deletions(-) diff --git a/lesana/collection.py b/lesana/collection.py index f399f62..77023f7 100644 --- a/lesana/collection.py +++ b/lesana/collection.py @@ -198,39 +198,9 @@ class Collection(object): doc = xapian.Document() self.indexer.set_document(doc) - # Fields with prefix, for field search - for field in self.indexed_fields: - if field['multi']: - values = entry.data.get(field['name']) - else: - values = [entry.data.get(field['name'])] - if not values: - values = [] - for v in values: - try: - self.indexer.index_text(str(v), 1, field['prefix']) - except ValueError as e: - logging.info( - "Not indexing empty? value {}: {}".format( - entry.data.get(field['name']), str(e) - ) - ) - # unprefixed fields, for full text search - for field in self.indexed_fields: - if field.get('free_search', False): - if field['multi']: - values = entry.data.get(field['name']) - else: - values = [entry.data.get(field['name'])] - if not values: - values = [] - for v in values: - try: - self.indexer.index_text(v) - self.indexer.increase_termpos() - except ValueError: - # probably already logged earlier - pass + for field, loader in self.fields.items(): + loader.index(doc, self.indexer, entry.data.get(field)) + doc.set_data(entry.yaml_data) doc.add_boolean_term(entry.idterm) doc.add_value(0, entry.fname.encode('utf-8')) diff --git a/lesana/types.py b/lesana/types.py index 0f20a75..b013a48 100644 --- a/lesana/types.py +++ b/lesana/types.py @@ -24,6 +24,28 @@ class LesanaType: def empty(self): raise NotImplementedError + def index(self, doc, indexer, value): + """ + Index a value for this field type. + + Override this for types that need any kind of special treatment + to be indexed. + + See LesanaList for an idea on how to do so. + """ + to_index = self.field.get('index', False) + if not to_index: + return False + if not value: + logging.info( + "Not indexing empty value {}".format(value) + ) + prefix = self.field.get('prefix', 'X' + self.field['name'].upper()) + indexer.index_text(str(value), 1, prefix) + if to_index == 'free': + indexer.index_text(str(value)) + indexer.increase_termpos() + class LesanaString(LesanaType): """ @@ -261,6 +283,10 @@ class LesanaList(LesanaType): def empty(self): return [] + def index(self, doc, indexer, value): + for v in value: + super().index(doc, indexer, v) + class LesanaValueError(ValueError): """ -- cgit v1.2.3