Algolia查询生成器
algoliaqb的Python项目详细描述
一个库,用于帮助生成搜索Algolia的查询。在
此包用于algoliasearch库。在
问题:
我一直在做的一个应用程序有一些代码重复和增加的复杂性。这个软件包是我解决这些问题和提高可读性的一种方法。在
安装
pip install algoliaqb
过滤字符串
默认情况下,filter_map将所有输入视为字符串。这很直接。假设您有以下过滤器映射:
^{pr2}$使用以下flask.args:
{ "group_id": 1 }
AlgoliaQB将在flask.args内查找group_id,获取该值,然后为您重新映射该键和值。在
tenant_id:1
现在让我们尝试一个带有更多过滤器的示例。在
filter_map = { "group_id": "tenant_id", "is_reported": "is_reported" }
使用以下flask.args:
{ "group_id": 1, "is_reported": "true }
返回的filter_字符串如下所示。在
tenant_id:1 AND is_reported:true
库使用示例:
fromalgoliasearch.search_clientimportSearchClientfromalgoliaqbimportAlgoliaQueryBuilderfromflaskimportrequestaqb=AlgoliaQueryBuilder(search_param="search",filter_map={"group_id":"tenant_id","is_reported":"is_reported"})# Extract the search query from our flask apps request.args var.search_query=aqb.get_search_query(request.args)# Get the filter query from our request args.filter_query=aqb.get_filter_query(request.args)# Now that we have the filter string, we just pass it into the search function.search_client=SearchClient()index=search_client.init_index("contacts")results=index.search(search_query,{"filters":filter_query})
- 项目
标签: