github.com/milvus-io/milvus-sdk-go/v2@v2.4.1/examples/query/query.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"log"
     7  	"math/rand"
     8  	"time"
     9  
    10  	"github.com/milvus-io/milvus-sdk-go/v2/client"
    11  	"github.com/milvus-io/milvus-sdk-go/v2/entity"
    12  )
    13  
    14  const (
    15  	milvusAddr     = `localhost:19530`
    16  	nEntities, dim = 3000, 128
    17  	collectionName = "query_example"
    18  
    19  	msgFmt                         = "==== %s ====\n"
    20  	idCol, randomCol, embeddingCol = "ID", "random", "embeddings"
    21  )
    22  
    23  func main() {
    24  	ctx := context.Background()
    25  
    26  	log.Printf(msgFmt, "start connecting to Milvus")
    27  	c, err := client.NewClient(ctx, client.Config{
    28  		Address: milvusAddr,
    29  	})
    30  	if err != nil {
    31  		log.Fatalf("failed to connect to milvus, err: %v", err)
    32  	}
    33  	defer c.Close()
    34  
    35  	// delete collection if exists
    36  	has, err := c.HasCollection(ctx, collectionName)
    37  	if err != nil {
    38  		log.Fatalf("failed to check collection exists, err: %v", err)
    39  	}
    40  	if has {
    41  		c.DropCollection(ctx, collectionName)
    42  	}
    43  
    44  	// define schema
    45  	log.Printf(msgFmt, fmt.Sprintf("create collection, `%s`", collectionName))
    46  	schema := entity.NewSchema().WithName(collectionName).WithDescription("query_example is the collection to exlain query API").
    47  		WithField(entity.NewField().WithName(idCol).WithDataType(entity.FieldTypeInt64).WithIsPrimaryKey(true).WithIsAutoID(false)).
    48  		WithField(entity.NewField().WithName(randomCol).WithDataType(entity.FieldTypeDouble)).
    49  		WithField(entity.NewField().WithName(embeddingCol).WithDataType(entity.FieldTypeFloatVector).WithDim(dim))
    50  
    51  	// create collection with consistency level, which serves as the default search/query consistency level
    52  	if err := c.CreateCollection(ctx, schema, entity.DefaultShardNumber, client.WithConsistencyLevel(entity.ClBounded)); err != nil {
    53  		log.Fatalf("create collection failed, err: %v", err)
    54  	}
    55  
    56  	log.Printf(msgFmt, "start inserting random entities")
    57  	idList, randomList := make([]int64, 0, nEntities), make([]float64, 0, nEntities)
    58  	embeddingList := make([][]float32, 0, nEntities)
    59  
    60  	rand.Seed(time.Now().UnixNano())
    61  
    62  	// generate data
    63  	for i := 0; i < nEntities; i++ {
    64  		idList = append(idList, int64(i))
    65  	}
    66  	for i := 0; i < nEntities; i++ {
    67  		randomList = append(randomList, rand.Float64())
    68  	}
    69  	for i := 0; i < nEntities; i++ {
    70  		vec := make([]float32, 0, dim)
    71  		for j := 0; j < dim; j++ {
    72  			vec = append(vec, rand.Float32())
    73  		}
    74  		embeddingList = append(embeddingList, vec)
    75  	}
    76  	idColData := entity.NewColumnInt64(idCol, idList)
    77  	randomColData := entity.NewColumnDouble(randomCol, randomList)
    78  	embeddingColData := entity.NewColumnFloatVector(embeddingCol, dim, embeddingList)
    79  
    80  	// build index
    81  	log.Printf(msgFmt, "start creating index IVF_FLAT")
    82  	idx, err := entity.NewIndexIvfFlat(entity.L2, 128)
    83  	if err != nil {
    84  		log.Fatalf("failed to create ivf flat index, err: %v", err)
    85  	}
    86  	if err := c.CreateIndex(ctx, collectionName, embeddingCol, idx, false); err != nil {
    87  		log.Fatalf("failed to create index, err: %v", err)
    88  	}
    89  
    90  	// insert data
    91  	if _, err := c.Insert(ctx, collectionName, "", idColData, randomColData, embeddingColData); err != nil {
    92  		log.Fatalf("failed to insert random data into %s, err: %s", collectionName, err.Error())
    93  	}
    94  
    95  	log.Printf(msgFmt, "start loading collection")
    96  	err = c.LoadCollection(ctx, collectionName, false)
    97  	if err != nil {
    98  		log.Fatalf("failed to load collection, err: %v", err)
    99  	}
   100  
   101  	//query
   102  	expr := "ID in [0, 1, 2]"
   103  	log.Printf(msgFmt, fmt.Sprintf("query with expr `%s`", expr))
   104  	resultSet, err := c.Query(ctx, collectionName, nil, expr, []string{idCol, randomCol})
   105  	if err != nil {
   106  		log.Fatalf("failed to query result, err: %v", err)
   107  	}
   108  	printResultSet(resultSet, idCol, randomCol)
   109  
   110  	// drop collection
   111  	log.Printf(msgFmt, fmt.Sprintf("drop collection `%s`", collectionName))
   112  	if err := c.DropCollection(ctx, collectionName); err != nil {
   113  		log.Fatalf("failed to drop collection, err: %v", err)
   114  	}
   115  }
   116  
   117  func printResultSet(rs client.ResultSet, outputFields ...string) {
   118  	for _, fieldName := range outputFields {
   119  		column := rs.GetColumn(fieldName)
   120  		if column == nil {
   121  			log.Printf("column %s not exists in result set\n", fieldName)
   122  		}
   123  		switch column.Type() {
   124  		case entity.FieldTypeInt64:
   125  			var result []int64
   126  			for i := 0; i < column.Len(); i++ {
   127  				v, err := column.GetAsInt64(i)
   128  				if err != nil {
   129  					log.Printf("column %s row %d cannot GetAsInt64, %s\n", fieldName, i, err.Error())
   130  				}
   131  				result = append(result, v)
   132  			}
   133  			log.Printf("Column %s: value: %v\n", fieldName, result)
   134  		case entity.FieldTypeDouble:
   135  			var result []float64
   136  			for i := 0; i < column.Len(); i++ {
   137  				v, err := column.GetAsDouble(i)
   138  				if err != nil {
   139  					log.Printf("column %s row %d cannot GetAsDouble, %s\n", fieldName, i, err.Error())
   140  				}
   141  				result = append(result, v)
   142  			}
   143  			log.Printf("Column %s: value: %v\n", fieldName, result)
   144  		}
   145  	}
   146  }