MilvusApi.h 12.7 KB
Newer Older
J
jinhai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you 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.

J
jinhai 已提交
18 19 20 21
#pragma once

#include "Status.h"

S
starlord 已提交
22
#include <memory>
J
jinhai 已提交
23 24 25
#include <string>
#include <vector>

G
groot 已提交
26
/** \brief Milvus SDK namespace
J
jinhai 已提交
27
 */
G
groot 已提交
28
namespace milvus {
J
jinhai 已提交
29 30 31 32 33

/**
 * @brief Index Type
 */
enum class IndexType {
Y
yudong.cai 已提交
34 35 36 37
    INVALID = 0,
    FLAT = 1,
    IVFFLAT = 2,
    IVFSQ8 = 3,
Y
Yukikaze-CZR 已提交
38
    RNSG = 4,
Y
yudong.cai 已提交
39
    IVFSQ8H = 5,
40 41 42
    IVFPQ = 6,
    SPTAGKDT = 7,
    SPTAGBKT = 8,
J
jinhai 已提交
43 44
};

S
starlord 已提交
45
enum class MetricType {
G
groot 已提交
46 47 48 49 50
    L2 = 1,         // Euclidean Distance
    IP = 2,         // Cosine Similarity
    HAMMING = 3,    // Hamming Distance
    JACCARD = 4,    // Jaccard Distance
    TANIMOTO = 5,   // Tanimoto Distance
S
starlord 已提交
51 52
};

J
jinhai 已提交
53 54 55 56
/**
 * @brief Connect API parameter
 */
struct ConnectParam {
S
starlord 已提交
57 58
    std::string ip_address;  ///< Server IP address
    std::string port;        ///< Server PORT
J
jinhai 已提交
59 60 61 62 63 64
};

/**
 * @brief Table Schema
 */
struct TableSchema {
S
starlord 已提交
65 66 67 68
    std::string table_name;                   ///< Table name
    int64_t dimension = 0;                    ///< Vector dimension, must be a positive value
    int64_t index_file_size = 0;              ///< Index file size, must be a positive value
    MetricType metric_type = MetricType::L2;  ///< Index metric type
J
jinhai 已提交
69 70 71 72
};

/**
 * @brief Range information
G
groot 已提交
73
 * for DATE range, the format is like: 'year-month-day'
J
jinhai 已提交
74 75
 */
struct Range {
S
starlord 已提交
76 77
    std::string start_value;  ///< Range start
    std::string end_value;    ///< Range stop
J
jinhai 已提交
78 79 80 81 82 83
};

/**
 * @brief Record inserted
 */
struct RowRecord {
G
groot 已提交
84 85
    std::vector<float> float_data;  ///< Vector raw float data
    std::vector<uint8_t> binary_data;  ///< Vector raw binary data
J
jinhai 已提交
86 87 88 89 90
};

/**
 * @brief TopK query result
 */
91
struct QueryResult {
F
fishpenguin 已提交
92 93
    std::vector<int64_t> ids;      ///< Query ids result
    std::vector<float> distances;  ///< Query distances result
J
jinhai 已提交
94
};
95
using TopKQueryResult = std::vector<QueryResult>;  ///< Topk query result
J
jinhai 已提交
96

Y
Yu Kun 已提交
97 98 99 100
/**
 * @brief index parameters
 */
struct IndexParam {
101 102 103
    std::string table_name;  ///< Table name for create index
    IndexType index_type;    ///< Create index type
    int32_t nlist;           ///< Index nlist
Y
Yu Kun 已提交
104
};
G
groot 已提交
105

G
groot 已提交
106 107 108 109 110 111 112 113 114 115 116
/**
 * @brief partition parameters
 */
struct PartitionParam {
    std::string table_name;
    std::string partition_name;
    std::string partition_tag;
};

using PartitionList = std::vector<PartitionParam>;

J
jinhai 已提交
117 118 119 120
/**
 * @brief SDK main class
 */
class Connection {
K
kun yu 已提交
121
 public:
J
jinhai 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
    /**
     * @brief CreateConnection
     *
     * Create a connection instance and return it's shared pointer
     *
     * @return Connection instance pointer
     */

    static std::shared_ptr<Connection>
    Create();

