package mainimport ("fmt""github.com/qdrant/go-client/qdrant")func main() {client, err := qdrant.NewClient(&qdrant.Config{Host: "localhost",Port: 6334,})if err!= nil {panic(err)}fmt.Println(client)}
2025/04/05 16:08:22 WARN Unable to get server version, use default err="rpc error: code = Unavailable desc = connection error: desc = \"error reading server preface: http2: frame too large\"" default=Unknown2025/04/05 16:08:22 WARN Failed to obtain server version. Unable to check client-server compatibility. Set SkipCompatibilityCheck=true to skip version check.
package mainimport ("context""fmt""github.com/qdrant/go-client/qdrant")func main() {client, err := qdrant.NewClient(&qdrant.Config{Host: "localhost",Port: 6334,})if err!= nil {panic(err)}fmt.Println(client)err=client.CreateCollection(context.Background(), &qdrant.CreateCollection{CollectionName: "example_collection",VectorsConfig: qdrant.NewVectorsConfig(&qdrant.VectorParams{Size: 4,Distance: qdrant.Distance_Cosine,}),})if err!= nil {panic(err)}operationInfo, err := client.Upsert(context.Background(), &qdrant.UpsertPoints{CollectionName: "example_collection",Points: []*qdrant.PointStruct{{Id: qdrant.NewIDNum(1),Vectors: qdrant.NewVectors(0.05, 0.61, 0.76, 0.74),Payload: qdrant.NewValueMap(map[string]any{"city": "London"}),},{Id: qdrant.NewIDNum(2),Vectors: qdrant.NewVectors(0.19, 0.81, 0.75, 0.11),Payload: qdrant.NewValueMap(map[string]any{"age": 32}),},{Id: qdrant.NewIDNum(3),Vectors: qdrant.NewVectors(0.36, 0.55, 0.47, 0.94),Payload: qdrant.NewValueMap(map[string]any{"vegan": true}),},},})if err != nil {panic(err)}fmt.Println(operationInfo)searchResult, err := client.Query(context.Background(), &qdrant.QueryPoints{CollectionName: "example_collection",Query: qdrant.NewQuery(0.2, 0.1, 0.9, 0.7),})if err != nil {panic(err)}fmt.Println(searchResult)searchResult, err = client.Query(context.Background(), &qdrant.QueryPoints{CollectionName: "example_collection",Query: qdrant.NewQuery(0.2, 0.1, 0.9, 0.7),Filter: &qdrant.Filter{Must: []*qdrant.Condition{qdrant.NewMatch("city", "London"),},},WithPayload: qdrant.NewWithPayload(true),})if err != nil {panic(err)}fmt.Println(searchResult)}
package mainimport("net/http""bytes""encoding/json""io/ioutil""fmt")func main() {createCollection()addData()}func createCollection() {data := Payload {// fill structVectors {Size: 768,Distance: "Dot",},}payloadBytes, err := json.Marshal(data)if err != nil {// handle err}body := bytes.NewReader(payloadBytes)req, err := http.NewRequest(http.MethodPut, "http://localhost:6333/collections/romeo", body)if err != nil {// handle err}req.Header.Set("Content-Type", "application/json")resp, err := http.DefaultClient.Do(req)if err != nil {// handle err}defer resp.Body.Close()if resp.StatusCode != http.StatusOK {// handle err}// handle responseresponseBytes, err := ioutil.ReadAll(resp.Body)if err != nil {// handle err}responseData := Payload{}err = json.Unmarshal(responseBytes, &responseData)if err != nil {// handle err}fmt.Println(string(responseBytes))}func addData() {data := AddReq{// fill structPoints: []Points{Points{ID: 1,Vector: []float64{0.05, 0.61, 0.76, 0.74},Payload: AddPayload{Colony: "Mars",},},Points{ID: 2,Vector: []float64{0.19, 0.81, 0.75, 0.11},Payload: AddPayload{Colony: "Jupiter",},},Points{ID: 3,Vector: []float64{0.36, 0.55, 0.47, 0.94},Payload: AddPayload{Colony: "Venus",},},Points{ID: 4,Vector: []float64{0.18, 0.01, 0.85, 0.80},Payload: AddPayload{Colony: "Moon",},},Points{ID: 5,Vector: []float64{0.24, 0.18, 0.22, 0.44},Payload: AddPayload{Colony: "Pluto",},},},}payloadBytes, err := json.Marshal(data)if err != nil {// handle err}body := bytes.NewReader(payloadBytes)req, err := http.NewRequest(http.MethodPut, "http://localhost:6333/collections/star_charts/points", body)if err != nil {// handle err}req.Header.Set("Content-Type", "application/json")resp, err := http.DefaultClient.Do(req)if err != nil {// handle err}defer resp.Body.Close()if resp.StatusCode != http.StatusOK {// handle err}// handle responseresponseBytes, err := ioutil.ReadAll(resp.Body)if err != nil {// handle err}responseData := Payload{}err = json.Unmarshal(responseBytes, &responseData)if err != nil {// handle err}fmt.Println(string(responseBytes))}type Payload struct {Vectors Vectors `json:"vectors"`}type Vectors struct {Size int `json:"size"`Distance string `json:"distance"`}type AddReq struct {Points []Points `json:"points"`}type AddPayload struct {Colony string `json:"colony"`}type Points struct {ID int `json:"id"`Vector []float64 `json:"vector"`Payload AddPayload `json:"payload"`}
{"status":{"error":"Wrong input: Collection `romeo` already exists!"},"time":0.00016268}{"result":{"operation_id":2,"status":"acknowledged"},"status":"ok","time":0.010663987}


文章转载自golang算法架构leetcode技术php,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




