honmaple 8 лет назад
Родитель
Сommit
79e6886cda
2 измененных файлов с 25 добавлено и 43 удалено
  1. 11 0
      README.org
  2. 14 43
      manager.py

+ 11 - 0
README.org

@@ -34,6 +34,7 @@
    + Choice markdown to ask
    + Tags rss
    + Avatar
+   + Full text search with whoosh
      
 ** Installation
    
@@ -73,6 +74,10 @@
     python manager.py init_db
     #+END_SRC
 
+*** create full text index
+    #+BEGIN_SRC sh
+    python manager.py create_index
+    #+END_SRC
 *** Create admin account
     #+BEGIN_SRC shell
     python manager.py create_user
@@ -105,6 +110,7 @@
    + 可选markdown提问
    + 节点rss
    + 头像...
+   + 全文搜索(基于whoosh)
      
 ** 安装
    创建虚拟环境
@@ -144,6 +150,11 @@
     python manager.py init_db
     #+END_SRC
     
+*** 创建全文搜索索引
+    #+BEGIN_SRC sh
+    python manager.py create_index
+    #+END_SRC
+
 *** 创建管理员账户
     #+BEGIN_SRC shell
     python manager.py create_user

+ 14 - 43
manager.py

@@ -6,7 +6,7 @@
 # Author: jianglin
 # Email: xiyang0807@gmail.com
 # Created: 2016-10-25 22:08:39 (CST)
-# Last Update:星期日 2017-4-16 13:54:53 (CST)
+# Last Update:星期日 2017-4-16 15:38:22 (CST)
 #          By:
 # Description:
 # **************************************************************************
@@ -26,62 +26,33 @@ manager = Manager(app)
 
 
 @manager.command
-def index():
+def create_index():
     from forums.extension import search
     return search.create_index()
 
 
 @manager.command
-def ss():
+def update_index():
     from forums.extension import search
-    from forums.api.topic.models import Topic
-    results = search.whoosh_search(Topic, '河海大学', ['title'], 3)
-    print('results:')
-    print(results)
-    for i in results:
-        print(i)
-        print(i.highlights("title"))  # 高亮标题中的检索词
+    return search.create_index(update=True)
 
 
 @manager.command
-def test():
+def delete_index():
+    from forums.extension import search
+    return search.create_index(delete=True)
+
+
+@manager.command
+def test_index():
     from forums.extension import search
-    from whoosh.index import open_dir
-    from whoosh.index import create_in
-    from whoosh.fields import Schema, TEXT, ID
-    from jieba.analyse import ChineseAnalyzer
     from forums.api.topic.models import Topic
-    analyzer = ChineseAnalyzer()
-
-    # 创建schema, stored为True表示能够被检索
-    schema = search._schema(Topic)
-    ix = search._index(Topic)
-    # schema = Schema(
-    #     title=TEXT(
-    #         stored=True, analyzer=analyzer),
-    #     id=ID(stored=False),
-    #     content=TEXT(
-    #         stored=True, analyzer=analyzer))
-    # 存储schema信息至'indexdir'目录下
-    # indexdir = 'whoosh_index/Topic'
-    # if not os.path.exists(indexdir):
-    #     os.mkdir(indexdir)
-    # ix = create_in(indexdir, schema)
-    # ix = open_dir(indexdir)
-    writer = ix.writer()
-    instances = Topic.query.enable_eagerloads(False).yield_per(100)
-    for instance in instances:
-        print(instance)
-        writer.add_document(
-            title=instance.title,
-            id=str(instance.id),
-            content=instance.content)
-    writer.commit()
-    searcher = ix.searcher()
-    results = searcher.find("title", "河海大学")
+    results = search.whoosh_search(Topic, '河海大学', ['title'], 3)
+    print('results:')
     print(results)
     for i in results:
         print(i)
+        print(i.highlights("title"))  # 高亮标题中的检索词
 
 
 @manager.command