    /**
     * @brief DestroyConnection
     *
     * Destroy the connection instance
     *
     * @param connection, the shared pointer to the instance to be destroyed
     *
     * @return if destroy is successful
     */

    static Status
S
starlord 已提交
144
    Destroy(std::shared_ptr<Connection>& connection_ptr);
J
jinhai 已提交
145 146 147 148

    /**
     * @brief Connect
     *
149 150
     * This method is used to connect server.
     * Connect function should be called before any operations.
J
jinhai 已提交
151 152 153 154 155 156
     *
     * @param param, use to provide server information
     *
     * @return Indicate if connect is successful
     */

Y
Yu Kun 已提交
157
    virtual Status
S
starlord 已提交
158
    Connect(const ConnectParam& param) = 0;
J
jinhai 已提交
159 160 161 162

    /**
     * @brief Connect
     *
163 164
     * This method is used to connect server.
     * Connect function should be called before any operations.
J
jinhai 已提交
165
     *
166
     * @param uri, use to provide server uri, example: milvus://ipaddress:port
J
jinhai 已提交
167 168 169
     *
     * @return Indicate if connect is successful
     */
Y
Yu Kun 已提交
170
    virtual Status
S
starlord 已提交
171
    Connect(const std::string& uri) = 0;
J
jinhai 已提交
172 173 174 175

    /**
     * @brief connected
     *
176
     * This method is used to test whether server is connected.
J
jinhai 已提交
177 178 179
     *
     * @return Indicate if connection status
     */
Y
Yu Kun 已提交
180 181
    virtual Status
    Connected() const = 0;
J
jinhai 已提交
182 183 184 185

    /**
     * @brief Disconnect
     *
186
     * This method is used to disconnect server.
J
jinhai 已提交
187 188 189
     *
     * @return Indicate if disconnect is successful
     */
Y
Yu Kun 已提交
190 191
    virtual Status
    Disconnect() = 0;
J
jinhai 已提交
192 193 194 195

    /**
     * @brief Create table method
     *
196
     * This method is used to create table.
J
jinhai 已提交
197 198 199 200 201
     *
     * @param param, use to provide table information to be created.
     *
     * @return Indicate if table is created successfully
     */
Y
Yu Kun 已提交
202
    virtual Status
S
starlord 已提交
203
    CreateTable(const TableSchema& param) = 0;
J
jinhai 已提交
204

G
groot 已提交
205 206 207
    /**
     * @brief Test table existence method
     *
208
     * This method is used to create table.
G
groot 已提交
209
     *
G
groot 已提交
210
     * @param table_name, target table's name.
G
groot 已提交
211 212 213
     *
     * @return Indicate if table is cexist
     */
Y
Yu Kun 已提交
214
    virtual bool
S
starlord 已提交
215
    HasTable(const std::string& table_name) = 0;
G
groot 已提交
216

J
jinhai 已提交
217
    /**
218
     * @brief Drop table method
J
jinhai 已提交
219
     *
220
     * This method is used to drop table(and its partitions).
J
jinhai 已提交
221
     *
G
groot 已提交
222
     * @param table_name, target table's name.
J
jinhai 已提交
223
     *
224
     * @return Indicate if table is drop successfully.
J
jinhai 已提交
225
     */
Y
Yu Kun 已提交
226
    virtual Status
S
starlord 已提交
227
    DropTable(const std::string& table_name) = 0;
K
kun yu 已提交
228

229
    /**
Y
Yu Kun 已提交
230
     * @brief Create index method
231
     *
G
groot 已提交
232
     * This method is used to create index for whole table(and its partitions).
233
     *
Y
Yu Kun 已提交
234 235 236 237 238
     * @param IndexParam
     *  table_name, table name is going to be create index.
     *  index type,
     *  nlist,
     *  index file size
239 240 241
     *
     * @return Indicate if build index successfully.
     */
Y
Yu Kun 已提交
242
    virtual Status
S
starlord 已提交
243
    CreateIndex(const IndexParam& index_param) = 0;
244

J
jinhai 已提交
245
    /**
246
     * @brief Insert vector to table
J
jinhai 已提交
247
     *
248
     * This method is used to insert vector array to table.
J
jinhai 已提交
249
     *
G
groot 已提交
250 251
     * @param table_name, target table's name.
     * @param partition_tag, target partition's tag, keep empty if no partition.
J
jinhai 已提交
252
     * @param record_array, vector array is inserted.
253 254 255 256
     * @param id_array,
     *  specify id for each vector,
     *  if this array is empty, milvus will generate unique id for each vector,
     *  and return all ids by this parameter.
J
jinhai 已提交
257 258 259
     *
     * @return Indicate if vector array are inserted successfully
     */
Y
Yu Kun 已提交
260
    virtual Status
G
groot 已提交
261
    Insert(const std::string& table_name, const std::string& partition_tag, const std::vector<RowRecord>& record_array,
S
starlord 已提交
262
           std::vector<int64_t>& id_array) = 0;
J
jinhai 已提交
263 264 265 266 267 268

