index_options.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. "fmt"
  19. "github.com/milvus-io/milvus-proto/go-api/v2/milvuspb"
  20. "github.com/milvus-io/milvus/client/v2/entity"
  21. "github.com/milvus-io/milvus/client/v2/index"
  22. )
  23. type CreateIndexOption interface {
  24. Request() *milvuspb.CreateIndexRequest
  25. }
  26. type createIndexOption struct {
  27. collectionName string
  28. fieldName string
  29. indexName string
  30. indexDef index.Index
  31. extraParams map[string]any
  32. }
  33. func (opt *createIndexOption) WithExtraParam(key string, value any) {
  34. opt.extraParams[key] = value
  35. }
  36. func (opt *createIndexOption) Request() *milvuspb.CreateIndexRequest {
  37. params := opt.indexDef.Params()
  38. for key, value := range opt.extraParams {
  39. params[key] = fmt.Sprintf("%v", value)
  40. }
  41. req := &milvuspb.CreateIndexRequest{
  42. CollectionName: opt.collectionName,
  43. FieldName: opt.fieldName,
  44. IndexName: opt.indexName,
  45. ExtraParams: entity.MapKvPairs(params),
  46. }
  47. return req
  48. }
  49. func (opt *createIndexOption) WithIndexName(indexName string) *createIndexOption {
  50. opt.indexName = indexName
  51. return opt
  52. }
  53. func NewCreateIndexOption(collectionName string, fieldName string, index index.Index) *createIndexOption {
  54. return &createIndexOption{
  55. collectionName: collectionName,
  56. fieldName: fieldName,
  57. indexDef: index,
  58. extraParams: make(map[string]any),
  59. }
  60. }
  61. type ListIndexOption interface {
  62. Request() *milvuspb.DescribeIndexRequest
  63. Matches(*milvuspb.IndexDescription) bool
  64. }
  65. var _ ListIndexOption = (*listIndexOption)(nil)
  66. type listIndexOption struct {
  67. collectionName string
  68. fieldName string
  69. }
  70. func (opt *listIndexOption) WithFieldName(fieldName string) *listIndexOption {
  71. opt.fieldName = fieldName
  72. return opt
  73. }
  74. func (opt *listIndexOption) Matches(idxDef *milvuspb.IndexDescription) bool {
  75. return opt.fieldName == "" || idxDef.GetFieldName() == opt.fieldName
  76. }
  77. func (opt *listIndexOption) Request() *milvuspb.DescribeIndexRequest {
  78. return &milvuspb.DescribeIndexRequest{
  79. CollectionName: opt.collectionName,
  80. FieldName: opt.fieldName,
  81. }
  82. }
  83. func NewListIndexOption(collectionName string) *listIndexOption {
  84. return &listIndexOption{
  85. collectionName: collectionName,
  86. }
  87. }
  88. type DescribeIndexOption interface {
  89. Request() *milvuspb.DescribeIndexRequest
  90. }
  91. type describeIndexOption struct {
  92. collectionName string
  93. fieldName string
  94. indexName string
  95. }
  96. func (opt *describeIndexOption) Request() *milvuspb.DescribeIndexRequest {
  97. return &milvuspb.DescribeIndexRequest{
  98. CollectionName: opt.collectionName,
  99. IndexName: opt.indexName,
  100. }
  101. }
  102. func NewDescribeIndexOption(collectionName string, indexName string) *describeIndexOption {
  103. return &describeIndexOption{
  104. collectionName: collectionName,
  105. indexName: indexName,
  106. }
  107. }
  108. type DropIndexOption interface {
  109. Request() *milvuspb.DropIndexRequest
  110. }
  111. type dropIndexOption struct {
  112. collectionName string
  113. indexName string
  114. }
  115. func (opt *dropIndexOption) Request() *milvuspb.DropIndexRequest {
  116. return &milvuspb.DropIndexRequest{
  117. CollectionName: opt.collectionName,
  118. IndexName: opt.indexName,
  119. }
  120. }
  121. func NewDropIndexOption(collectionName string, indexName string) *dropIndexOption {
  122. return &dropIndexOption{
  123. collectionName: collectionName,
  124. indexName: indexName,
  125. }
  126. }