1. Matt Chaput
  2. whoosh
Issue #384 resolved

Search file name in folders

Anonymous created an issue

How can i create search using whoosh that will search filename in folders and give result. can anybody help me?

Comments (2)

  1. Xiaosong Rong

    A dame simple example:

    import os
    from whoosh import (index, fields, qparser)
    
    schema = fields.Schema(
        name=fields.ID(stored=True),
        path=fields.STORED,
    )
    
    indexdir = 'indexdir/'                                                                                                                         
    if not os.path.exists(indexdir):
        os.makedirs(indexdir)
    
    ix = index.create_in(indexdir, schema)
    writer = ix.writer()
    
    # the folder that you want to search
    target_path = './'
    
    for parent, dirs, files in os.walk(target_path):
        for _name in files:
            _path = os.path.join(parent, _name)
            writer.add_document(name=unicode(_name), path=unicode(_path))
    writer.commit()
    
    with ix.searcher() as searcher:
        parser = qparser.QueryParser('name', ix.schema)
        # the file that you want to search
        filename = u''
        query = parser.parse(filename)
        results = searcher.search(query)
        for r in results:
            print r
    
  2. Log in to comment