Skip to content

Commit 690a12f

Browse files
authored
Fix Go vector search result output (#32)
* Fix search result output * Clarify environment variables
1 parent eea6681 commit 690a12f

4 files changed

Lines changed: 33 additions & 36 deletions

File tree

mongo-vcore-vector-search-go/.env.example

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ EMBEDDING_SIZE_BATCH=16
1919

2020
# Data File Configuration
2121
# Path to source JSON file without vector embeddings
22-
DATA_FILE_WITHOUT_VECTORS=../data/HotelsData_toCosmosDB.json
22+
DATA_FILE_WITHOUT_VECTORS=data/HotelsData_toCosmosDB.json
2323

2424
# Path to JSON file with generated vector embeddings
25-
DATA_FILE_WITH_VECTORS=../data/HotelsData_toCosmosDB_Vector.json
25+
DATA_FILE_WITH_VECTORS=data/HotelsData_toCosmosDB_Vector.json
2626

2727
# Path to JSON file with vector similarity data (optional)
2828
DATA_FILE_WITH_SIMILARITY=data/HotelsData_with_similarity.json

mongo-vcore-vector-search-go/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ MONGO_CONNECTION_STRING=mongodb+srv://username:password@your-cluster.mongocluste
124124
MONGO_CLUSTER_NAME=vectorSearch
125125
126126
# Data Configuration (defaults should work)
127-
DATA_FILE_WITHOUT_VECTORS=../data/HotelsData_toCosmosDB_Vector.json
128-
DATA_FILE_WITH_VECTORS=../data/HotelsData_toCosmosDB_Vector.json
127+
DATA_FILE_WITHOUT_VECTORS=data/HotelsData_toCosmosDB_Vector.json
128+
DATA_FILE_WITH_VECTORS=data/HotelsData_toCosmosDB_Vector.json
129129
FIELD_TO_EMBED=Description
130130
EMBEDDED_FIELD=DescriptionVector
131131
EMBEDDING_DIMENSIONS=1536

mongo-vcore-vector-search-go/src/create_embeddings.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,12 @@ func ProcessEmbeddingBatch(ctx context.Context, dataBatch []map[string]interface
105105

106106
// EmbeddingConfig holds configuration for the embedding creation process
107107
type EmbeddingConfig struct {
108-
ModelName string
109-
InputFile string
110-
OutputFile string
111-
FieldToEmbed string
112-
EmbeddedField string
113-
BatchSize int
108+
ModelName string
109+
DataWithoutVectors string
110+
DataWithVectors string
111+
FieldToEmbed string
112+
EmbeddedField string
113+
BatchSize int
114114
}
115115

116116
// LoadEmbeddingConfig loads configuration from environment variables
@@ -124,12 +124,12 @@ func LoadEmbeddingConfig() *EmbeddingConfig {
124124
batchSize, _ := strconv.Atoi(getEnvOrDefault("EMBEDDING_SIZE_BATCH", "16"))
125125

126126
return &EmbeddingConfig{
127-
ModelName: getEnvOrDefault("AZURE_OPENAI_EMBEDDING_MODEL", "text-embedding-ada-002"),
128-
InputFile: getEnvOrDefault("DATA_FILE_WITHOUT_VECTORS", "data/HotelsData_toCosmosDB_Vector.json"),
129-
OutputFile: getEnvOrDefault("DATA_FILE_WITH_VECTORS", "data/HotelsData_with_vectors.json"),
130-
FieldToEmbed: getEnvOrDefault("FIELD_TO_EMBED", "Description"),
131-
EmbeddedField: getEnvOrDefault("EMBEDDED_FIELD", "DescriptionVector"),
132-
BatchSize: batchSize,
127+
ModelName: getEnvOrDefault("AZURE_OPENAI_EMBEDDING_MODEL", "text-embedding-ada-002"),
128+
DataWithoutVectors: getEnvOrDefault("DATA_FILE_WITHOUT_VECTORS", "HotelsData_toCosmosDB.json"),
129+
DataWithVectors: getEnvOrDefault("DATA_FILE_WITH_VECTORS", "data/HotelsData_toCosmosDB_Vector.json"),
130+
FieldToEmbed: getEnvOrDefault("FIELD_TO_EMBED", "Description"),
131+
EmbeddedField: getEnvOrDefault("EMBEDDED_FIELD", "DescriptionVector"),
132+
BatchSize: batchSize,
133133
}
134134
}
135135

@@ -149,8 +149,8 @@ func main() {
149149
config := LoadEmbeddingConfig()
150150

151151
fmt.Printf("Configuration:\n")
152-
fmt.Printf(" Input file: %s\n", config.InputFile)
153-
fmt.Printf(" Output file: %s\n", config.OutputFile)
152+
fmt.Printf(" Input file: %s\n", config.DataWithoutVectors)
153+
fmt.Printf(" Output file: %s\n", config.DataWithVectors)
154154
fmt.Printf(" Field to embed: %s\n", config.FieldToEmbed)
155155
fmt.Printf(" Embedding field: %s\n", config.EmbeddedField)
156156
fmt.Printf(" Batch size: %d\n", config.BatchSize)
@@ -169,8 +169,8 @@ func main() {
169169
}()
170170

171171
// Read the input data file
172-
fmt.Printf("\nReading input data from %s...\n", config.InputFile)
173-
data, err := ReadFileReturnJSON(config.InputFile)
172+
fmt.Printf("\nReading input data from %s...\n", config.DataWithoutVectors)
173+
data, err := ReadFileReturnJSON(config.DataWithoutVectors)
174174
if err != nil {
175175
log.Fatalf("Failed to read input file: %v", err)
176176
}
@@ -212,8 +212,8 @@ func main() {
212212
}
213213

214214
// Save the enhanced data with embeddings
215-
fmt.Printf("\nSaving enhanced data to %s...\n", config.OutputFile)
216-
err = WriteFileJSON(data, config.OutputFile)
215+
fmt.Printf("\nSaving enhanced data to %s...\n", config.DataWithVectors)
216+
err = WriteFileJSON(data, config.DataWithVectors)
217217
if err != nil {
218218
log.Fatalf("Failed to save output file: %v", err)
219219
}

mongo-vcore-vector-search-go/src/utils.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -402,27 +402,24 @@ func PrintSearchResults(results []SearchResult, maxResults int, showScore bool)
402402
for i := 0; i < maxResults; i++ {
403403
result := results[i]
404404

405-
// Extract hotel data from the document
405+
// Extract HotelName from document (assuming bson.D structure)
406+
doc := result.Document.(bson.D)
406407
var hotelName string
407-
if doc, ok := result.Document.(bson.M); ok {
408-
if name, exists := doc["HotelName"]; exists {
409-
hotelName = fmt.Sprintf("%v", name)
410-
}
411-
} else if doc, ok := result.Document.(map[string]interface{}); ok {
412-
if name, exists := doc["HotelName"]; exists {
413-
hotelName = fmt.Sprintf("%v", name)
408+
for _, elem := range doc {
409+
if elem.Key == "HotelName" {
410+
hotelName = fmt.Sprintf("%v", elem.Value)
411+
break
414412
}
415413
}
416414

415+
// Display results
416+
fmt.Printf("%d. HotelName: %s", i+1, hotelName)
417+
417418
if showScore {
418-
fmt.Printf("HotelName: %s, Score: %.4f\n", hotelName, result.Score)
419-
} else {
420-
fmt.Printf("HotelName: %s\n", hotelName)
419+
fmt.Printf(", Score: %.4f", result.Score)
421420
}
422-
}
423421

424-
if len(results) > maxResults {
425-
fmt.Printf("\n... and %d more results\n", len(results)-maxResults)
422+
fmt.Println()
426423
}
427424
}
428425

0 commit comments

Comments
 (0)