本文整理汇总了Python中whoosh.filedb.filestore.RamStorage.searcher方法的典型用法代码示例。如果您正苦于以下问题:Python RamStorage.searcher方法的具体用法?Python RamStorage.searcher怎么用?Python RamStorage.searcher使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类whoosh.filedb.filestore.RamStorage
的用法示例。
在下文中一共展示了RamStorage.searcher方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_hit_column
# 需要导入模块: from whoosh.filedb.filestore import RamStorage [as 别名]
# 或者: from whoosh.filedb.filestore.RamStorage import searcher [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")
示例2: test_fractional_weights
# 需要导入模块: from whoosh.filedb.filestore import RamStorage [as 别名]
# 或者: from whoosh.filedb.filestore.RamStorage import searcher [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])
示例3: test_numeric
# 需要导入模块: from whoosh.filedb.filestore import RamStorage [as 别名]
# 或者: from whoosh.filedb.filestore.RamStorage import searcher [as 别名]
def test_numeric():
schema = fields.Schema(id=fields.ID(stored=True),
integer=fields.NUMERIC(int),
floating=fields.NUMERIC(float))
ix = RamStorage().create_index(schema)
w = ix.writer()
w.add_document(id=u("a"), integer=5820, floating=1.2)
w.add_document(id=u("b"), integer=22, floating=2.3)
w.add_document(id=u("c"), integer=78, floating=3.4)
w.add_document(id=u("d"), integer=13, floating=4.5)
w.add_document(id=u("e"), integer=9, floating=5.6)
w.commit()
with ix.searcher() as s:
qp = qparser.QueryParser("integer", schema)
q = qp.parse(u("5820"))
r = s.search(q)
assert len(r) == 1
assert r[0]["id"] == "a"
with ix.searcher() as s:
r = s.search(qp.parse("floating:4.5"))
assert len(r) == 1
assert r[0]["id"] == "d"
q = qp.parse("integer:*")
assert q.__class__ == query.Every
assert q.field() == "integer"
q = qp.parse("integer:5?6")
assert q == query.NullQuery
示例4: test_collect_limit
# 需要导入模块: from whoosh.filedb.filestore import RamStorage [as 别名]
# 或者: from whoosh.filedb.filestore.RamStorage import searcher [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_nested_delete
# 需要导入模块: from whoosh.filedb.filestore import RamStorage [as 别名]
# 或者: from whoosh.filedb.filestore.RamStorage import searcher [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"]
示例6: test_nested_parent
# 需要导入模块: from whoosh.filedb.filestore import RamStorage [as 别名]
# 或者: from whoosh.filedb.filestore.RamStorage import searcher [as 别名]
def test_nested_parent():
schema = fields.Schema(name=fields.ID(stored=True), type=fields.ID,
part=fields.ID, price=fields.NUMERIC)
ix = RamStorage().create_index(schema)
with ix.writer() as w:
with w.group():
w.add_document(name=u("iPad"), type=u("product"))
w.add_document(part=u("screen"), price=100)
w.add_document(part=u("battery"), price=50)
w.add_document(part=u("case"), price=20)
with w.group():
w.add_document(name=u("iPhone"), type=u("product"))
w.add_document(part=u("screen"), price=60)
w.add_document(part=u("battery"), price=30)
w.add_document(part=u("case"), price=10)
with w.group():
w.add_document(name=u("Mac mini"), type=u("product"))
w.add_document(part=u("hard drive"), price=50)
w.add_document(part=u("case"), price=50)
with ix.searcher() as s:
price = s.schema["price"]
pq = query.Term("type", "product")
cq = query.Term("price", 50)
q = query.NestedParent(pq, cq)
r = s.search(q)
assert sorted([hit["name"] for hit in r]) == ["Mac mini", "iPad"]
示例7: test_highlight_daterange
# 需要导入模块: from whoosh.filedb.filestore import RamStorage [as 别名]
# 或者: from whoosh.filedb.filestore.RamStorage import searcher [as 别名]
def test_highlight_daterange():
from datetime import datetime
schema = fields.Schema(id=fields.ID(unique=True, stored=True),
title=fields.TEXT(stored=True),
content=fields.TEXT(stored=True),
released=fields.DATETIME(stored=True))
ix = RamStorage().create_index(schema)
w = ix.writer()
w.update_document(
id=u('1'),
title=u('Life Aquatic'),
content=u('A nautic film crew sets out to kill a gigantic shark.'),
released=datetime(2004, 12, 25)
)
w.update_document(
id=u('2'),
title=u('Darjeeling Limited'),
content=u('Three brothers meet in India for a life changing train journey.'),
released=datetime(2007, 10, 27)
)
w.commit()
s = ix.searcher()
r = s.search(Term('content', u('train')), terms=True)
assert_equal(len(r), 1)
assert_equal(r[0]["id"], "2")
assert_equal(r[0].highlights("content"), 'for a life changing <b class="match term0">train</b> journey')
r = s.search(DateRange('released', datetime(2007, 1, 1), None))
assert_equal(len(r), 1)
assert_equal(r[0].highlights("content"), '')
示例8: test_no_parents
# 需要导入模块: from whoosh.filedb.filestore import RamStorage [as 别名]
# 或者: from whoosh.filedb.filestore.RamStorage import searcher [as 别名]
def test_no_parents():
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", "bravo")
cq = query.Or([query.Term("name", "two"), query.Term("name", "four")])
q = query.NestedParent(pq, cq)
r = s.search(q)
assert r.is_empty()
示例9: test_scoring
# 需要导入模块: from whoosh.filedb.filestore import RamStorage [as 别名]
# 或者: from whoosh.filedb.filestore.RamStorage import searcher [as 别名]
def test_scoring():
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 ix.searcher() as s:
q = query.NestedParent(query.Term("kind", "class"),
query.Term("name", "add"))
r = s.search(q)
assert [hit["name"] for hit in r] == ["Calculator", "Index", "Accumulator"]
示例10: test_whole_noterms
# 需要导入模块: from whoosh.filedb.filestore import RamStorage [as 别名]
# 或者: from whoosh.filedb.filestore.RamStorage import searcher [as 别名]
def test_whole_noterms():
schema = fields.Schema(text=fields.TEXT(stored=True), tag=fields.KEYWORD)
ix = RamStorage().create_index(schema)
with ix.writer() as w:
w.add_document(text=u("alfa bravo charlie delta echo foxtrot golf"),
tag=u("foo"))
with ix.searcher() as s:
r = s.search(query.Term("text", u("delta")))
assert len(r) == 1
r.fragmenter = highlight.WholeFragmenter()
r.formatter = highlight.UppercaseFormatter()
hi = r[0].highlights("text")
assert hi == u("alfa bravo charlie DELTA echo foxtrot golf")
r = s.search(query.Term("tag", u("foo")))
assert len(r) == 1
r.fragmenter = highlight.WholeFragmenter()
r.formatter = highlight.UppercaseFormatter()
hi = r[0].highlights("text")
assert hi == u("")
hi = r[0].highlights("text", minscore=0)
assert hi == u("alfa bravo charlie delta echo foxtrot golf")
示例11: test_extend_filtered
# 需要导入模块: from whoosh.filedb.filestore import RamStorage [as 别名]
# 或者: from whoosh.filedb.filestore.RamStorage import searcher [as 别名]
def test_extend_filtered():
schema = fields.Schema(id=fields.STORED, text=fields.TEXT(stored=True))
ix = RamStorage().create_index(schema)
w = ix.writer()
w.add_document(id=1, text=u("alfa bravo charlie"))
w.add_document(id=2, text=u("bravo charlie delta"))
w.add_document(id=3, text=u("juliet delta echo"))
w.add_document(id=4, text=u("delta bravo alfa"))
w.add_document(id=5, text=u("foxtrot sierra tango"))
w.commit()
hits = lambda result: [hit["id"] for hit in result]
with ix.searcher() as s:
r1 = s.search(query.Term("text", u("alfa")), filter=set([1, 4]))
assert r1.allowed == set([1, 4])
assert len(r1.top_n) == 0
r2 = s.search(query.Term("text", u("bravo")))
assert len(r2.top_n) == 3
assert hits(r2) == [1, 2, 4]
r3 = r1.copy()
assert r3.allowed == set([1, 4])
assert len(r3.top_n) == 0
r3.extend(r2)
assert len(r3.top_n) == 3
assert hits(r3) == [1, 2, 4]
示例12: test_boolean
# 需要导入模块: from whoosh.filedb.filestore import RamStorage [as 别名]
# 或者: from whoosh.filedb.filestore.RamStorage import searcher [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)
示例13: test_missing_field
# 需要导入模块: from whoosh.filedb.filestore import RamStorage [as 别名]
# 或者: from whoosh.filedb.filestore.RamStorage import searcher [as 别名]
def test_missing_field():
schema = fields.Schema()
ix = RamStorage().create_index(schema)
with ix.searcher() as s:
with pytest.raises(KeyError):
s.document_numbers(id=u("test"))
示例14: test_pages
# 需要导入模块: from whoosh.filedb.filestore import RamStorage [as 别名]
# 或者: from whoosh.filedb.filestore.RamStorage import searcher [as 别名]
def test_pages():
from whoosh.scoring import Frequency
schema = fields.Schema(id=fields.ID(stored=True), c=fields.TEXT)
ix = RamStorage().create_index(schema)
w = ix.writer()
w.add_document(id=u("1"), c=u("alfa alfa alfa alfa alfa alfa"))
w.add_document(id=u("2"), c=u("alfa alfa alfa alfa alfa"))
w.add_document(id=u("3"), c=u("alfa alfa alfa alfa"))
w.add_document(id=u("4"), c=u("alfa alfa alfa"))
w.add_document(id=u("5"), c=u("alfa alfa"))
w.add_document(id=u("6"), c=u("alfa"))
w.commit()
with ix.searcher(weighting=Frequency) as s:
q = query.Term("c", u("alfa"))
r = s.search(q)
assert [d["id"] for d in r] == ["1", "2", "3", "4", "5", "6"]
r = s.search_page(q, 2, pagelen=2)
assert [d["id"] for d in r] == ["3", "4"]
r = s.search_page(q, 2, pagelen=4)
assert r.total == 6
assert r.pagenum == 2
assert r.pagelen == 2
示例15: test_numeric_ranges
# 需要导入模块: from whoosh.filedb.filestore import RamStorage [as 别名]
# 或者: from whoosh.filedb.filestore.RamStorage import searcher [as 别名]
def test_numeric_ranges():
schema = fields.Schema(id=fields.STORED, num=fields.NUMERIC)
ix = RamStorage().create_index(schema)
w = ix.writer()
for i in xrange(400):
w.add_document(id=i, num=i)
w.commit()
with ix.searcher() as s:
qp = qparser.QueryParser("num", schema)
def check(qs, target):
q = qp.parse(qs)
result = [s.stored_fields(d)["id"] for d in q.docs(s)]
assert result == target
# Note that range() is always inclusive-exclusive
check("[10 to 390]", list(range(10, 390 + 1)))
check("[100 to]", list(range(100, 400)))
check("[to 350]", list(range(0, 350 + 1)))
check("[16 to 255]", list(range(16, 255 + 1)))
check("{10 to 390]", list(range(11, 390 + 1)))
check("[10 to 390}", list(range(10, 390)))
check("{10 to 390}", list(range(11, 390)))
check("{16 to 255}", list(range(17, 255)))