示例#1
0
def do_suggest(query_params, suggest_params):
    search_params = suggest_params["search"]
    range_result = _get_predicate_ranges(query_params, search_params)
    if is_result_empty(range_result):
        message = _(u"Either the predicate {0} does not exists or it does not have any rdfs:range defined in the triplestore")
        message = message.format(search_params["target"])
        raise HTTPError(400, message)

    classes = _validate_class_restriction(query_params, range_result)
    graphs = _validate_graph_restriction(query_params, range_result)
    indexes = ["semantica." + uri_to_slug(graph) for graph in graphs]

    search_fields = list(set(_get_search_fields(query_params, suggest_params) + LABEL_PROPERTIES))

    response_params = suggest_params.get("response", {})
    response_fields = _get_response_fields(
        query_params,
        response_params,
        classes,
        LABEL_PROPERTIES)

    # request_body = _build_body_query(
    #     query_params,
    #     search_params,
    #     classes,
    #     search_fields,
    #     response_fields)

    analyze_response = run_analyze(search_params["pattern"], settings.ES_ANALYZER, indexes)
    tokens = analyze_response["tokens"]

    request_body = _build_body_query_compatible_with_uatu_and_es_19_in_envs(
        query_params,
        tokens,
        classes,
        search_fields,
        response_fields,
        search_params["pattern"]
    )

    # Sorting in ES is done using memory. From the docs [1]:
    # "When sorting, the relevant sorted field values are loaded into memory.
    # This means that per shard, there should be enough memory to contain them"
    # Currently Globo.com ES servers don't have enough memory to load all data
    # During the 30th October 2013, a query using sort caused Split-brain and all ES shards were down.
    # Therefore, we need to think twice and use sort cleverly to avoid problems
    # [1] http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html
    assert not "sort" in request_body  # Read comments above

    elasticsearch_result = run_search(request_body, indexes=indexes)
    class_fields = response_params.get("class_fields", [])

    total_items = elasticsearch_result["hits"]["total"]
    if total_items:
        items = _build_items(query_params, elasticsearch_result, LABEL_PROPERTIES, class_fields)
        response = build_json(items, total_items, query_params)
    else:
        response = {}
    return response
示例#2
0
def do_suggest(query_params, suggest_params):
    search_params = suggest_params["search"]
    range_result = _get_predicate_ranges(query_params, search_params)
    if is_result_empty(range_result):
        message = _(
            u"Either the predicate {0} does not exists or it does not have any rdfs:range defined in the triplestore"
        )
        message = message.format(search_params["target"])
        raise HTTPError(400, message)

    classes = _validate_class_restriction(query_params, range_result)
    graphs = _validate_graph_restriction(query_params, range_result)
    indexes = ["semantica." + uri_to_slug(graph) for graph in graphs]

    search_fields = list(
        set(
            _get_search_fields(query_params, suggest_params) +
            LABEL_PROPERTIES))

    response_params = suggest_params.get("response", {})
    response_fields = _get_response_fields(query_params, response_params,
                                           classes, LABEL_PROPERTIES)

    # request_body = _build_body_query(
    #     query_params,
    #     search_params,
    #     classes,
    #     search_fields,
    #     response_fields)

    analyze_response = run_analyze(search_params["pattern"],
                                   settings.ES_ANALYZER, indexes)
    tokens = analyze_response["tokens"]

    request_body = _build_body_query_compatible_with_uatu_and_es_19_in_envs(
        query_params, tokens, classes, search_fields, response_fields,
        search_params["pattern"])

    # Sorting in ES is done using memory. From the docs [1]:
    # "When sorting, the relevant sorted field values are loaded into memory.
    # This means that per shard, there should be enough memory to contain them"
    # Currently Globo.com ES servers don't have enough memory to load all data
    # During the 30th October 2013, a query using sort caused Split-brain and all ES shards were down.
    # Therefore, we need to think twice and use sort cleverly to avoid problems
    # [1] http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html
    assert not "sort" in request_body  # Read comments above

    elasticsearch_result = run_search(request_body, indexes=indexes)
    class_fields = response_params.get("class_fields", [])

    total_items = elasticsearch_result["hits"]["total"]
    if total_items:
        items = _build_items(query_params, elasticsearch_result,
                             LABEL_PROPERTIES, class_fields)
        response = build_json(items, total_items, query_params)
    else:
        response = {}
    return response
示例#3
0
def delete_instance(query_params):
    dependants_result_dict = query_dependants(query_params)

    if not is_result_empty(dependants_result_dict):
        values = [item['dependant']['value'] for item in dependants_result_dict['results']['bindings']]
        str_values = ", ".join(values)
        raise HTTPError(409, log_message=_(u"Cannot exclude instance because of the dependencies: {0}").format(str_values))

    query_result_dict = query_delete(query_params)

    if some_triples_deleted(query_result_dict, query_params['graph_uri']):
        return True
示例#4
0
def get_instance(query_params):
    """
    Given a URI, verify that the type corresponds to the class being passed as a parameter
    Retrieve all properties and objects of this URI (subject)
    """
    if must_retrieve_graph_and_class_uri(query_params):
        query_result_dict = get_class_and_graph(query_params)
        bindings = query_result_dict['results']['bindings']
        query_params["graph_uri"] = extract_graph_uri(bindings)
        query_params["class_uri"] = extract_class_uri(bindings)

    query_result_dict = query_all_properties_and_objects(query_params)

    if is_result_empty(query_result_dict):
        return None
    else:
        class_schema = get_class.get_cached_schema(query_params)
        return assemble_instance_json(query_params, query_result_dict,
                                      class_schema)
示例#5
0
def delete_instance(query_params):
    dependants_result_dict = query_dependants(query_params)

    if not is_result_empty(dependants_result_dict):
        values = [
            item['dependant']['value']
            for item in dependants_result_dict['results']['bindings']
        ]
        str_values = ", ".join(values)
        raise HTTPError(
            409,
            log_message=_(
                u"Cannot exclude instance because of the dependencies: {0}").
            format(str_values))

    query_result_dict = query_delete(query_params)

    if some_triples_deleted(query_result_dict, query_params['graph_uri']):
        return True
示例#6
0
def get_instance(query_params):
    """
    Given a URI, verify that the type corresponds to the class being passed as a parameter
    Retrieve all properties and objects of this URI (subject)
    """
    if must_retrieve_graph_and_class_uri(query_params):
        query_result_dict = get_class_and_graph(query_params)
        bindings = query_result_dict['results']['bindings']
        query_params["graph_uri"] = extract_graph_uri(bindings)
        query_params["class_uri"] = extract_class_uri(bindings)

    query_result_dict = query_all_properties_and_objects(query_params)

    if is_result_empty(query_result_dict):
        return None
    else:
        class_schema = get_class.get_cached_schema(query_params)
        return assemble_instance_json(query_params,
                                      query_result_dict,
                                      class_schema)