collection_options.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // Licensed to the LF AI & Data foundation under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. package client
  17. import (
  18. "google.golang.org/protobuf/proto"
  19. "github.com/milvus-io/milvus-proto/go-api/v2/commonpb"
  20. "github.com/milvus-io/milvus-proto/go-api/v2/milvuspb"
  21. "github.com/milvus-io/milvus/client/v2/entity"
  22. "github.com/milvus-io/milvus/client/v2/index"
  23. )
  24. // CreateCollectionOption is the interface builds CreateCollectionRequest.
  25. type CreateCollectionOption interface {
  26. // Request is the method returns the composed request.
  27. Request() *milvuspb.CreateCollectionRequest
  28. // Indexes is the method returns IndexOption to create
  29. Indexes() []CreateIndexOption
  30. IsFast() bool
  31. }
  32. // createCollectionOption contains all the parameters to create collection.
  33. type createCollectionOption struct {
  34. name string
  35. shardNum int32
  36. // fast create collection params
  37. varcharPK bool
  38. varcharPKMaxLength int
  39. pkFieldName string
  40. vectorFieldName string
  41. dim int64
  42. autoID bool
  43. enabledDynamicSchema bool
  44. // advanced create collection params
  45. schema *entity.Schema
  46. consistencyLevel entity.ConsistencyLevel
  47. properties map[string]string
  48. // partition key
  49. numPartitions int64
  50. // is fast create collection
  51. isFast bool
  52. // fast creation with index
  53. metricType entity.MetricType
  54. }
  55. func (opt *createCollectionOption) WithAutoID(autoID bool) *createCollectionOption {
  56. opt.autoID = autoID
  57. return opt
  58. }
  59. func (opt *createCollectionOption) WithShardNum(shardNum int32) *createCollectionOption {
  60. opt.shardNum = shardNum
  61. return opt
  62. }
  63. func (opt *createCollectionOption) WithDynamicSchema(dynamicSchema bool) *createCollectionOption {
  64. opt.enabledDynamicSchema = dynamicSchema
  65. return opt
  66. }
  67. func (opt *createCollectionOption) WithVarcharPK(varcharPK bool, maxLen int) *createCollectionOption {
  68. opt.varcharPK = varcharPK
  69. opt.varcharPKMaxLength = maxLen
  70. return opt
  71. }
  72. func (opt *createCollectionOption) Request() *milvuspb.CreateCollectionRequest {
  73. // fast create collection
  74. if opt.isFast {
  75. var pkField *entity.Field
  76. if opt.varcharPK {
  77. pkField = entity.NewField().WithDataType(entity.FieldTypeVarChar).WithMaxLength(int64(opt.varcharPKMaxLength))
  78. } else {
  79. pkField = entity.NewField().WithDataType(entity.FieldTypeInt64)
  80. }
  81. pkField = pkField.WithName(opt.pkFieldName).WithIsPrimaryKey(true).WithIsAutoID(opt.autoID)
  82. opt.schema = entity.NewSchema().
  83. WithName(opt.name).
  84. WithAutoID(opt.autoID).
  85. WithDynamicFieldEnabled(opt.enabledDynamicSchema).
  86. WithField(pkField).
  87. WithField(entity.NewField().WithName(opt.vectorFieldName).WithDataType(entity.FieldTypeFloatVector).WithDim(opt.dim))
  88. }
  89. var schemaBytes []byte
  90. if opt.schema != nil {
  91. schemaProto := opt.schema.ProtoMessage()
  92. schemaBytes, _ = proto.Marshal(schemaProto)
  93. }
  94. return &milvuspb.CreateCollectionRequest{
  95. DbName: "", // reserved fields, not used for now
  96. CollectionName: opt.name,
  97. Schema: schemaBytes,
  98. ShardsNum: opt.shardNum,
  99. ConsistencyLevel: commonpb.ConsistencyLevel(opt.consistencyLevel),
  100. NumPartitions: opt.numPartitions,
  101. Properties: entity.MapKvPairs(opt.properties),
  102. }
  103. }
  104. func (opt *createCollectionOption) Indexes() []CreateIndexOption {
  105. // fast create
  106. if opt.isFast {
  107. return []CreateIndexOption{
  108. NewCreateIndexOption(opt.name, opt.vectorFieldName, index.NewGenericIndex("", map[string]string{})),
  109. }
  110. }
  111. return nil
  112. }
  113. func (opt *createCollectionOption) IsFast() bool {
  114. return opt.isFast
  115. }
  116. // SimpleCreateCollectionOptions returns a CreateCollectionOption with default fast collection options.
  117. func SimpleCreateCollectionOptions(name string, dim int64) *createCollectionOption {
  118. return &createCollectionOption{
  119. name: name,
  120. shardNum: 1,
  121. pkFieldName: "id",
  122. vectorFieldName: "vector",
  123. autoID: true,
  124. dim: dim,
  125. enabledDynamicSchema: true,
  126. consistencyLevel: entity.DefaultConsistencyLevel,
  127. isFast: true,
  128. metricType: entity.COSINE,
  129. }
  130. }
  131. // NewCreateCollectionOption returns a CreateCollectionOption with customized collection schema
  132. func NewCreateCollectionOption(name string, collectionSchema *entity.Schema) *createCollectionOption {
  133. return &createCollectionOption{
  134. name: name,
  135. shardNum: 1,
  136. schema: collectionSchema,
  137. consistencyLevel: entity.DefaultConsistencyLevel,
  138. metricType: entity.COSINE,
  139. }
  140. }
  141. type ListCollectionOption interface {
  142. Request() *milvuspb.ShowCollectionsRequest
  143. }
  144. type listCollectionOption struct{}
  145. func (opt *listCollectionOption) Request() *milvuspb.ShowCollectionsRequest {
  146. return &milvuspb.ShowCollectionsRequest{}
  147. }
  148. func NewListCollectionOption() *listCollectionOption {
  149. return &listCollectionOption{}
  150. }
  151. // DescribeCollectionOption is the interface builds DescribeCollection request.
  152. type DescribeCollectionOption interface {
  153. // Request is the method returns the composed request.
  154. Request() *milvuspb.DescribeCollectionRequest
  155. }
  156. type describeCollectionOption struct {
  157. name string
  158. }
  159. func (opt *describeCollectionOption) Request() *milvuspb.DescribeCollectionRequest {
  160. return &milvuspb.DescribeCollectionRequest{
  161. CollectionName: opt.name,
  162. }
  163. }
  164. // NewDescribeCollectionOption composes a describeCollectionOption with provided collection name.
  165. func NewDescribeCollectionOption(name string) *describeCollectionOption {
  166. return &describeCollectionOption{
  167. name: name,
  168. }
  169. }
  170. // HasCollectionOption is the interface to build DescribeCollectionRequest.
  171. type HasCollectionOption interface {
  172. Request() *milvuspb.DescribeCollectionRequest
  173. }
  174. type hasCollectionOpt struct {
  175. name string
  176. }
  177. func (opt *hasCollectionOpt) Request() *milvuspb.DescribeCollectionRequest {
  178. return &milvuspb.DescribeCollectionRequest{
  179. CollectionName: opt.name,
  180. }
  181. }
  182. func NewHasCollectionOption(name string) HasCollectionOption {
  183. return &hasCollectionOpt{
  184. name: name,
  185. }
  186. }
  187. // The DropCollectionOption interface builds DropCollectionRequest.
  188. type DropCollectionOption interface {
  189. Request() *milvuspb.DropCollectionRequest
  190. }
  191. type dropCollectionOption struct {
  192. name string
  193. }
  194. func (opt *dropCollectionOption) Request() *milvuspb.DropCollectionRequest {
  195. return &milvuspb.DropCollectionRequest{
  196. CollectionName: opt.name,
  197. }
  198. }
  199. func NewDropCollectionOption(name string) *dropCollectionOption {
  200. return &dropCollectionOption{
  201. name: name,
  202. }
  203. }
  204. type RenameCollectionOption interface {
  205. Request() *milvuspb.RenameCollectionRequest
  206. }
  207. type renameCollectionOption struct {
  208. oldCollectionName string
  209. newCollectionName string
  210. }
  211. func (opt *renameCollectionOption) Request() *milvuspb.RenameCollectionRequest {
  212. return &milvuspb.RenameCollectionRequest{
  213. OldName: opt.oldCollectionName,
  214. NewName: opt.newCollectionName,
  215. }
  216. }
  217. func NewRenameCollectionOption(oldName, newName string) *renameCollectionOption {
  218. return &renameCollectionOption{
  219. oldCollectionName: oldName,
  220. newCollectionName: newName,
  221. }
  222. }