使用Whoosh即时搜索-使用Whoosh输出JSON结果索引并搜索MySQL表

问题描述

|| 我想使用Whoosh为MySQL表建立索引并创建一个即时搜索页面,因此我需要将Whoosh搜索的结果转换为JSON。是否已经有脚本或项目实现了此目的?我曾尝试搜索,但只找到Haystack搜索Django。 如果不能,我会得到一些广泛的指导,我应该如何去做。 谢谢。     

解决方法

        Whoosh结果对象基本上是词典列表。从示例中:
>>> # 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\"}]
您可以很容易地将其转换为JSON:
import json
def results2json(results):
   return json.dumps([r for r in results])
    

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...