Skip to content

Commit 0ced19f

Browse files
committed
whoosh全文索引查找
1 parent 73e31db commit 0ced19f

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

index_search_whoosh.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,49 @@ title/path/content就是所谓的字段。每个字段对应索引查找目标
9595

9696
##写索引文件
9797

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.

0 commit comments

Comments
 (0)