github.com/milvus-io/milvus-sdk-go/v2@v2.4.1/entity/index.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 entity
    13  
    14  import common "github.com/milvus-io/milvus-proto/go-api/v2/commonpb"
    15  
    16  //go:generate go run genidx/genidx.go
    17  
    18  // IndexState export index state
    19  type IndexState common.IndexState
    20  
    21  // IndexType index type
    22  type IndexType string
    23  
    24  // MetricType metric type
    25  type MetricType string
    26  
    27  // Index Constants
    28  const (
    29  	Flat       IndexType = "FLAT" //faiss
    30  	BinFlat    IndexType = "BIN_FLAT"
    31  	IvfFlat    IndexType = "IVF_FLAT" //faiss
    32  	BinIvfFlat IndexType = "BIN_IVF_FLAT"
    33  	IvfPQ      IndexType = "IVF_PQ" //faiss
    34  	IvfSQ8     IndexType = "IVF_SQ8"
    35  	HNSW       IndexType = "HNSW"
    36  	IvfHNSW    IndexType = "IVF_HNSW"
    37  	AUTOINDEX  IndexType = "AUTOINDEX"
    38  	DISKANN    IndexType = "DISKANN"
    39  	SCANN      IndexType = "SCANN"
    40  
    41  	GPUIvfFlat IndexType = "GPU_IVF_FLAT"
    42  	GPUIvfPQ   IndexType = "GPU_IVF_PQ"
    43  
    44  	GPUCagra      IndexType = "GPU_CAGRA"
    45  	GPUBruteForce IndexType = "GPU_BRUTE_FORCE"
    46  
    47  	// Sparse
    48  	SparseInverted IndexType = "SPARSE_INVERTED_INDEX"
    49  	SparseWAND     IndexType = "SPARSE_WAND"
    50  
    51  	// DEPRECATED
    52  	Scalar IndexType = ""
    53  
    54  	Trie     IndexType = "Trie"
    55  	Sorted   IndexType = "STL_SORT"
    56  	Inverted IndexType = "INVERTED"
    57  )
    58  
    59  // Metric Constants
    60  const (
    61  	L2             MetricType = "L2"
    62  	IP             MetricType = "IP"
    63  	COSINE         MetricType = "COSINE"
    64  	HAMMING        MetricType = "HAMMING"
    65  	JACCARD        MetricType = "JACCARD"
    66  	TANIMOTO       MetricType = "TANIMOTO"
    67  	SUBSTRUCTURE   MetricType = "SUBSTRUCTURE"
    68  	SUPERSTRUCTURE MetricType = "SUPERSTRUCTURE"
    69  )
    70  
    71  // index param field tag
    72  const (
    73  	tParams     = `params`
    74  	tIndexType  = `index_type`
    75  	tMetricType = `metric_type`
    76  )
    77  
    78  // Index represent index in milvus
    79  type Index interface {
    80  	Name() string
    81  	IndexType() IndexType
    82  	Params() map[string]string
    83  }
    84  
    85  // SearchParam interface for index related search param
    86  type SearchParam interface {
    87  	// returns parameters for search/query
    88  	Params() map[string]interface{}
    89  	AddRadius(radius float64)
    90  	AddRangeFilter(rangeFilter float64)
    91  }
    92  
    93  type baseSearchParams struct {
    94  	params map[string]interface{}
    95  }
    96  
    97  func (sp *baseSearchParams) Params() map[string]interface{} {
    98  	params := make(map[string]interface{})
    99  	for k, v := range sp.params {
   100  		params[k] = v
   101  	}
   102  	return params
   103  }
   104  
   105  func (sp *baseSearchParams) AddRadius(radius float64) {
   106  	sp.params["radius"] = radius
   107  }
   108  
   109  func (sp *baseSearchParams) AddRangeFilter(rangeFilter float64) {
   110  	sp.params["range_filter"] = rangeFilter
   111  }
   112  
   113  func newBaseSearchParams() baseSearchParams {
   114  	return baseSearchParams{
   115  		params: make(map[string]interface{}),
   116  	}
   117  }
   118  
   119  type baseIndex struct {
   120  	it   IndexType
   121  	name string
   122  }
   123  
   124  // Name implements Index
   125  func (b baseIndex) Name() string {
   126  	return b.name
   127  }
   128  
   129  // IndexType implements Index
   130  func (b baseIndex) IndexType() IndexType {
   131  	return b.it
   132  }
   133  
   134  // GenericIndex index struct for general usage
   135  // no constraint for index is applied
   136  type GenericIndex struct {
   137  	baseIndex
   138  	params map[string]string
   139  }
   140  
   141  // Params implements Index
   142  func (gi GenericIndex) Params() map[string]string {
   143  	m := make(map[string]string)
   144  	if gi.baseIndex.it != "" {
   145  		m[tIndexType] = string(gi.IndexType())
   146  	}
   147  	for k, v := range gi.params {
   148  		m[k] = v
   149  	}
   150  	return m
   151  }
   152  
   153  // NewGenericIndex create generic index instance
   154  func NewGenericIndex(name string, it IndexType, params map[string]string) Index {
   155  	return GenericIndex{
   156  		baseIndex: baseIndex{
   157  			it:   it,
   158  			name: name,
   159  		},
   160  		params: params,
   161  	}
   162  }