这里有详细内容(链接被被阉割!)
whoosh在应用上划分三个步骤:
下面依次阐述各步骤
使用Whoosh的第一步就是要建立索引对象。首先要定义索引模式,以字段的形式列在索引中。例如:
>>> from whoosh.fields import * >>> schema = Schema(title=TEXT, path=ID, content=TEXT)
>>> schema = Schema(title=TEXT(stored=True), path=ID(stored=True), content=TEXT)
from whoosh.fields import SchemaClass, TEXT, KEYWORD, ID, STORED
class MySchema(SchemaClass):
path = ID(stored=True)
title = TEXT(stored=True)
content = TEXT
tags = KEYWORD
在上例中,title=TEXT,title是字段名称,后面的TEXT是该字段的类型。这两个分别说明了索引内容和查找对象类型。whoosh有如下字段类型,供建立所以模式使用。
关于索引字段类型的更多内容,请看这里.(链接被被阉割!)
索引模式建立之后,还要建立索引存储目录。如下:
import os.path
from whoosh.index import create_in
from whoosh.index import open_dir
if not os.path.exists('index'): #如果目录index不存在则创建
os.mkdir('index')
ix = create_in("index",schema) #按照schema模式建立索引目录
ix = open_dir("index") #打开该目录一遍存储索引文件import os.path
from whoosh import fields
from whoosh import index
schema = fields.Schema(title=TEXT(stored=True), path=ID(stored=True), content=TEXT)
if not os.path.exists("index"):
os.mkdir("index")
ix = index.create_in("index",schema)
ix = index.open_dir("index")
(待续)
本文属于阉割之后的版本。要看完整版,请到我的github:qiwsir的ITArticles里面的BasicPython。
原文:http://blog.csdn.net/qiwsir/article/details/37660447