Created
September 15, 2018 02:19
-
-
Save charlesreid1/f5934ce7cb1235f7e9dc4fe40f1ac9c8 to your computer and use it in GitHub Desktop.
Whoosh test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from whoosh import analysis, fields, index, qparser, query, searching, scoring | |
from whoosh.filedb.filestore import RamStorage | |
from whoosh.compat import b, u, text_type | |
def main(): | |
schema = fields.Schema(name=fields.TEXT(stored=True), | |
hobbies=fields.TEXT(stored=True)) | |
storage = RamStorage() | |
ix = storage.create_index(schema) | |
writer = ix.writer() | |
writer.add_document(name=u('Frank'), hobbies=u('baseball, basketball')) | |
writer.add_document(name=u('Joe'), hobbies=u('track, rugby')) | |
writer.add_document(name=u('Adam'), hobbies=u('sportsball, Joe')) | |
writer.add_document(name=u('Tom'), hobbies=u('whatever')) | |
writer.commit() | |
r = ix.reader() | |
assert r.field_length("hobbies") == 7 | |
assert r.field_length("name") == 4 | |
r.close() | |
writer = ix.writer() | |
writer.add_document(name=u('Jonny')) | |
writer.commit() | |
with ix.searcher() as s: | |
r = s.reader() | |
assert r.field_length("hobbies") == 7 | |
assert r.field_length("name") == 5 | |
################# | |
parser = qparser.MultifieldParser(['name', 'hobbies'], schema=schema) | |
# this works because, by default, this is | |
# searching both name and hobbies fields. | |
q = parser.parse(u("baseball")) | |
result = s.search(q) | |
assert len(result) == 1 | |
# this searches only the hobbies field | |
q = parser.parse(u("hobbies:baseball")) | |
result = s.search(q) | |
assert len(result) == 1 | |
# this searches only the name field | |
q = parser.parse(u("name:Joe")) | |
result = s.search(q) | |
assert len(result) == 1 | |
# one hobby, one name | |
q = parser.parse(u("Joe")) | |
result = s.search(q) | |
assert len(result) == 2 | |
################# | |
parser = qparser.QueryParser("name",schema=schema) | |
q = parser.parse(u("baseball")) | |
result = s.search(q) | |
assert len(result) == 0 | |
# huh. this actually works. | |
q = parser.parse(u("hobbies:baseball")) | |
result = s.search(q) | |
assert len(result) == 1 | |
# only one result because only "name" is searched | |
q = parser.parse(u("Joe")) | |
result = s.search(q) | |
assert len(result) == 1 | |
# here, all free search terms only search the name field | |
# (returns none, even though there is a hobbies entry named Joe) | |
q = parser.parse(u("hobbies:baseball Joe")) | |
result = s.search(q) | |
assert len(result) == 0 | |
q = parser.parse(u("hobbies:rugby Joe")) | |
result = s.search(q) | |
assert len(result) == 1 | |
if __name__=="__main__": | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment