Skip to content

Instantly share code, notes, and snippets.

@quiiver
Created April 22, 2022 22:02
Show Gist options
  • Save quiiver/fa42357f1683d9b14b4977ec392e36c5 to your computer and use it in GitHub Desktop.
Save quiiver/fa42357f1683d9b14b4977ec392e36c5 to your computer and use it in GitHub Desktop.
import os, json
from fastapi import FastAPI
app = FastAPI()
SUGGEST_FILES_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../data/")
SUGGESTIONS = {}
RESULTS = {}
@app.on_event("startup")
def startup():
global SUGGESTIONS
global RESULTS
with open(os.path.join(SUGGEST_FILES_PATH, "InstantSuggest_Queries_20220125.json")) as queries_file:
SUGGESTIONS = json.load(queries_file).get("mapping")
with open(os.path.join(SUGGEST_FILES_PATH, "InstantSuggest_Results_20220125.json")) as queries_file:
blocks = json.load(queries_file).get("blocks")
for block in blocks:
RESULTS[block.get("id")] = block
@app.get("/search")
def search(q: str):
id = SUGGESTIONS.get(q)
if id != None:
return RESULTS.get(id)
return {"error": "No suggestions available", "query": q}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment