本文整理汇总了Python中whoosh.filedb.filestore.RamStorage.writer方法的典型用法代码示例。如果您正苦于以下问题:Python RamStorage.writer方法的具体用法?Python RamStorage.writer怎么用?Python RamStorage.writer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类whoosh.filedb.filestore.RamStorage
的用法示例。
在下文中一共展示了RamStorage.writer方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_filter
# 需要导入模块: from whoosh.filedb.filestore import RamStorage [as 别名]
# 或者: from whoosh.filedb.filestore.RamStorage import writer [as 别名]
def test_filter():
schema = fields.Schema(id=fields.STORED, path=fields.ID, text=fields.TEXT)
ix = RamStorage().create_index(schema)
w = ix.writer()
w.add_document(id=1, path=u("/a/1"), text=u("alfa bravo charlie"))
w.add_document(id=2, path=u("/b/1"), text=u("bravo charlie delta"))
w.add_document(id=3, path=u("/c/1"), text=u("charlie delta echo"))
w.commit(merge=False)
w = ix.writer()
w.add_document(id=4, path=u("/a/2"), text=u("delta echo alfa"))
w.add_document(id=5, path=u("/b/2"), text=u("echo alfa bravo"))
w.add_document(id=6, path=u("/c/2"), text=u("alfa bravo charlie"))
w.commit(merge=False)
w = ix.writer()
w.add_document(id=7, path=u("/a/3"), text=u("bravo charlie delta"))
w.add_document(id=8, path=u("/b/3"), text=u("charlie delta echo"))
w.add_document(id=9, path=u("/c/3"), text=u("delta echo alfa"))
w.commit(merge=False)
with ix.searcher() as s:
fq = Or([Prefix("path", "/a"), Prefix("path", "/b")])
r = s.search(Term("text", "alfa"), filter=fq)
assert_equal([d["id"] for d in r], [1, 4, 5])
r = s.search(Term("text", "bravo"), filter=fq)
assert_equal([d["id"] for d in r], [1, 2, 5, 7, ])
示例2: test_all_parents_deleted
# 需要导入模块: from whoosh.filedb.filestore import RamStorage [as 别名]
# 或者: from whoosh.filedb.filestore.RamStorage import writer [as 别名]
def test_all_parents_deleted():
schema = fields.Schema(kind=fields.ID, name=fields.KEYWORD(scorable=True, stored=True))
ix = RamStorage().create_index(schema)
with ix.writer() as w:
with w.group():
w.add_document(kind=u("class"), name=u("Index"))
w.add_document(kind=u("method"), name=u("add document"))
w.add_document(kind=u("method"), name=u("add reader"))
w.add_document(kind=u("method"), name=u("close"))
with w.group():
w.add_document(kind=u("class"), name=u("Accumulator"))
w.add_document(kind=u("method"), name=u("add"))
w.add_document(kind=u("method"), name=u("get result"))
with w.group():
w.add_document(kind=u("class"), name=u("Calculator"))
w.add_document(kind=u("method"), name=u("add"))
w.add_document(kind=u("method"), name=u("add all"))
w.add_document(kind=u("method"), name=u("add some"))
w.add_document(kind=u("method"), name=u("multiply"))
w.add_document(kind=u("method"), name=u("close"))
with w.group():
w.add_document(kind=u("class"), name=u("Deleter"))
w.add_document(kind=u("method"), name=u("add"))
w.add_document(kind=u("method"), name=u("delete"))
with ix.writer() as w:
w.delete_by_term("name", "Index")
w.delete_by_term("name", "Accumulator")
w.delete_by_term("name", "Calculator")
w.delete_by_term("name", "Deleter")
with ix.searcher() as s:
q = query.NestedParent(query.Term("kind", "class"), query.Term("name", "add"))
r = s.search(q)
assert r.is_empty()
示例3: test_excludematcher
# 需要导入模块: from whoosh.filedb.filestore import RamStorage [as 别名]
# 或者: from whoosh.filedb.filestore.RamStorage import writer [as 别名]
def test_excludematcher():
schema = fields.Schema(content=fields.TEXT(stored=True))
ix = RamStorage().create_index(schema)
domain = ("alfa", "bravo", "charlie", "delta")
for _ in xrange(3):
w = ix.writer()
for ls in permutations(domain):
w.add_document(content=u(" ").join(ls))
w.commit(merge=False)
w = ix.writer()
w.delete_document(5)
w.delete_document(10)
w.delete_document(28)
w.commit(merge=False)
q = Term("content", "bravo")
with ix.searcher() as s:
m = q.matcher(s)
while m.is_active():
content = s.stored_fields(m.id())["content"].split()
spans = m.spans()
for span in spans:
assert content[span.start] == "bravo"
m.next()
示例4: test_collect_limit
# 需要导入模块: from whoosh.filedb.filestore import RamStorage [as 别名]
# 或者: from whoosh.filedb.filestore.RamStorage import writer [as 别名]
def test_collect_limit():
schema = fields.Schema(id=fields.STORED, text=fields.TEXT)
ix = RamStorage().create_index(schema)
w = ix.writer()
w.add_document(id="a", text=u("alfa bravo charlie delta echo"))
w.add_document(id="b", text=u("bravo charlie delta echo foxtrot"))
w.add_document(id="c", text=u("charlie delta echo foxtrot golf"))
w.add_document(id="d", text=u("delta echo foxtrot golf hotel"))
w.add_document(id="e", text=u("echo foxtrot golf hotel india"))
w.commit()
with ix.searcher() as s:
r = s.search(query.Term("text", u("golf")), limit=10)
assert_equal(len(r), 3)
count = 0
for _ in r:
count += 1
assert_equal(count, 3)
w = ix.writer()
w.add_document(id="f", text=u("foxtrot golf hotel india juliet"))
w.add_document(id="g", text=u("golf hotel india juliet kilo"))
w.add_document(id="h", text=u("hotel india juliet kilo lima"))
w.add_document(id="i", text=u("india juliet kilo lima mike"))
w.add_document(id="j", text=u("juliet kilo lima mike november"))
w.commit(merge=False)
with ix.searcher() as s:
r = s.search(query.Term("text", u("golf")), limit=20)
assert_equal(len(r), 5)
count = 0
for _ in r:
count += 1
assert_equal(count, 5)
示例5: test_hit_column
# 需要导入模块: from whoosh.filedb.filestore import RamStorage [as 别名]
# 或者: from whoosh.filedb.filestore.RamStorage import writer [as 别名]
def test_hit_column():
# Not stored
schema = fields.Schema(text=fields.TEXT())
ix = RamStorage().create_index(schema)
with ix.writer() as w:
w.add_document(text=u("alfa bravo charlie"))
with ix.searcher() as s:
r = s.search(query.Term("text", "alfa"))
assert len(r) == 1
hit = r[0]
with pytest.raises(KeyError):
_ = hit["text"]
# With column
schema = fields.Schema(text=fields.TEXT(sortable=True))
ix = RamStorage().create_index(schema)
with ix.writer(codec=W3Codec()) as w:
w.add_document(text=u("alfa bravo charlie"))
with ix.searcher() as s:
r = s.search(query.Term("text", "alfa"))
assert len(r) == 1
hit = r[0]
assert hit["text"] == u("alfa bravo charlie")
示例6: test_fractional_weights
# 需要导入模块: from whoosh.filedb.filestore import RamStorage [as 别名]
# 或者: from whoosh.filedb.filestore.RamStorage import writer [as 别名]
def test_fractional_weights():
ana = analysis.RegexTokenizer(r"\S+") | analysis.DelimitedAttributeFilter()
# With Positions format
schema = fields.Schema(f=fields.TEXT(analyzer=ana))
ix = RamStorage().create_index(schema)
w = ix.writer()
w.add_document(f=u("alfa^0.5 bravo^1.5 charlie^2.0 delta^1.5"))
w.commit()
with ix.searcher() as s:
wts = []
for word in s.lexicon("f"):
p = s.postings("f", word)
wts.append(p.weight())
assert_equal(wts, [0.5, 1.5, 2.0, 1.5])
# Try again with Frequency format
schema = fields.Schema(f=fields.TEXT(analyzer=ana, phrase=False))
ix = RamStorage().create_index(schema)
w = ix.writer()
w.add_document(f=u("alfa^0.5 bravo^1.5 charlie^2.0 delta^1.5"))
w.commit()
with ix.searcher() as s:
wts = []
for word in s.lexicon("f"):
p = s.postings("f", word)
wts.append(p.weight())
assert_equal(wts, [0.5, 1.5, 2.0, 1.5])
示例7: test_sort_text_field
# 需要导入模块: from whoosh.filedb.filestore import RamStorage [as 别名]
# 或者: from whoosh.filedb.filestore.RamStorage import writer [as 别名]
def test_sort_text_field():
domain = (
("Visual Display of Quantitative Information, The", 10),
("Envisioning Information", 10),
("Visual Explanations", 10),
("Beautiful Evidence", -10),
("Visual and Statistical Thinking", -10),
("Cognitive Style of Powerpoint", -10),
)
sorted_titles = sorted(d[0] for d in domain)
schema = fields.Schema(title=fields.TEXT(stored=True, sortable=True), num=fields.NUMERIC(sortable=True))
def test(ix):
with ix.searcher() as s:
# Sort by title
r = s.search(query.Every(), sortedby="title")
assert [hit["title"] for hit in r] == sorted_titles
# Sort by reverse title
facet = sorting.FieldFacet("title", reverse=True)
r = s.search(query.Every(), sortedby=facet)
assert [hit["title"] for hit in r] == list(reversed(sorted_titles))
# Sort by num (-10 to 10) first, and within that, by reverse title
facet = sorting.MultiFacet()
facet.add_field("num")
facet.add_field("title", reverse=True)
r = s.search(query.Every(), sortedby=facet)
target = [
"Visual and Statistical Thinking",
"Cognitive Style of Powerpoint",
"Beautiful Evidence",
"Visual Explanations",
"Visual Display of Quantitative Information, The",
"Envisioning Information",
]
assert [hit["title"] for hit in r] == target
# Single segment
ix = RamStorage().create_index(schema)
with ix.writer() as w:
for title, num in domain:
w.add_document(title=u(title), num=num)
test(ix)
# Multisegment
ix = RamStorage().create_index(schema)
# Segment 1
with ix.writer() as w:
for title, num in domain[:3]:
w.add_document(title=u(title), num=num)
# Segment 2
with ix.writer() as w:
for title, num in domain[3:]:
w.add_document(title=u(title), num=num)
w.merge = False
test(ix)
示例8: test_zero_procs
# 需要导入模块: from whoosh.filedb.filestore import RamStorage [as 别名]
# 或者: from whoosh.filedb.filestore.RamStorage import writer [as 别名]
def test_zero_procs():
schema = fields.Schema(text=fields.TEXT)
ix = RamStorage().create_index(schema)
with ix.writer(procs=0) as w:
assert isinstance(w, writing.IndexWriter)
with ix.writer(procs=1) as w:
assert isinstance(w, writing.IndexWriter)
示例9: test_nested_delete
# 需要导入模块: from whoosh.filedb.filestore import RamStorage [as 别名]
# 或者: from whoosh.filedb.filestore.RamStorage import writer [as 别名]
def test_nested_delete():
schema = fields.Schema(kind=fields.ID,
name=fields.KEYWORD(scorable=True, stored=True))
ix = RamStorage().create_index(schema)
with ix.writer() as w:
with w.group():
w.add_document(kind=u("class"), name=u("Index"))
w.add_document(kind=u("method"), name=u("add document"))
w.add_document(kind=u("method"), name=u("add reader"))
w.add_document(kind=u("method"), name=u("close"))
with w.group():
w.add_document(kind=u("class"), name=u("Accumulator"))
w.add_document(kind=u("method"), name=u("add"))
w.add_document(kind=u("method"), name=u("get result"))
with w.group():
w.add_document(kind=u("class"), name=u("Calculator"))
w.add_document(kind=u("method"), name=u("add"))
w.add_document(kind=u("method"), name=u("add all"))
w.add_document(kind=u("method"), name=u("add some"))
w.add_document(kind=u("method"), name=u("multiply"))
w.add_document(kind=u("method"), name=u("close"))
with w.group():
w.add_document(kind=u("class"), name=u("Deleter"))
w.add_document(kind=u("method"), name=u("add"))
w.add_document(kind=u("method"), name=u("delete"))
# Delete "Accumulator" class
with ix.writer() as w:
q = query.NestedParent(query.Term("kind", "class"),
query.Term("name", "Accumulator"))
w.delete_by_query(q)
# Check that Accumulator AND ITS METHODS are deleted
with ix.searcher() as s:
r = s.search(query.Term("kind", "class"))
assert sorted(hit["name"] for hit in r) == ["Calculator", "Deleter", "Index"]
names = [fs["name"] for _, fs in s.iter_docs()]
assert names == ["Index", "add document", "add reader", "close",
"Calculator", "add", "add all", "add some",
"multiply", "close", "Deleter", "add", "delete"]
# Delete any class with a close method
with ix.writer() as w:
q = query.NestedParent(query.Term("kind", "class"),
query.Term("name", "close"))
w.delete_by_query(q)
# Check the CLASSES AND METHODS are gone
with ix.searcher() as s:
names = [fs["name"] for _, fs in s.iter_docs()]
assert names == ["Deleter", "add", "delete"]
示例10: test_nocachefield_segments
# 需要导入模块: from whoosh.filedb.filestore import RamStorage [as 别名]
# 或者: from whoosh.filedb.filestore.RamStorage import writer [as 别名]
def test_nocachefield_segments():
schema = fields.Schema(a=fields.ID(stored=True))
ix = RamStorage().create_index(schema)
w = ix.writer()
w.add_document(a=u("bravo"))
w.add_document(a=u("echo"))
w.add_document(a=u("juliet"))
w.commit()
w = ix.writer()
w.add_document(a=u("kilo"))
w.add_document(a=u("foxtrot"))
w.add_document(a=u("charlie"))
w.commit(merge=False)
w = ix.writer()
w.delete_by_term("a", u("echo"))
w.add_document(a=u("alfa"))
w.add_document(a=u("india"))
w.add_document(a=u("delta"))
w.commit(merge=False)
with ix.searcher() as s:
q = query.TermRange("a", u("bravo"), u("k"))
facet = sorting.FieldFacet("a", reverse=True)
r = s.search(q, sortedby=facet)
assert [hit["a"] for hit in r] == ["juliet", "india", "foxtrot", "delta", "charlie", "bravo"]
mq = query.Or([query.Term("a", u("bravo")), query.Term("a", u("delta"))])
anq = query.AndNot(q, mq)
r = s.search(anq, sortedby=facet)
assert [hit["a"] for hit in r] == ["juliet", "india", "foxtrot", "charlie"]
mq = query.Or([query.Term("a", u("bravo")), query.Term("a", u("delta"))])
r = s.search(q, mask=mq, sortedby=facet)
assert [hit["a"] for hit in r] == ["juliet", "india", "foxtrot", "charlie"]
fq = query.Or(
[
query.Term("a", u("alfa")),
query.Term("a", u("charlie")),
query.Term("a", u("echo")),
query.Term("a", u("india")),
]
)
r = s.search(query.Every(), filter=fq, sortedby=facet)
assert [hit["a"] for hit in r] == ["india", "charlie", "alfa"]
nq = query.Not(query.Or([query.Term("a", u("alfa")), query.Term("a", u("india"))]))
r = s.search(query.Every(), filter=nq, sortedby=facet)
assert [hit["a"] for hit in r] == ["kilo", "juliet", "foxtrot", "delta", "charlie", "bravo"]
示例11: test_boolean_find_deleted
# 需要导入模块: from whoosh.filedb.filestore import RamStorage [as 别名]
# 或者: from whoosh.filedb.filestore.RamStorage import writer [as 别名]
def test_boolean_find_deleted():
# "Random" string of ones and zeros representing deleted and undeleted
domain = "1110001010001110010101000101001011101010001011111101000101010101"
schema = fields.Schema(i=fields.STORED, b=fields.BOOLEAN(stored=True))
ix = RamStorage().create_index(schema)
count = 0
# Create multiple segments just in case
for _ in xrange(5):
w = ix.writer()
for c in domain:
w.add_document(i=count, b=(c == "1"))
w.commit(merge=False)
# Delete documents where "b" is True
with ix.writer() as w:
w.delete_by_term("b", "t")
with ix.searcher() as s:
# Double check that documents with b=True are all deleted
reader = s.reader()
for docnum in xrange(s.doc_count_all()):
b = s.stored_fields(docnum)["b"]
assert b == reader.is_deleted(docnum)
# Try doing a search for documents where b=True
qp = qparser.QueryParser("b", ix.schema)
q = qp.parse("b:t")
r = s.search(q, limit=None)
assert len(r) == 0
# Make sure Every query doesn't match deleted docs
r = s.search(qp.parse("*"), limit=None)
assert not any(hit["b"] for hit in r)
assert not any(reader.is_deleted(hit.docnum) for hit in r)
r = s.search(qp.parse("*:*"), limit=None)
assert not any(hit["b"] for hit in r)
assert not any(reader.is_deleted(hit.docnum) for hit in r)
# Make sure Not query doesn't match deleted docs
q = qp.parse("NOT b:t")
r = s.search(q, limit=None)
assert not any(hit["b"] for hit in r)
assert not any(reader.is_deleted(hit.docnum) for hit in r)
r = s.search(q, limit=5)
assert not any(hit["b"] for hit in r)
assert not any(reader.is_deleted(hit.docnum) for hit in r)
示例12: test_term_stats
# 需要导入模块: from whoosh.filedb.filestore import RamStorage [as 别名]
# 或者: from whoosh.filedb.filestore.RamStorage import writer [as 别名]
def test_term_stats():
schema = fields.Schema(t=fields.TEXT)
ix = RamStorage().create_index(schema)
w = ix.writer()
w.add_document(t=u("alfa bravo charlie delta echo"))
w.add_document(t=u("bravo charlie delta echo foxtrot"))
w.add_document(t=u("charlie delta echo foxtrot golf"))
w.add_document(t=u("delta echo foxtrot"))
w.add_document(t=u("echo foxtrot golf hotel india juliet"))
w.add_document(t=u("foxtrot alfa alfa alfa"))
w.commit()
with ix.reader() as r:
ti = r.term_info("t", u("alfa"))
assert_equal(ti.weight(), 4.0)
assert_equal(ti.doc_frequency(), 2)
assert_equal(ti.min_length(), 4)
assert_equal(ti.max_length(), 5)
assert_equal(ti.max_weight(), 3.0)
assert_equal(ti.max_wol(), 3.0 / 4.0)
assert_equal(r.term_info("t", u("echo")).min_length(), 3)
assert_equal(r.doc_field_length(3, "t"), 3)
assert_equal(r.min_field_length("t"), 3)
assert_equal(r.max_field_length("t"), 6)
w = ix.writer()
w.add_document(t=u("alfa"))
w.add_document(t=u("bravo charlie"))
w.add_document(t=u("echo foxtrot tango bravo"))
w.add_document(t=u("golf hotel"))
w.add_document(t=u("india"))
w.add_document(t=u("juliet alfa bravo charlie delta echo foxtrot"))
w.commit(merge=False)
with ix.reader() as r:
ti = r.term_info("t", u("alfa"))
assert_equal(ti.weight(), 6.0)
assert_equal(ti.doc_frequency(), 4)
assert_equal(ti.min_length(), 1)
assert_equal(ti.max_length(), 7)
assert_equal(ti.max_weight(), 3.0)
assert_equal(ti.max_wol(), 1.0)
assert_equal(r.term_info("t", u("echo")).min_length(), 3)
assert_equal(r.min_field_length("t"), 1)
assert_equal(r.max_field_length("t"), 7)
示例13: test_everything_is_a_parent
# 需要导入模块: from whoosh.filedb.filestore import RamStorage [as 别名]
# 或者: from whoosh.filedb.filestore.RamStorage import writer [as 别名]
def test_everything_is_a_parent():
schema = fields.Schema(id=fields.STORED, kind=fields.ID,
name=fields.ID(stored=True))
k = u("alfa")
ix = RamStorage().create_index(schema)
with ix.writer() as w:
w.add_document(id=0, kind=k, name=u("one"))
w.add_document(id=1, kind=k, name=u("two"))
w.add_document(id=2, kind=k, name=u("three"))
w.add_document(id=3, kind=k, name=u("four"))
w.add_document(id=4, kind=k, name=u("one"))
w.add_document(id=5, kind=k, name=u("two"))
w.add_document(id=6, kind=k, name=u("three"))
w.add_document(id=7, kind=k, name=u("four"))
w.add_document(id=8, kind=k, name=u("one"))
w.add_document(id=9, kind=k, name=u("two"))
w.add_document(id=10, kind=k, name=u("three"))
w.add_document(id=11, kind=k, name=u("four"))
with ix.searcher() as s:
pq = query.Term("kind", k)
cq = query.Or([query.Term("name", "two"), query.Term("name", "four")])
q = query.NestedParent(pq, cq)
r = s.search(q)
assert [hit["id"] for hit in r] == [1, 3, 5, 7, 9, 11]
示例14: test_boolean
# 需要导入模块: from whoosh.filedb.filestore import RamStorage [as 别名]
# 或者: from whoosh.filedb.filestore.RamStorage import writer [as 别名]
def test_boolean():
schema = fields.Schema(id=fields.ID(stored=True),
done=fields.BOOLEAN(stored=True))
ix = RamStorage().create_index(schema)
w = ix.writer()
w.add_document(id=u("a"), done=True)
w.add_document(id=u("b"), done=False)
w.add_document(id=u("c"), done=True)
w.add_document(id=u("d"), done=False)
w.add_document(id=u("e"), done=True)
w.commit()
with ix.searcher() as s:
qp = qparser.QueryParser("id", schema)
r = s.search(qp.parse("done:true"))
assert sorted([d["id"] for d in r]) == ["a", "c", "e"]
assert all(d["done"] for d in r)
r = s.search(qp.parse("done:yes"))
assert sorted([d["id"] for d in r]) == ["a", "c", "e"]
assert all(d["done"] for d in r)
q = qp.parse("done:false")
assert q.__class__ == query.Term
assert q.text is False
assert schema["done"].to_bytes(False) == b("f")
r = s.search(q)
assert sorted([d["id"] for d in r]) == ["b", "d"]
assert not any(d["done"] for d in r)
r = s.search(qp.parse("done:no"))
assert sorted([d["id"] for d in r]) == ["b", "d"]
assert not any(d["done"] for d in r)
示例15: test_boolean_strings
# 需要导入模块: from whoosh.filedb.filestore import RamStorage [as 别名]
# 或者: from whoosh.filedb.filestore.RamStorage import writer [as 别名]
def test_boolean_strings():
schema = fields.Schema(i=fields.STORED, b=fields.BOOLEAN(stored=True))
ix = RamStorage().create_index(schema)
with ix.writer() as w:
w.add_document(i=0, b="true")
w.add_document(i=1, b="True")
w.add_document(i=2, b="false")
w.add_document(i=3, b="False")
w.add_document(i=4, b=u("true"))
w.add_document(i=5, b=u("True"))
w.add_document(i=6, b=u("false"))
w.add_document(i=7, b=u("False"))
with ix.searcher() as s:
qp = qparser.QueryParser("b", ix.schema)
def check(qs, nums):
q = qp.parse(qs)
r = s.search(q, limit=None)
assert [hit["i"] for hit in r] == nums
trues = [0, 1, 4, 5]
falses = [2, 3, 6, 7]
check("true", trues)
check("True", trues)
check("false", falses)
check("False", falses)
check("t", trues)
check("f", falses)