    /**
     * @brief Search vector
     *
     * This method is used to query vector in table.
     *
269 270
     * @param table_name, target table's name.
     * @param partition_tags, target partitions, keep empty if no partition.
J
jinhai 已提交
271
     * @param query_record_array, all vector are going to be queried.
272
     * @param query_range_array, [deprecated] time ranges, if not specified, will search in whole table
J
jinhai 已提交
273
     * @param topk, how many similarity vectors will be searched.
274
     * @param nprobe, the number of centroids choose to search.
G
groot 已提交
275
     * @param topk_query_result_array, result array.
J
jinhai 已提交
276 277 278
     *
     * @return Indicate if query is successful.
     */
Y
Yu Kun 已提交
279
    virtual Status
G
groot 已提交
280
    Search(const std::string& table_name, const std::vector<std::string>& partition_tags,
G
groot 已提交
281
           const std::vector<RowRecord>& query_record_array, const std::vector<Range>& query_range_array, int64_t topk,
G
groot 已提交
282
           int64_t nprobe, TopKQueryResult& topk_query_result) = 0;
J
jinhai 已提交
283 284 285 286 287 288

    /**
     * @brief Show table description
     *
     * This method is used to show table information.
     *
G
groot 已提交
289
     * @param table_name, target table's name.
J
jinhai 已提交
290 291 292 293
     * @param table_schema, table_schema is given when operation is successful.
     *
     * @return Indicate if this operation is successful.
     */
Y
Yu Kun 已提交
294
    virtual Status
S
starlord 已提交
295
    DescribeTable(const std::string& table_name, TableSchema& table_schema) = 0;
J
jinhai 已提交
296

G
groot 已提交
297 298 299 300 301
    /**
     * @brief Get table row count
     *
     * This method is used to get table row count.
     *
G
groot 已提交
302 303
     * @param table_name, target table's name.
     * @param row_count, table total row count(including partitions).
G
groot 已提交
304 305 306
     *
     * @return Indicate if this operation is successful.
     */
Y
Yu Kun 已提交
307
    virtual Status
S
starlord 已提交
308
    CountTable(const std::string& table_name, int64_t& row_count) = 0;
G
groot 已提交
309

J
jinhai 已提交
310 311 312 313 314
    /**
     * @brief Show all tables in database
     *
     * This method is used to list all tables.
     *
315
     * @param table_array, all tables in database.
J
jinhai 已提交
316 317 318
     *
     * @return Indicate if this operation is successful.
     */
Y
Yu Kun 已提交
319
    virtual Status
S
starlord 已提交
320
    ShowTables(std::vector<std::string>& table_array) = 0;
J
jinhai 已提交
321 322 323 324 325 326 327 328

    /**
     * @brief Give the client version
     *
     * This method is used to give the client version.
     *
     * @return Client version.
     */
Y
Yu Kun 已提交
329 330
    virtual std::string
    ClientVersion() const = 0;
J
jinhai 已提交
331 332 333 334 335 336 337 338

    /**
     * @brief Give the server version
     *
     * This method is used to give the server version.
     *
     * @return Server version.
     */
Y
Yu Kun 已提交
339 340
    virtual std::string
    ServerVersion() const = 0;
J
jinhai 已提交
341 342 343 344 345 346 347 348

