HybridSearchRequest.cpp 5.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
// or implied. See the License for the specific language governing permissions and limitations under the License.

#include "server/delivery/hybrid_request/HybridSearchRequest.h"
#include "db/Utils.h"
#include "server/DBWrapper.h"
#include "utils/CommonUtil.h"
#include "utils/Log.h"
#include "utils/TimeRecorder.h"
#include "utils/ValidationUtil.h"

#include <fiu-local.h>
#include <memory>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
F
feisiyicl 已提交
26
#ifdef ENABLE_CPU_PROFILING
27 28 29 30 31 32 33 34
#include <gperftools/profiler.h>
#endif

namespace milvus {
namespace server {

HybridSearchRequest::HybridSearchRequest(const std::shared_ptr<milvus::server::Context>& context,
                                         const std::string& collection_name, std::vector<std::string>& partition_list,
Y
yukun 已提交
35 36 37
                                         query::GeneralQueryPtr& general_query, query::QueryPtr& query_ptr,
                                         milvus::json& json_params, std::vector<std::string>& field_names,
                                         engine::QueryResult& result)
38 39 40 41
    : BaseRequest(context, BaseRequest::kHybridSearch),
      collection_name_(collection_name),
      partition_list_(partition_list),
      general_query_(general_query),
Y
yukun 已提交
42
      query_ptr_(query_ptr),
Y
yukun 已提交
43
      field_names_(field_names),
44 45 46 47
      result_(result) {
}

BaseRequestPtr
Y
yukun 已提交
48 49 50 51 52 53
HybridSearchRequest::Create(const std::shared_ptr<milvus::server::Context>& context, const std::string& collection_name,
                            std::vector<std::string>& partition_list, query::GeneralQueryPtr& general_query,
                            query::QueryPtr& query_ptr, milvus::json& json_params,
                            std::vector<std::string>& field_names, engine::QueryResult& result) {
    return std::shared_ptr<BaseRequest>(new HybridSearchRequest(context, collection_name, partition_list, general_query,
                                                                query_ptr, json_params, field_names, result));
54 55 56 57 58
}

Status
HybridSearchRequest::OnExecute() {
    try {
B
BossZou 已提交
59
        fiu_do_on("HybridSearchRequest.OnExecute.throw_std_exception", throw std::exception());
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
        std::string hdr = "SearchRequest(table=" + collection_name_;

        TimeRecorder rc(hdr);

        // step 1: check table name
        auto status = ValidationUtil::ValidateCollectionName(collection_name_);
        if (!status.ok()) {
            return status;
        }

        // step 2: check table existence
        // only process root table, ignore partition table
        engine::meta::CollectionSchema collection_schema;
        engine::meta::hybrid::FieldsSchema fields_schema;
        collection_schema.collection_id_ = collection_name_;
        status = DBWrapper::DB()->DescribeHybridCollection(collection_schema, fields_schema);
B
BossZou 已提交
76 77
        fiu_do_on("HybridSearchRequest.OnExecute.describe_table_fail",
                  status = Status(milvus::SERVER_UNEXPECTED_ERROR, ""));
78 79 80 81 82 83 84 85 86 87 88 89 90
        if (!status.ok()) {
            if (status.code() == DB_NOT_FOUND) {
                return Status(SERVER_COLLECTION_NOT_EXIST, CollectionNotExistMsg(collection_name_));
            } else {
                return status;
            }
        } else {
            if (!collection_schema.owner_collection_.empty()) {
                return Status(SERVER_INVALID_COLLECTION_NAME, CollectionNotExistMsg(collection_name_));
            }
        }

        std::unordered_map<std::string, engine::meta::hybrid::DataType> attr_type;
Y
yukun 已提交
91
        for (auto& field_schema : fields_schema.fields_schema_) {
92
            attr_type.insert(
Y
yukun 已提交
93
                std::make_pair(field_schema.field_name_, (engine::meta::hybrid::DataType)field_schema.field_type_));
94 95
        }

Y
yukun 已提交
96 97 98 99 100 101 102 103 104 105 106
        if (json_params.contains("field_names")) {
            if (json_params["field_names"].is_array()) {
                for (auto& name : json_params["field_names"]) {
                    field_names_.emplace_back(name.get<std::string>());
                }
            }
        } else {
            for (auto& field_schema : fields_schema.fields_schema_) {
                field_names_.emplace_back(field_schema.field_name_);
            }
        }
107

Y
yukun 已提交
108 109
        status = DBWrapper::DB()->HybridQuery(context_, collection_name_, partition_list_, general_query_, query_ptr_,
                                              field_names_, attr_type, result_);
110

F
feisiyicl 已提交
111
#ifdef ENABLE_CPU_PROFILING
112 113 114
        ProfilerStop();
#endif

B
BossZou 已提交
115
        fiu_do_on("HybridSearchRequest.OnExecute.query_fail", status = Status(milvus::SERVER_UNEXPECTED_ERROR, ""));
116 117 118
        if (!status.ok()) {
            return status;
        }
Y
yukun 已提交
119 120
        fiu_do_on("HybridSearchRequest.OnExecute.empty_result_ids", result_.result_ids_.clear());
        if (result_.result_ids_.empty()) {
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
            return Status::OK();  // empty table
        }

        auto post_query_ctx = context_->Child("Constructing result");

        // step 7: construct result array
        post_query_ctx->GetTraceContext()->GetSpan()->Finish();

        // step 8: print time cost percent
        rc.RecordSection("construct result and send");
        rc.ElapseFromBegin("totally cost");
    } catch (std::exception& ex) {
        return Status(SERVER_UNEXPECTED_ERROR, ex.what());
    }

    return Status::OK();
}

}  // namespace server
}  // namespace milvus