1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
from __future__ import with_statement
from whoosh import fields
from whoosh.compat import u, b
from whoosh.util.testing import TempIndex
def test_addfield():
schema = fields.Schema(id=fields.ID(stored=True), content=fields.TEXT)
with TempIndex(schema, "addfield") as ix:
w = ix.writer()
w.add_document(id=u("a"), content=u("alfa"))
w.add_document(id=u("b"), content=u("bravo"))
w.add_document(id=u("c"), content=u("charlie"))
w.commit()
ix.add_field("added", fields.KEYWORD(stored=True))
w = ix.writer()
w.add_document(id=u("d"), content=u("delta"), added=u("fourth"))
w.add_document(id=u("e"), content=u("echo"), added=u("fifth"))
w.commit(merge=False)
with ix.searcher() as s:
assert ("id", "d") in s.reader()
assert s.document(id="d") == {"id": "d", "added": "fourth"}
assert s.document(id="b") == {"id": "b"}
def test_addfield_spelling():
schema = fields.Schema(id=fields.ID(stored=True), content=fields.TEXT)
with TempIndex(schema, "addfield") as ix:
w = ix.writer()
w.add_document(id=u("a"), content=u("alfa"))
w.add_document(id=u("b"), content=u("bravo"))
w.add_document(id=u("c"), content=u("charlie"))
w.commit()
ix.add_field("added", fields.KEYWORD(stored=True))
w = ix.writer()
w.add_document(id=u("d"), content=u("delta"), added=u("fourth"))
w.add_document(id=u("e"), content=u("echo"), added=u("fifth"))
w.commit(merge=False)
with ix.searcher() as s:
assert s.document(id=u("d")) == {"id": "d", "added": "fourth"}
assert s.document(id=u("b")) == {"id": "b"}
def test_removefield():
schema = fields.Schema(id=fields.ID(stored=True),
content=fields.TEXT,
city=fields.KEYWORD(stored=True))
with TempIndex(schema, "removefield") as ix:
w = ix.writer()
w.add_document(id=u("b"), content=u("bravo"), city=u("baghdad"))
w.add_document(id=u("c"), content=u("charlie"), city=u("cairo"))
w.add_document(id=u("d"), content=u("delta"), city=u("dakar"))
w.commit()
with ix.searcher() as s:
assert s.document(id=u("c")) == {"id": "c", "city": "cairo"}
w = ix.writer()
w.remove_field("content")
w.remove_field("city")
w.commit()
ixschema = ix._current_schema()
assert ixschema.names() == ["id"]
assert ixschema.stored_names() == ["id"]
with ix.searcher() as s:
assert ("content", b("charlie")) not in s.reader()
assert s.document(id=u("c")) == {"id": u("c")}
def test_optimize_away():
schema = fields.Schema(id=fields.ID(stored=True),
content=fields.TEXT,
city=fields.KEYWORD(stored=True))
with TempIndex(schema, "optimizeaway") as ix:
w = ix.writer()
w.add_document(id=u("b"), content=u("bravo"), city=u("baghdad"))
w.add_document(id=u("c"), content=u("charlie"), city=u("cairo"))
w.add_document(id=u("d"), content=u("delta"), city=u("dakar"))
w.commit()
with ix.searcher() as s:
assert s.document(id=u("c")) == {"id": "c", "city": "cairo"}
w = ix.writer()
w.remove_field("content")
w.remove_field("city")
w.commit(optimize=True)
with ix.searcher() as s:
assert ("content", u("charlie")) not in s.reader()
assert s.document(id=u("c")) == {"id": u("c")}
if __name__ == "__main__":
test_addfield()
|