    /**
     * @brief Give the server status
     *
     * This method is used to give the server status.
     *
     * @return Server status.
     */
Y
Yu Kun 已提交
349 350
    virtual std::string
    ServerStatus() const = 0;
Y
Yu Kun 已提交
351

G
groot 已提交
352 353 354 355 356
    /**
     * @brief dump server tasks information
     *
     * This method is internal used.
     *
357
     * @return Task information in tasktables.
G
groot 已提交
358
     */
Y
Yu Kun 已提交
359 360 361
    virtual std::string
    DumpTaskTables() const = 0;

Y
Yu Kun 已提交
362
    /**
363
     * [deprecated]
G
groot 已提交
364
     * @brief delete tables by date range
Y
Yu Kun 已提交
365
     *
G
groot 已提交
366
     * This method is used to delete table data by date range.
Y
Yu Kun 已提交
367
     *
G
groot 已提交
368
     * @param table_name, target table's name.
Y
Yu Kun 已提交
369 370 371 372 373
     * @param Range, table range to delete.
     *
     * @return Indicate if this operation is successful.
     */
    virtual Status
G
groot 已提交
374
    DeleteByDate(const std::string& table_name, const Range& range) = 0;
Y
Yu Kun 已提交
375 376 377 378 379 380 381 382 383 384 385

    /**
     * @brief preload table
     *
     * This method is used to preload table
     *
     * @param table_name
     *
     * @return Indicate if this operation is successful.
     */
    virtual Status
S
starlord 已提交
386
    PreloadTable(const std::string& table_name) const = 0;
Y
Yu Kun 已提交
387 388 389 390 391 392

    /**
     * @brief describe index
     *
     * This method is used to describe index
     *
G
groot 已提交
393 394
     * @param table_name, target table's name.
     * @param index_param, returned index information.
Y
Yu Kun 已提交
395
     *
G
groot 已提交
396
     * @return Indicate if this operation is successful.
Y
Yu Kun 已提交
397
     */
398
    virtual Status
S
starlord 已提交
399
    DescribeIndex(const std::string& table_name, IndexParam& index_param) const = 0;
Y
Yu Kun 已提交
400 401 402 403

    /**
     * @brief drop index
     *
G
groot 已提交
404
     * This method is used to drop index of table(and its partitions)
Y
Yu Kun 已提交
405
     *
G
groot 已提交
406
     * @param table_name, target table's name.
Y
Yu Kun 已提交
407 408 409 410
     *
     * @return Indicate if this operation is successful.
     */
    virtual Status
S
starlord 已提交
411
    DropIndex(const std::string& table_name) const = 0;
G
groot 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450

    /**
     * @brief Create partition method
     *
     * This method is used to create table partition
     *
     * @param param, use to provide partition information to be created.
     *
     * @return Indicate if partition is created successfully
     */
    virtual Status
    CreatePartition(const PartitionParam& param) = 0;

    /**
     * @brief Test table existence method
     *
     * This method is used to create table
     *
     * @param table_name, table name is going to be tested.
     * @param partition_array, partition array of the table.
     *
     * @return Indicate if this operation is successful
     */
    virtual Status
    ShowPartitions(const std::string& table_name, PartitionList& partition_array) const = 0;

    /**
     * @brief Delete partition method
     *
     * This method is used to delete table partition.
     *
     * @param param, target partition to be deleted.
     *      NOTE: if param.table_name is empty, you must specify param.partition_name,
     *          else you can specify param.table_name and param.tag and let the param.partition_name be empty
     *
     * @return Indicate if partition is delete successfully.
     */
    virtual Status
    DropPartition(const PartitionParam& param) = 0;
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476

    /**
     * @brief Get config method
     *
     * This method is used to set config.
     *
     * @param node_name, config node name.
     * @param value, config value.
     *
     * @return Indicate if this operation is successful.
     */
    virtual Status
    GetConfig(const std::string& node_name, std::string& value) const = 0;

    /**
     * @brief Set config method
     *
     * This method is used to set config.
     *
     * @param node_name, config node name.
     * @param value, config value.
     *
     * @return Indicate if this operation is successful.
     */
    virtual Status
    SetConfig(const std::string& node_name, const std::string& value) const = 0;
J
jinhai 已提交
477 478
};

S
starlord 已提交
479
}  // namespace milvus