async def health_check(request: ONSRequest):
    """
    API to check health of the app (i.e connection status to Elasticsearch)
    :param request:
    :return:
    """
    # Ping elasticsearch to get cluster health
    app: SearchApp = request.app

    client: Elasticsearch = app.elasticsearch.client

    # Send the request
    try:
        health = client.cluster.health()
        if isawaitable(health):
            health = await health

        code = 200 if 'status' in health and health['status'] in [
            'yellow', 'green'
        ] else 500

        if code != 200:
            logger.error(request.request_id,
                         "Healthcheck results in non-200 response",
                         extra={"health": health})
        return json(request, health, code)
    except Exception as e:
        logger.error(request.request_id,
                     "Unable to get Elasticsearch cluster health",
                     exc_info=e)
        body = {"elasticsearch": "unavailable"}
        return json(request, body, 500)
async def spell_check(request: ONSRequest):
    """
    API for spell checking
    :param request:
    :return:
    """
    search_term = request.get_search_term()

    # Get spell checker
    app: SearchApp = request.app
    spell_checker: SpellChecker = app.spell_checker

    # Generate the tokens
    tokens = search_term.split()
    if len(tokens) > 0:
        # Get the result
        result = spell_checker.correct_spelling(tokens)

        # Return the json response
        return json(request, result, 200)

    # No input tokens - raise a 400 BAD_REQUEST
    message = "Found no input tokens in query: %s" % search_term
    logger.error(request.request_id, message)
    return json(request, message, 400)
Exemplo n.º 3
0
async def ons_content_query(request: ONSRequest,
                            list_type: str) -> HTTPResponse:
    """
    Handles content queries to the <list_type> API.
    :param request:
    :param list_type: The list_type to query against (i.e ons, onsdata or onspublications; see api.search.list_type.py)
    :return:
    """
    # Parse list_type to enum
    if ListType.is_list_type(list_type):
        list_type_enum: ListType = ListType.from_str(list_type)
        # Initialise the search engine
        sanic_search_engine = SanicSearchEngine(request.app, SearchEngine,
                                                Index.ONS)

        # Perform the request
        search_result: SearchResult = await sanic_search_engine.content_query(
            request, list_type_enum)

        return json(request, search_result.to_dict(), 200)
    else:
        # Log and return 404
        message = "Received content query request for unknown list type: '{0}'".format(
            list_type)
        logger.error(request.request_id, message)
        return json(request, message, 404)
Exemplo n.º 4
0
async def proxy_query(request: ONSRequest) -> HTTPResponse:
    """
    Proxy an Elasticsearch query through the APP over HTTP.
    TODO: Setup authentication for this route
    :param request:
    :return:
    """
    # Initialise the search engine
    sanic_search_engine = SanicSearchEngine(request.app, SearchEngine,
                                            Index.ONS)

    # Perform the request
    search_result: SearchResult = await sanic_search_engine.proxy(request)

    return json(request, search_result.to_dict(), 200)
Exemplo n.º 5
0
async def ons_departments_query(request: ONSRequest) -> HTTPResponse:
    """
    Handles departments queries to the ONS list type. Note, filtering by list_type does not apply
    to the featured result (hence we only offer an 'ONS' list type for this API).
    :param request:
    :return:
    """
    # Initialise the search engine
    sanic_search_engine = SanicSearchEngine(request.app, SearchEngine,
                                            Index.DEPARTMENTS)

    # Perform the request
    search_result: SearchResult = await sanic_search_engine.departments_query(
        request)

    return json(request, search_result.to_dict(), 200)
Exemplo n.º 6
0
async def ons_counts_query(request: ONSRequest,
                           list_type: str) -> HTTPResponse:
    """
    Handles type counts queries to the <list_type> API.
    :param request:
    :param list_type: Not used for type counts query, but allows route to support multiply list_type queries
    :return:
    """
    # Initialise the search engine
    sanic_search_engine = SanicSearchEngine(request.app, SearchEngine,
                                            Index.ONS)

    # Perform the request
    search_result: SearchResult = await sanic_search_engine.type_counts_query(
        request)

    return json(request, search_result.to_dict(), 200)