Piki is a minimal wiki
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

search.py 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. from datetime import datetime
  2. from django.conf import settings
  3. import fstools
  4. import logging
  5. import os
  6. from whoosh.fields import Schema, ID, TEXT, DATETIME
  7. from whoosh.qparser.dateparse import DateParserPlugin
  8. from whoosh import index, qparser
  9. from .models import PikiPage
  10. logger = logging.getLogger(settings.ROOT_LOGGER_NAME).getChild(__name__)
  11. SCHEMA = Schema(
  12. id=ID(unique=True, stored=True),
  13. # Page
  14. title=TEXT,
  15. page_src=TEXT,
  16. tag=TEXT,
  17. # metadata
  18. creation_time=DATETIME,
  19. modified_time=DATETIME,
  20. modified_user=TEXT
  21. )
  22. def mk_whooshpath_if_needed():
  23. if not os.path.exists(settings.WHOOSH_PATH):
  24. fstools.mkdir(settings.WHOOSH_PATH)
  25. def create_index():
  26. mk_whooshpath_if_needed()
  27. logger.debug('Search Index created.')
  28. return index.create_in(settings.WHOOSH_PATH, schema=SCHEMA)
  29. def rebuild_index(ix):
  30. pages = PikiPage.objects.all()
  31. for pp in pages:
  32. if not pp.deleted:
  33. add_item(ix, pp)
  34. return len(pages)
  35. def load_index():
  36. mk_whooshpath_if_needed()
  37. try:
  38. ix = index.open_dir(settings.WHOOSH_PATH)
  39. except index.EmptyIndexError:
  40. ix = create_index()
  41. else:
  42. logger.debug('Search Index opened.')
  43. return ix
  44. def add_item(ix, pp: PikiPage):
  45. # Define Standard data
  46. #
  47. data = dict(
  48. id=pp.rel_path,
  49. #
  50. title=pp.title,
  51. page_src=pp.page_txt,
  52. tag=pp.tags,
  53. #
  54. creation_time=pp.creation_time,
  55. modified_time=pp.modified_time,
  56. modified_user=None if pp.modified_user is None else pp.modified_user.username
  57. )
  58. with ix.writer() as w:
  59. logger.info('Adding document with id=%s to the search index.', data.get('id'))
  60. w.update_document(**data)
  61. for key in data:
  62. logger.debug(' - Adding %s=%s', key, repr(data[key]))
  63. def whoosh_search(search_txt):
  64. ix = load_index()
  65. qp = qparser.MultifieldParser(['title', 'page_src', 'tag'], ix.schema)
  66. qp.add_plugin(DateParserPlugin(free=True))
  67. try:
  68. q = qp.parse(search_txt)
  69. except AttributeError:
  70. return None
  71. except Exception:
  72. return None
  73. with ix.searcher() as s:
  74. results = s.search(q, limit=None)
  75. rpl = []
  76. for hit in results:
  77. rpl.append(hit['id'])
  78. return rpl
  79. def delete_item(ix, pp: PikiPage):
  80. with ix.writer() as w:
  81. logger.info('Removing document with id=%s from the search index.', pp.rel_path)
  82. w.delete_by_term("id", pp.rel_path)
  83. def update_item(pp: PikiPage):
  84. ix = load_index()
  85. delete_item(ix, pp)
  86. add_item(ix, pp)