Exemple #1
0
def suggest_user(name, limit=10, offset=None):
    api_assert(limit is None or limit <= 100, "Requesting too many users")

    query = {
        "suggest": {
            "suggest": {
                "text": (name or "").lower(),
                "completion": {
                    "field": "suggest",
                    "size": limit
                },
            }
        },
    }

    index_name = ES_CONFIG["users"]["index_name"]

    result = None
    try:
        # print '\n--ES latest hosted_index %s\n' % hosted_index
        result = get_hosted_es().search(index=index_name, body=query)
    except Exception as e:
        LOG.info(e)
    finally:
        if result is None:
            result = {}

    options = next(iter(result.get("suggest", {}).get("suggest", [])),
                   {}).get("options", [])
    users = [{
        "id": option.get("_source", {}).get("id"),
        "username": option.get("_source", {}).get("username"),
        "fullname": option.get("_source", {}).get("fullname"),
    } for option in options]
    return users
Exemple #2
0
def _get_matching_objects(query, index_name, doc_type, get_count=False):
    result = None
    try:
        result = get_hosted_es().search(index_name, doc_type, body=query)
    except Exception as e:
        LOG.warning("Got ElasticSearch exception: \n " + str(e))

    if result is None:
        LOG.debug("No Elasticsearch attempt succeeded")
        result = {}
    return _parse_results(result, get_count)
Exemple #3
0
def get_matching_suggestions(query: Union[str, Dict], index_name: str):
    result = None
    try:
        result = get_hosted_es().search(index=index_name, body=query)
    except Exception as e:
        LOG.warning(e)
    finally:
        if result is None:
            result = {}

    options = next(iter(result.get("suggest", {}).get("suggest", [])),
                   {}).get("options", [])

    return options
Exemple #4
0
def suggest_tables(metastore_id, prefix, limit=10):
    verify_metastore_permission(metastore_id)
    # Unfortuantely currently we can't search including underscore,
    # so split. # TODO: Allow for both.
    # parsed_keywords = map(lambda x: re.split('(-|_)', x), keywords)
    query = {
        "suggest": {
            "suggest": {
                "text": prefix,
                "completion": {
                    "field": "completion_name",
                    "size": limit,
                    "contexts": {
                        "metastore_id": metastore_id
                    },
                },
            }
        },
    }

    index_name = ES_CONFIG["tables"]["index_name"]
    type_name = ES_CONFIG["tables"]["type_name"]

    result = None
    try:
        # print '\n--ES latest hosted_index %s\n' % hosted_index
        result = get_hosted_es().search(index_name, type_name, body=query)
    except Exception as e:
        LOG.info(e)
    finally:
        if result is None:
            result = {}
    options = next(iter(result.get("suggest", {}).get("suggest", [])),
                   {}).get("options", [])
    texts = [
        "{}.{}".format(
            option.get("_source", {}).get("schema", ""),
            option.get("_source", {}).get("name", ""),
        ) for option in options
    ]
    return {"data": texts}
Exemple #5
0
def suggest_tables(metastore_id, prefix, limit=10):
    verify_metastore_permission(metastore_id)

    query = {
        "suggest": {
            "suggest": {
                "text": prefix,
                "completion": {
                    "field": "completion_name",
                    "size": limit,
                    "contexts": {
                        "metastore_id": metastore_id
                    },
                },
            }
        },
    }

    index_name = ES_CONFIG["tables"]["index_name"]
    type_name = ES_CONFIG["tables"]["type_name"]

    result = None
    try:
        result = get_hosted_es().search(index_name, type_name, body=query)
    except Exception as e:
        LOG.info(e)
    finally:
        if result is None:
            result = {}
    options = next(iter(result.get("suggest", {}).get("suggest", [])),
                   {}).get("options", [])
    texts = [
        "{}.{}".format(
            option.get("_source", {}).get("schema", ""),
            option.get("_source", {}).get("name", ""),
        ) for option in options
    ]
    return {"data": texts}