github.com/milvus-io/milvus-sdk-go/v2@v2.4.1/entity/index_sparse.go (about)

     1  package entity
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  
     7  	"github.com/cockroachdb/errors"
     8  )
     9  
    10  var _ Index = (*IndexSparseInverted)(nil)
    11  
    12  // IndexSparseInverted index type for SPARSE_INVERTED_INDEX
    13  type IndexSparseInverted struct {
    14  	metricType MetricType
    15  	dropRatio  float64
    16  }
    17  
    18  func (i *IndexSparseInverted) Name() string {
    19  	return "SparseInverted"
    20  }
    21  
    22  func (i *IndexSparseInverted) IndexType() IndexType {
    23  	return SparseInverted
    24  }
    25  
    26  func (i *IndexSparseInverted) Params() map[string]string {
    27  	params := map[string]string{
    28  		"drop_ratio_build": fmt.Sprintf("%v", i.dropRatio),
    29  	}
    30  	bs, _ := json.Marshal(params)
    31  	return map[string]string{
    32  		tParams:     string(bs),
    33  		tIndexType:  string(i.IndexType()),
    34  		tMetricType: string(i.metricType),
    35  	}
    36  }
    37  
    38  type IndexSparseInvertedSearchParam struct {
    39  	baseSearchParams
    40  }
    41  
    42  func NewIndexSparseInvertedSearchParam(dropRatio float64) (*IndexSparseInvertedSearchParam, error) {
    43  	if dropRatio < 0 || dropRatio >= 1 {
    44  		return nil, errors.Newf("invalid dropRatio for search: %v, must be in range [0, 1)", dropRatio)
    45  	}
    46  	sp := &IndexSparseInvertedSearchParam{
    47  		baseSearchParams: newBaseSearchParams(),
    48  	}
    49  
    50  	sp.params["drop_ratio_search"] = dropRatio
    51  	return sp, nil
    52  }
    53  
    54  // IndexSparseInverted index type for SPARSE_INVERTED_INDEX
    55  func NewIndexSparseInverted(metricType MetricType, dropRatio float64) (*IndexSparseInverted, error) {
    56  	if dropRatio < 0 || dropRatio >= 1.0 {
    57  		return nil, errors.Newf("invalid dropRatio for build: %v, must be in range [0, 1)", dropRatio)
    58  	}
    59  	return &IndexSparseInverted{
    60  		metricType: metricType,
    61  		dropRatio:  dropRatio,
    62  	}, nil
    63  }
    64  
    65  type IndexSparseWAND struct {
    66  	metricType MetricType
    67  	dropRatio  float64
    68  }
    69  
    70  func (i *IndexSparseWAND) Name() string {
    71  	return "SparseWAND"
    72  }
    73  
    74  func (i *IndexSparseWAND) IndexType() IndexType {
    75  	return SparseWAND
    76  }
    77  
    78  func (i *IndexSparseWAND) Params() map[string]string {
    79  	params := map[string]string{
    80  		"drop_ratio_build": fmt.Sprintf("%v", i.dropRatio),
    81  	}
    82  	bs, _ := json.Marshal(params)
    83  	return map[string]string{
    84  		tParams:     string(bs),
    85  		tIndexType:  string(i.IndexType()),
    86  		tMetricType: string(i.metricType),
    87  	}
    88  }
    89  
    90  // IndexSparseWAND index type for SPARSE_WAND, weak-and
    91  func NewIndexSparseWAND(metricType MetricType, dropRatio float64) (*IndexSparseWAND, error) {
    92  	if dropRatio < 0 || dropRatio >= 1.0 {
    93  		return nil, errors.Newf("invalid dropRatio for build: %v, must be in range [0, 1)", dropRatio)
    94  	}
    95  	return &IndexSparseWAND{
    96  		metricType: metricType,
    97  		dropRatio:  dropRatio,
    98  	}, nil
    99  }
   100  
   101  type IndexSparseWANDSearchParam struct {
   102  	baseSearchParams
   103  }
   104  
   105  func NewIndexSparseWANDSearchParam(dropRatio float64) (*IndexSparseWANDSearchParam, error) {
   106  	if dropRatio < 0 || dropRatio >= 1 {
   107  		return nil, errors.Newf("invalid dropRatio for search: %v, must be in range [0, 1)", dropRatio)
   108  	}
   109  	sp := &IndexSparseWANDSearchParam{
   110  		baseSearchParams: newBaseSearchParams(),
   111  	}
   112  
   113  	sp.params["drop_ratio_search"] = dropRatio
   114  	return sp, nil
   115  }