client

package
v0.0.0-...-4412ad8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 11, 2022 License: Apache-2.0, MIT Imports: 14 Imported by: 0

README

Vearch Go Client

MASTER address is 127.0.0.1, MASTER api_port is 8817,ROUTER address is 127.0.0.1,ROUTER port is 9001, ROUTER rpc_port is 9002.

Create DB

# request
curl -H "content-type: application/json" -XPUT -d'
{
	"name":"ts_db"
}
' {{MASTER}}/db/_create

Create Space

curl -H "content-type: application/json" -XPUT -d'
{
	"name": "ts_space",
	"partition_num": 1,
	"replica_num": 1,
	"engine": {
		"name": "gamma",
		"index_size": 10000,
		"id_type": "string",
		"retrieval_type": "IVFPQ",
		"retrieval_param": {
			"metric_type":"InnerProduct",
			"ncentroids": 2048,
			"nsubvector": 32
		}
	},
	"properties": {
		"field1": {
			"type": "string",
			"index": true
		},
		"field2": {
			"type": "string",
			"index": true
		},
		"field3": {
			"type": "integer",
			"index": true
		},
		"vector": {
			"type": "vector",
			"dimension": 128,
			"store_type": "MemoryOnly",
			"format": "normalization"
		}
	}
}
' {{MASTER}}/space/test_vector_db/_create

Demo


var (
	address   = "127.0.0.1:9002"
	dbName    = "ts_db"
	spaceName = "ts_space"
)

// TestDoc The struct of test doc
type TestDoc struct {
	// ID the primary key, default null str.
	ID     string    `json:"_id"`
	Field1 string    `json:"field1"`
	Field2 string    `json:"field2"`
	Field3 int32     `json:"field3"`
	Vector []float32 `json:"vector"`
}

func test() {
	// init a doc
	doc := &TestDoc{}
	doc.ID = "1"
	doc.Field1 = "field1"
	doc.Field2 = "field2"
	doc.Field3 = 123
	doc.Vector = make([]float32, 128)
	for j := range doc.Vector {
		doc.Vector[j] = float32(math.Abs(float64(rand.Float32())))
	}
	client.Normalization(doc.Vector)

	//make a connection
	vc, err := client.New(dbName, spaceName, address)
	if err != nil {
		panic(fmt.Sprintf("conn failed, %s", err.Error()))
	}
	defer vc.Close()

	// change a doc to pb
	d, err := client.MakeDocument(doc)
	if err != nil {
		panic(fmt.Sprintf("MakeDocument failed, %s", err.Error()))
	}

	// add a doc
	ctx := context.Background()
	res, err := vc.Add(ctx, d)
	if err != nil {
		panic(fmt.Sprintf("Add failed, case: [%v], res: [%v], err: [%v]", doc, res, err))
	}

	// Get a doc by id
	items, err := vc.Get(ctx, []string{doc.ID})
	if err != nil {
		panic(fmt.Sprintf("Get failed, case: [%v] , err: [%v]", doc.ID, err))
	}
	getDoc := &TestDoc{}
	err = client.DocToStruct(items[0].Doc, getDoc)

	// Search result by feature
	vectorQuery := &vearchpb.VectorQuery{}
	for _, field := range d.Fields {
		if field.Type == vearchpb.FieldType_VECTOR {
			vectorQuery.Name = field.Name
			vectorQuery.Value = field.Value
			vectorQuery.MinScore = 0.0
			vectorQuery.MaxScore = 1.0
			vectorQuery.Boost = 1
			break
		}
	}
	// query
	query := &vearchpb.SearchRequest{}
	query.ReqNum = 1
	query.TopN = 10
	query.VecFields = []*vearchpb.VectorQuery{vectorQuery}
	query.Fields = []string{"field1", "field2"}

	query.RetrievalParams = `{"nprobe" : 10, "metric_type" : "InnerProduct"}`
	query.MultiVectorRank = 1

	resp, err := vc.Search(ctx, query)
	if err != nil {
		panic(fmt.Sprintf("request failed, response: %v, err: %s", resp, err.Error()))
	}

	// Delete a doc by id
	items, err = vc.Delete(ctx, []string{doc.ID})
	if err != nil {
		panic(fmt.Sprintf("delete failed, case: [%v] , err: [%v]", doc.ID, err))
	}
}

Documentation

Index

Constants

View Source
const (
	String      = "string"
	Int32       = "int32"
	Int64       = "int64"
	Float32     = "float32"
	Float64     = "float64"
	ArrayFloat  = "[]float32"
	ArrayUint8  = "[]uint8"
	ArrayString = "[]string"
)

Variables

View Source
var (
	ErrResponse = errors.New("Response or Response.Head or Response.Head.Err can not be nil")
)

Functions

func DocToStruct

func DocToStruct(doc *Document, res interface{}) (err error)

func MakeBinaryVectorField

func MakeBinaryVectorField(key string, value []uint8) (*Field, error)

func MakeDocument

func MakeDocument(doc interface{}) (*Document, error)

func MakeFloat32Field

func MakeFloat32Field(key string, value float32) *Field

func MakeFloat64Field

func MakeFloat64Field(key string, value float64) *Field

func MakeFloatVectorField

func MakeFloatVectorField(key string, value []float32) (*Field, error)

func MakeInt32Field

func MakeInt32Field(key string, value int32) *Field

func MakeInt64Field

func MakeInt64Field(key string, value int64) *Field

func MakeStringField

func MakeStringField(key string, value string) *Field

func MakeStringVectorField

func MakeStringVectorField(key string, value []string) (*Field, error)

func New

func New(dbName, spaceName string, addrs ...string) (*vearchClient, error)

func Normalization

func Normalization(feature []float32) error

Types

This section is empty.

Directories

Path Synopsis
utils

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL