1 | /** |
2 | * Copyright 2021 Alibaba, Inc. and its affiliates. All Rights Reserved. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | |
16 | * \author Haichao.chc |
17 | * \date Oct 2020 |
18 | * \brief Example usage of proxima client |
19 | */ |
20 | |
21 | #include <iostream> |
22 | |
23 | #include "proxima_search_client.h" |
24 | |
25 | using namespace proxima::be; |
26 | |
27 | int main() { |
28 | // Create a client instance |
29 | ProximaSearchClientPtr client = ProximaSearchClient::Create(); |
30 | |
31 | // Try to connect server |
32 | { |
33 | ChannelOptions options("127.0.0.1:16000" ); |
34 | Status status = client->connect(options); |
35 | if (status.code != 0) { |
36 | std::cerr << "Connect server failed. code[" << status.code << "] reason[" |
37 | << status.reason << "]" << std::endl; |
38 | return status.code; |
39 | } |
40 | std::cout << "===>Connect server success." << std::endl; |
41 | } |
42 | |
43 | // Create collection |
44 | { |
45 | // Describe a collection config which |
46 | // set collection name --> "test_collection" |
47 | // set one index column --> "test_column" |
48 | // set two forward columsn --> "fwd_column1" and "fwd_column2" |
49 | CollectionConfig config; |
50 | config.collection_name = "test_collection" ; |
51 | config.forward_columns = {"fwd_column1" , "fwd_column2" }; |
52 | config.index_columns = { |
53 | IndexColumnParam("test_column" , DataType::VECTOR_FP32, 8)}; |
54 | |
55 | Status status = client->create_collection(config); |
56 | if (status.code != 0) { |
57 | std::cerr << "Create collection failed. code[" << status.code |
58 | << "] reason[" << status.reason << "]" << std::endl; |
59 | return status.code; |
60 | } |
61 | std::cout << "===>Create collection success." << std::endl; |
62 | } |
63 | |
64 | // Get collection information |
65 | { |
66 | CollectionInfo collection_info; |
67 | Status status = |
68 | client->describe_collection("test_collection" , &collection_info); |
69 | if (status.code != 0) { |
70 | std::cerr << "Describe collection failed. code[" << status.code |
71 | << "] reason[" << status.reason << "]" << std::endl; |
72 | return status.code; |
73 | } |
74 | |
75 | std::cout << "===>Describe collection success." << std::endl; |
76 | std::cout << "collection_name: " << collection_info.collection_name |
77 | << std::endl; |
78 | std::cout << "collection_status: " << (int)collection_info.collection_status |
79 | << std::endl; |
80 | std::cout << "collection_uuid: " << collection_info.collection_uuid |
81 | << std::endl; |
82 | for (size_t i = 0; i < collection_info.forward_columns.size(); i++) { |
83 | std::cout << "forward_column: " << collection_info.forward_columns[i] |
84 | << std::endl; |
85 | } |
86 | for (size_t i = 0; i < collection_info.index_columns.size(); i++) { |
87 | IndexColumnParam &index_param = collection_info.index_columns[i]; |
88 | std::cout << "index_column: " << index_param.column_name << std::endl; |
89 | std::cout << "index_type: " << (int)index_param.index_type << std::endl; |
90 | std::cout << "data_type: " << (int)index_param.data_type << std::endl; |
91 | std::cout << "dimension: " << index_param.dimension << std::endl; |
92 | } |
93 | } |
94 | |
95 | // Insert records |
96 | { |
97 | WriteRequestPtr write_request = WriteRequest::Create(); |
98 | write_request->set_collection_name("test_collection" ); |
99 | // Set row meta first, just decribe the format of sending records |
100 | // Make sure the sort of forward columns and index columns match |
101 | // upper CollectionConfig's sort. |
102 | write_request->add_forward_columns({"fwd_column1" , "fwd_column2" }); |
103 | write_request->add_index_column("test_column" , DataType::VECTOR_FP32, 8); |
104 | |
105 | for (int i = 0; i < 10; i++) { |
106 | WriteRequest::RowPtr row = write_request->add_row(); |
107 | row->set_primary_key(i); |
108 | row->set_operation_type(OperationType::INSERT); |
109 | row->add_index_value({i + 0.1f, i + 0.2f, i + 0.3f, i + 0.4f, i + 0.5f, |
110 | i + 0.6f, i + 0.7f, i + 0.8f}); // "test_column" |
111 | row->add_forward_value("hello" + std::to_string(i)); // "fwd_column1" |
112 | row->add_forward_value(1); // "fwd_column2" |
113 | } |
114 | |
115 | Status status = client->write(*write_request); |
116 | |
117 | if (status.code != 0) { |
118 | std::cerr << "Write records failed. code[" << status.code << "] reason[" |
119 | << status.reason << "]" << std::endl; |
120 | return status.code; |
121 | } |
122 | |
123 | std::cout << "===>Write records success." << std::endl; |
124 | } |
125 | |
126 | // Get document by key |
127 | { |
128 | GetDocumentRequestPtr get_document_request = GetDocumentRequest::Create(); |
129 | GetDocumentResponsePtr get_document_response = |
130 | GetDocumentResponse::Create(); |
131 | get_document_request->set_collection_name("test_collection" ); |
132 | get_document_request->set_primary_key(0); |
133 | Status status = client->get_document_by_key(*get_document_request, |
134 | get_document_response.get()); |
135 | if (status.code != 0) { |
136 | std::cerr << "Get document by key failed. code[" << status.code |
137 | << "] reason[" << status.reason << "]" << std::endl; |
138 | return status.code; |
139 | } |
140 | std::cout << "===>Get document by key success." << std::endl; |
141 | auto doc = get_document_response->document(); |
142 | std::cout << "doc_key: " << doc->primary_key() << std::endl; |
143 | } |
144 | |
145 | // Query records |
146 | { |
147 | QueryRequestPtr query_request = QueryRequest::Create(); |
148 | QueryResponsePtr query_response = QueryResponse::Create(); |
149 | |
150 | query_request->set_collection_name("test_collection" ); |
151 | QueryRequest::KnnQueryParamPtr knn_param = |
152 | query_request->add_knn_query_param(); |
153 | knn_param->set_column_name("test_column" ); |
154 | knn_param->set_topk(10); |
155 | knn_param->set_features({0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8}); |
156 | |
157 | Status status = client->query(*query_request, query_response.get()); |
158 | if (status.code != 0) { |
159 | std::cerr << "Query records failed. code[" << status.code << "] reason[" |
160 | << status.reason << "]" << std::endl; |
161 | return status.code; |
162 | } |
163 | std::cout << "===>Query records success." << std::endl; |
164 | |
165 | for (size_t i = 0; i < query_response->result_count(); i++) { |
166 | auto result = query_response->result(i); |
167 | for (size_t j = 0; j < result->document_count(); j++) { |
168 | auto doc = result->document(j); |
169 | std::cout << "doc_key: " << doc->primary_key() << std::endl; |
170 | std::cout << "doc_score: " << doc->score() << std::endl; |
171 | |
172 | std::string fwd_val1; |
173 | int fwd_val2; |
174 | doc->get_forward_value("fwd_column1" , &fwd_val1); |
175 | doc->get_forward_value("fwd_column2" , &fwd_val2); |
176 | |
177 | std::cout << "forward count: " << doc->forward_count() << std::endl; |
178 | std::cout << "fwd_column1: " << fwd_val1 << std::endl; |
179 | std::cout << "fwd_column2: " << fwd_val2 << std::endl; |
180 | } |
181 | } |
182 | |
183 | // Print end |
184 | } |
185 | |
186 | // Drop collection |
187 | { |
188 | Status status = client->drop_collection("test_collection" ); |
189 | if (status.code != 0) { |
190 | std::cerr << "Drop collection failed. code[" << status.code << "] reason[" |
191 | << status.reason << "]" << std::endl; |
192 | return status.code; |
193 | } |
194 | |
195 | std::cout << "===>Drop collection success" << std::endl; |
196 | } |
197 | } |
198 | |