Skip to content

Commit 5914375

Browse files
committed
WIP
1 parent dcd4549 commit 5914375

File tree

1 file changed

+40
-9
lines changed

1 file changed

+40
-9
lines changed

mongo/test/search_index_view_test.go

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,57 @@ import (
88
"github.com/stretchr/testify/require"
99
"github.com/testcontainers/testcontainers-go"
1010
"github.com/testcontainers/testcontainers-go/modules/mongodb/atlaslocal"
11+
"go.mongodb.org/mongo-driver/v2/bson"
12+
"go.mongodb.org/mongo-driver/v2/mongo"
13+
"go.mongodb.org/mongo-driver/v2/mongo/options"
1114
)
1215

1316
func TestNewSingleResultFromDocument(t *testing.T) {
1417
ctx := context.Background()
1518

16-
atlaslocalContainer, err := atlaslocal.Run(ctx, "mongodb/mongodb-atlas-local:latest") // testcontainers.WithExposedPorts("27017/tcp"),
17-
// testcontainers.WithWaitStrategy(
18-
// wait.ForListeningPort("27017/tcp"),
19-
// wait.ForLog("Ready to accept connections"),
20-
// ),
19+
atlaslocalContainer, err := atlaslocal.Run(ctx, "mongodb/mongodb-atlas-local:latest")
2120

2221
defer func() {
2322
err := testcontainers.TerminateContainer(atlaslocalContainer)
2423
assert.NoErrorf(t, err, "failed to terminate container: %v", err)
2524
}()
2625
require.NoErrorf(t, err, "failed to start container: %v", err)
2726

28-
// connString, err := atlaslocalContainer.ConnectionString(ctx)
29-
// require.NoErrorf(t, err, "failed to get connection string: %v", err)
27+
connString, err := atlaslocalContainer.ConnectionString(ctx)
28+
require.NoErrorf(t, err, "failed to get connection string: %v", err)
3029

31-
// _, err = mongo.Connect(options.Client().ApplyURI(connString))
32-
// require.NoErrorf(t, err, "failed to connect to MongoDB: %v", err)
30+
client, err := mongo.Connect(options.Client().ApplyURI(connString))
31+
require.NoErrorf(t, err, "failed to connect to MongoDB: %v", err)
32+
defer func() {
33+
err := client.Disconnect(ctx)
34+
assert.NoErrorf(t, err, "failed to disconnect: %v", err)
35+
}()
36+
37+
collection := client.Database("testdb").Collection("testCollection")
38+
39+
documents := []interface{}{
40+
bson.M{
41+
"string_field": "test1 test1",
42+
},
43+
}
44+
45+
collection.InsertMany(ctx, documents)
46+
47+
idx := bson.D{
48+
// Define custom analyzer for case-insensitive keyword matching
49+
{"mappings", bson.D{
50+
{"dynamic", true},
51+
}},
52+
}
53+
54+
// Create search index with explicit field mappings
55+
// Index fields with multiple types (like ES multi-fields):
56+
// - type: "string" for text/wildcard search
57+
// - type: "token" for exact matching with equals operator
58+
searchIndexModel := mongo.SearchIndexModel{
59+
Definition: idx,
60+
}
61+
62+
_, err = collection.SearchIndexes().CreateOne(ctx, searchIndexModel)
63+
require.NoErrorf(t, err, "failed to create search index: %v", err)
3364
}

0 commit comments

Comments
 (0)