Commits

Anonymous committed 105c27c

a working example of whoosh.

Comments (0)

Files changed (4)

+indexdir
+peps = http://hg.python.org/peps
+864b604d3742c2edc7ea759b3d599337f8dc7ae3 peps
+def getschema():
+    from whoosh.fields import Schema, TEXT, ID
+    return Schema(path=ID(stored=True), content=TEXT)
+
+
+def makeindex(ix):
+    import sys
+    from glob import glob
+    from codecs import open
+
+    writer = ix.writer()
+    print 'Start indexing.'
+    path_list = glob('peps/pep-*.txt')
+    for path in path_list:
+        writer.add_document(path=unicode(path),
+                            content=open(path, 'r', 'utf-8').read())
+        sys.stdout.write('.')
+        sys.stdout.flush()
+    writer.commit()
+    print
+    print 'Indexed {0} peps'.format(len(path_list))
+
+
+
+def getindex(indexdir="indexdir"):
+    import os
+    from whoosh import index
+    if not os.path.isdir(indexdir):
+        print "Directory '{0}' does not exist.".format(indexdir)
+        print "Creating index."
+        os.mkdir(indexdir)
+        ix = index.create_in(indexdir, getschema())
+        makeindex(ix)
+    else:
+        print "Loading index from {0}.".format(indexdir)
+        ix = index.open_dir(indexdir)
+    return ix
+
+
+def search(ix, querystr):
+    from whoosh.qparser import QueryParser
+
+    querystr = unicode(querystr)
+    print u"Search '{0}'".format(querystr)
+    with ix.searcher() as searcher:
+        query = QueryParser("content", ix.schema).parse(querystr)
+        results = searcher.search(query)
+        print results
+        for r in results:
+            print r
+
+
+def main():
+    import sys
+    ix = getindex()
+    querystr = ' '.join(sys.argv[1:])
+    if querystr:
+        search(ix, querystr)
+
+
+if __name__ == '__main__':
+    main()