|
| 1 | +#问题 |
| 2 | + |
| 3 | +Whoosh是python中解决索引查找的模块,在讨论[索引查找的文章](https://github.com/qiwsir/algorithm/blob/master/index_search.md)已经对有关索引查找进行了阐述,此处详细说明Whoosh模块的应用。 |
| 4 | + |
| 5 | +#思路说明 |
| 6 | + |
| 7 | +##Whoosh的安装 |
| 8 | + |
| 9 | +[这里有详细内容](https://github.com/qiwsir/algorithm/blob/master/index_search.md) |
| 10 | + |
| 11 | +##whoosh的使用步骤 |
| 12 | + |
| 13 | +whoosh在应用上划分三个步骤: |
| 14 | + |
| 15 | +1. 建立索引和模式对象 |
| 16 | +2. 写入索引文件 |
| 17 | +3. 搜索 |
| 18 | + |
| 19 | +下面依次阐述各步骤 |
| 20 | + |
| 21 | +##建立索引和模式对象 |
| 22 | + |
| 23 | +###建立索引模式 |
| 24 | + |
| 25 | +使用Whoosh的第一步就是要建立索引对象。首先要定义索引模式,以字段的形式列在索引中。例如: |
| 26 | + |
| 27 | + >>> from whoosh.fields import * |
| 28 | + >>> schema = Schema(title=TEXT, path=ID, content=TEXT) |
| 29 | + |
| 30 | +title/path/content就是所谓的字段。每个字段对应索引查找目标文件的一部分信息,上面的例子中就是建立索引的模式:索引内容包括title/path/content。一个字段建立了索引,意味着它能够被搜索;也能够被存储,意味着返回结果。例如上面的例子,可以写成: |
| 31 | + |
| 32 | + >>> schema = Schema(title=TEXT(stored=True), path=ID(stored=True), content=TEXT) |
| 33 | + |
| 34 | +这里在某些字段后面添加了(stored=True),意味着将返回该字段的搜索结果。 |
| 35 | + |
| 36 | +以上就建立好了索引模式,不需要重复建立索引模式,因为一旦此模式建立,将随索引保存。 |
| 37 | + |
| 38 | +在生产过程中,如果你愿意,还可以建立一个类用于建立索引模式。如下例子: |
| 39 | + |
| 40 | + from whoosh.fields import SchemaClass, TEXT, KEYWORD, ID, STORED |
| 41 | + |
| 42 | + class MySchema(SchemaClass): |
| 43 | + path = ID(stored=True) |
| 44 | + title = TEXT(stored=True) |
| 45 | + content = TEXT |
| 46 | + tags = KEYWORD |
| 47 | + |
| 48 | +**索引字段类型** |
| 49 | + |
| 50 | +在上例中,title=TEXT,title是字段名称,后面的TEXT是该字段的类型。这两个分别说明了索引内容和查找对象类型。whoosh有如下字段类型,供建立所以模式使用。 |
| 51 | + |
| 52 | +- whoosh.fields.ID:仅能为一个单元值,即不能分割为若干个词,通常用于诸如文件路径,URL,日期,分类。 |
| 53 | +- whoosh.fields.STORED:该字段随文件保存,但是不能被索引,也不能被查询。常用于显示文件信息。 |
| 54 | +- whoosh.fields.KEYWORD:用空格或者逗号(半角)分割的关键词,可被索引和搜索。为了节省空间,不支持词汇搜索。 |
| 55 | +- whoosh.fields.TEXT:文件的文本内容。建立文本的索引并存储,支持词汇搜索。 |
| 56 | +- whoosh.fields.NUMERIC:数字类型,保存整数或浮点数。 |
| 57 | +- whoosh.fields.BOOLEAN:布尔类值 |
| 58 | +- whoosh.fields.DATETIME:时间对象类型 |
| 59 | + |
| 60 | +关于[索引字段类型的更多内容,请看这里](https://pythonhosted.org/Whoosh/schema.html). |
| 61 | + |
| 62 | +###建立索引存储目录 |
| 63 | + |
| 64 | +索引模式建立之后,还要建立索引存储目录。如下: |
| 65 | + |
| 66 | + import os.path |
| 67 | + from whoosh.index import create_in |
| 68 | + from whoosh.index import open_dir |
| 69 | + |
| 70 | + if not os.path.exists('index'): #如果目录index不存在则创建 |
| 71 | + os.mkdir('index') |
| 72 | + ix = create_in("index",schema) #按照schema模式建立索引目录 |
| 73 | + ix = open_dir("index") #打开该目录一遍存储索引文件 |
| 74 | + |
| 75 | +上例中,用create_in创建一个具有前述索引模式的索引存储目录对象,所有的索引将被保存在该目录(index)中。 |
| 76 | + |
| 77 | +之后,用open_dir打开这个目录。 |
| 78 | + |
| 79 | +第一步到此结束。 |
| 80 | + |
| 81 | +把上面的代码整理一下,供参考: |
| 82 | + |
| 83 | + import os.path |
| 84 | + |
| 85 | + from whoosh import fields |
| 86 | + from whoosh import index |
| 87 | + |
| 88 | + schema = fields.Schema(title=TEXT(stored=True), path=ID(stored=True), content=TEXT) |
| 89 | + |
| 90 | + if not os.path.exists("index"): |
| 91 | + os.mkdir("index") |
| 92 | + |
| 93 | + ix = index.create_in("index",schema) |
| 94 | + ix = index.open_dir("index") |
| 95 | + |
| 96 | +##写索引文件 |
| 97 | + |
| 98 | +(待续) |
0 commit comments