Search code examples
pythonmysqljsonsearchwhoosh

Instant search with Whoosh - Index and search a MySQL table using Whoosh outputing JSON results


I want to index a MySQL table using Whoosh and create an instant search page, so I need the results of the Whoosh search to be in JSON. Is there a script or a project that implements this already? I've tried searching but I only find Haystack search for Django.

If not can I get some broad pointers how I should go about doing this.

Thanks.


Solution

  • The Whoosh Results object is basically a list of dictionaries. From the examples:

    >>> # Show the best hit's stored fields
    >>> results[0]
    {"title": u"Hello World in Python", "path": u"/a/b/c"}
    >>> results[0:2]
    [{"title": u"Hello World in Python", "path": u"/a/b/c"}, {"title": u"Foo", "path": u"/bar"}]
    

    You could very easily turn this into JSON:

    import json
    def results2json(results):
       return json.dumps([r for r in results])