| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -95,4 +95,49 @@ title/path/content就是所谓的字段。每个字段对应索引查找目标 | |||
| 95 | 95 | ||
| 96 | 96 | ##写索引文件 | |
| 97 | 97 | ||
| 98 | - (待续) | ||
| 98 | + 下面开始写入索引内容,过程如下: | ||
| 99 | + | ||
| 100 | + writer = ix.writer() | ||
| 101 | + writer.add_document(title=u"my document", content=u"this is my document", path=u"/a", tags=u"firlst short", icon=u"/icons/star.png") | ||
| 102 | + writer.add_document(title=u"my second document", content=u"this is my second document", path=u"/b", tags=u"second short", icon=u"/icons/sheep.png") | ||
| 103 | + writer.commit() | ||
| 104 | + | ||
| 105 | + 特别注意: | ||
| 106 | + | ||
| 107 | + - 字段的值必须是unicode类型 | ||
| 108 | + - 不是每个字段都必须赋值 | ||
| 109 | + | ||
| 110 | + 更多的内容,请参考:[如何索引文件官方文档](https://pythonhosted.org/Whoosh/indexing.html) | ||
| 111 | + | ||
| 112 | + ##搜索 | ||
| 113 | + | ||
| 114 | + 开始搜索,需要新建立一个对象,如: | ||
| 115 | + | ||
| 116 | + searcher = ix.searcher() | ||
| 117 | + | ||
| 118 | + 一般来讲,不是这么简单地,建立对象相当于开始搜索,完事之后要关闭,所以在实战中,常常写成: | ||
| 119 | + | ||
| 120 | + withe ix.searcher() as searcher: | ||
| 121 | + (do somthing) | ||
| 122 | + | ||
| 123 | + 或者写成(与上面的等效): | ||
| 124 | + | ||
| 125 | + try: | ||
| 126 | + searcher = ix.searcher() | ||
| 127 | + (do somthing) | ||
| 128 | + finally: | ||
| 129 | + searcher.close() | ||
| 130 | + | ||
| 131 | + 接下来就开始搜索了,以搜索content为例: | ||
| 132 | + | ||
| 133 | + from whoosh.qparser import QueryParser | ||
| 134 | + with ix.searcher() as searcher: | ||
| 135 | + query = QueryParser("content",ix.schema).parse("second") | ||
| 136 | + result = searcher.search(query) | ||
| 137 | + results[0] | ||
| 138 | + | ||
| 139 | + 返回显示: | ||
| 140 | + | ||
| 141 | + {"title":u"my second document","path":u"/a"} | ||
| 142 | + | ||
| 143 | + 前面已经将上述两个字段设置为stored=True. | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments