github.com/milvus-io/milvus-sdk-go/v2@v2.4.1/client/options.go (about)

     1  // Copyright (C) 2019-2021 Zilliz. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
     4  // with the License. You may obtain a copy of the License at
     5  //
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software distributed under the License
     9  // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
    10  // or implied. See the License for the specific language governing permissions and limitations under the License.
    11  
    12  package client
    13  
    14  import (
    15  	"fmt"
    16  
    17  	"github.com/cockroachdb/errors"
    18  
    19  	"github.com/milvus-io/milvus-proto/go-api/v2/commonpb"
    20  
    21  	"github.com/milvus-io/milvus-proto/go-api/v2/milvuspb"
    22  	"github.com/milvus-io/milvus-sdk-go/v2/entity"
    23  )
    24  
    25  // CreateCollectionOption is an option that is used to alter create collection options.
    26  type CreateCollectionOption func(opt *createCollOpt)
    27  
    28  type createCollOpt struct {
    29  	ConsistencyLevel    entity.ConsistencyLevel
    30  	NumPartitions       int64
    31  	PrimaryKeyFieldName string
    32  	PrimaryKeyFieldType entity.FieldType
    33  	PrimaryKeyMaxLength int64
    34  	VectorFieldName     string
    35  	MetricsType         entity.MetricType
    36  	AutoID              bool
    37  	EnableDynamicSchema bool
    38  	Properties          map[string]string
    39  	MsgBase             *commonpb.MsgBase
    40  }
    41  
    42  func WithPKFieldName(name string) CreateCollectionOption {
    43  	return func(opt *createCollOpt) {
    44  		opt.PrimaryKeyFieldName = name
    45  	}
    46  }
    47  
    48  func WithPKFieldType(tp entity.FieldType) CreateCollectionOption {
    49  	return func(opt *createCollOpt) {
    50  		opt.PrimaryKeyFieldType = tp
    51  	}
    52  }
    53  
    54  func WithPKMaxLength(maxLength int64) CreateCollectionOption {
    55  	return func(opt *createCollOpt) {
    56  		opt.PrimaryKeyMaxLength = maxLength
    57  	}
    58  }
    59  
    60  func WithVectorFieldName(name string) CreateCollectionOption {
    61  	return func(opt *createCollOpt) {
    62  		opt.VectorFieldName = name
    63  	}
    64  }
    65  
    66  func WithMetricsType(mt entity.MetricType) CreateCollectionOption {
    67  	return func(opt *createCollOpt) {
    68  		opt.MetricsType = mt
    69  	}
    70  }
    71  
    72  func WithAutoID(autoID bool) CreateCollectionOption {
    73  	return func(opt *createCollOpt) {
    74  		opt.AutoID = autoID
    75  	}
    76  }
    77  
    78  func WithEnableDynamicSchema(enable bool) CreateCollectionOption {
    79  	return func(opt *createCollOpt) {
    80  		opt.EnableDynamicSchema = enable
    81  	}
    82  }
    83  
    84  // WithConsistencyLevel specifies a specific ConsistencyLevel, rather than using the default ReaderProperties.
    85  func WithConsistencyLevel(cl entity.ConsistencyLevel) CreateCollectionOption {
    86  	return func(opt *createCollOpt) {
    87  		opt.ConsistencyLevel = cl
    88  	}
    89  }
    90  
    91  // WithPartitionNum returns a create collection options to set physical partition number when logical partition feature.
    92  func WithPartitionNum(partitionNums int64) CreateCollectionOption {
    93  	return func(opt *createCollOpt) {
    94  		opt.NumPartitions = partitionNums
    95  	}
    96  }
    97  
    98  func WithCollectionProperty(key, value string) CreateCollectionOption {
    99  	return func(opt *createCollOpt) {
   100  		if opt.Properties == nil {
   101  			opt.Properties = make(map[string]string)
   102  		}
   103  		opt.Properties[key] = value
   104  	}
   105  }
   106  
   107  // LoadCollectionOption is an option that is used to modify LoadCollectionRequest
   108  type LoadCollectionOption func(*milvuspb.LoadCollectionRequest)
   109  
   110  // WithReplicaNumber specifies a specific ReplicaNumber, rather than using the default ReplicaNumber.
   111  func WithReplicaNumber(rn int32) LoadCollectionOption {
   112  	return func(req *milvuspb.LoadCollectionRequest) {
   113  		req.ReplicaNumber = rn
   114  	}
   115  }
   116  
   117  // WithResourceGroups specifies some specific ResourceGroup(s) to load the replica(s), rather than using the default ResourceGroup.
   118  func WithResourceGroups(rgs []string) LoadCollectionOption {
   119  	return func(req *milvuspb.LoadCollectionRequest) {
   120  		req.ResourceGroups = rgs
   121  	}
   122  }
   123  
   124  // SearchQueryOption is an option of search/query request
   125  type SearchQueryOption struct {
   126  	// Consistency Level
   127  	ConsistencyLevel   entity.ConsistencyLevel
   128  	GuaranteeTimestamp uint64
   129  	// Pagination
   130  	Limit  int64
   131  	Offset int64
   132  
   133  	IgnoreGrowing bool
   134  	ForTuning     bool
   135  
   136  	GroupByField string
   137  
   138  	isIterator    bool
   139  	reduceForBest bool
   140  }
   141  
   142  // SearchQueryOptionFunc is a function which modifies SearchOption
   143  type SearchQueryOptionFunc func(option *SearchQueryOption)
   144  
   145  func withIterator() SearchQueryOptionFunc {
   146  	return func(option *SearchQueryOption) {
   147  		option.isIterator = true
   148  	}
   149  }
   150  
   151  func reduceForBest(value bool) SearchQueryOptionFunc {
   152  	return func(option *SearchQueryOption) {
   153  		option.reduceForBest = value
   154  	}
   155  }
   156  
   157  func WithForTuning() SearchQueryOptionFunc {
   158  	return func(option *SearchQueryOption) {
   159  		option.ForTuning = true
   160  	}
   161  }
   162  
   163  func WithIgnoreGrowing() SearchQueryOptionFunc {
   164  	return func(option *SearchQueryOption) {
   165  		option.IgnoreGrowing = true
   166  	}
   167  }
   168  
   169  // WithOffset returns search/query option with offset.
   170  func WithOffset(offset int64) SearchQueryOptionFunc {
   171  	return func(option *SearchQueryOption) {
   172  		option.Offset = offset
   173  	}
   174  }
   175  
   176  // WithLimit returns search/query option with limit.
   177  func WithLimit(limit int64) SearchQueryOptionFunc {
   178  	return func(option *SearchQueryOption) {
   179  		option.Limit = limit
   180  	}
   181  }
   182  
   183  func WithGroupByField(groupByField string) SearchQueryOptionFunc {
   184  	return func(option *SearchQueryOption) {
   185  		option.GroupByField = groupByField
   186  	}
   187  }
   188  
   189  // WithSearchQueryConsistencyLevel specifies consistency level
   190  func WithSearchQueryConsistencyLevel(cl entity.ConsistencyLevel) SearchQueryOptionFunc {
   191  	return func(option *SearchQueryOption) {
   192  		option.ConsistencyLevel = cl
   193  	}
   194  }
   195  
   196  // WithGuaranteeTimestamp specifies guarantee timestamp
   197  func WithGuaranteeTimestamp(gt uint64) SearchQueryOptionFunc {
   198  	return func(option *SearchQueryOption) {
   199  		option.GuaranteeTimestamp = gt
   200  	}
   201  }
   202  
   203  // Deprecated: time travel is not supported since v2.3.0
   204  func WithTravelTimestamp(_ uint64) SearchQueryOptionFunc {
   205  	return func(option *SearchQueryOption) {}
   206  }
   207  
   208  func makeSearchQueryOption(collName string, opts ...SearchQueryOptionFunc) (*SearchQueryOption, error) {
   209  	opt := &SearchQueryOption{
   210  		ConsistencyLevel: entity.ClBounded, // default
   211  	}
   212  	info, ok := MetaCache.getCollectionInfo(collName)
   213  	if ok {
   214  		opt.ConsistencyLevel = info.ConsistencyLevel
   215  	}
   216  	for _, o := range opts {
   217  		o(opt)
   218  	}
   219  	// sanity-check
   220  	if opt.ConsistencyLevel != entity.ClCustomized && opt.GuaranteeTimestamp != 0 {
   221  		return nil, errors.New("user can only specify guarantee timestamp under customized consistency level")
   222  	}
   223  
   224  	switch opt.ConsistencyLevel {
   225  	case entity.ClStrong:
   226  		opt.GuaranteeTimestamp = StrongTimestamp
   227  	case entity.ClSession:
   228  		ts, ok := MetaCache.getSessionTs(collName)
   229  		if !ok {
   230  			ts = EventuallyTimestamp
   231  		}
   232  		opt.GuaranteeTimestamp = ts
   233  	case entity.ClBounded:
   234  		opt.GuaranteeTimestamp = BoundedTimestamp
   235  	case entity.ClEventually:
   236  		opt.GuaranteeTimestamp = EventuallyTimestamp
   237  	case entity.ClCustomized:
   238  		// respect opt.GuaranteeTimestamp
   239  	}
   240  	return opt, nil
   241  }
   242  
   243  // BulkInsertOption is an option that is used to modify ImportRequest
   244  type BulkInsertOption func(request *milvuspb.ImportRequest)
   245  
   246  // WithStartTs specifies a specific startTs
   247  func WithStartTs(startTs int64) BulkInsertOption {
   248  	return func(req *milvuspb.ImportRequest) {
   249  		optionMap := entity.KvPairsMap(req.GetOptions())
   250  		optionMap["start_ts"] = fmt.Sprint(startTs)
   251  		req.Options = entity.MapKvPairs(optionMap)
   252  	}
   253  }
   254  
   255  // WithEndTs specifies a specific endTs
   256  func WithEndTs(endTs int64) BulkInsertOption {
   257  	return func(req *milvuspb.ImportRequest) {
   258  		optionMap := entity.KvPairsMap(req.GetOptions())
   259  		optionMap["end_ts"] = fmt.Sprint(endTs)
   260  		req.Options = entity.MapKvPairs(optionMap)
   261  	}
   262  }
   263  
   264  // IsBackup specifies it is triggered by backup tool
   265  func IsBackup() BulkInsertOption {
   266  	return func(req *milvuspb.ImportRequest) {
   267  		optionMap := entity.KvPairsMap(req.GetOptions())
   268  		optionMap["backup"] = "true"
   269  		req.Options = entity.MapKvPairs(optionMap)
   270  	}
   271  }
   272  
   273  type getOption struct {
   274  	partitionNames []string
   275  	outputFields   []string
   276  }
   277  
   278  type GetOption func(o *getOption)
   279  
   280  func GetWithPartitions(partionNames ...string) GetOption {
   281  	return func(o *getOption) {
   282  		o.partitionNames = partionNames
   283  	}
   284  }
   285  
   286  func GetWithOutputFields(outputFields ...string) GetOption {
   287  	return func(o *getOption) {
   288  		o.outputFields = outputFields
   289  	}
   290  }
   291  
   292  type listCollectionOpt struct {
   293  	showInMemory bool
   294  }
   295  
   296  type ListCollectionOption func(*listCollectionOpt)
   297  
   298  func WithShowInMemory(value bool) ListCollectionOption {
   299  	return func(opt *listCollectionOpt) {
   300  		opt.showInMemory = value
   301  	}
   302  }
   303  
   304  type DropCollectionOption func(*milvuspb.DropCollectionRequest)
   305  
   306  type ReleaseCollectionOption func(*milvuspb.ReleaseCollectionRequest)
   307  
   308  type FlushOption func(*milvuspb.FlushRequest)
   309  
   310  type CreateDatabaseOption func(*milvuspb.CreateDatabaseRequest)
   311  
   312  type DropDatabaseOption func(*milvuspb.DropDatabaseRequest)
   313  
   314  type DescribeDatabaseOption func(*milvuspb.DescribeDatabaseRequest)
   315  
   316  type ReplicateMessageOption func(*milvuspb.ReplicateMessageRequest)
   317  
   318  type CreatePartitionOption func(*milvuspb.CreatePartitionRequest)
   319  
   320  type DropPartitionOption func(*milvuspb.DropPartitionRequest)
   321  
   322  type LoadPartitionsOption func(*milvuspb.LoadPartitionsRequest)
   323  
   324  type ReleasePartitionsOption func(*milvuspb.ReleasePartitionsRequest)