Search index fails unless the "Options" is set on the index model

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Unresolved
    • Priority: Major - P3
    • 2.4.1
    • Affects Version/s: None
    • Component/s: None
    • None
    • None
    • Go Drivers
    • None
    • None
    • None
    • None
    • None
    • None

      Detailed steps to reproduce the problem?

      The code they gave us to reproduce is as follows:

      import (
      	"context"
      	"fmt"
      	"testing"
      
      	v1bson "go.mongodb.org/mongo-driver/bson"
      	v1mongo "go.mongodb.org/mongo-driver/mongo"
      	v1options "go.mongodb.org/mongo-driver/mongo/options"
      	v2bson "go.mongodb.org/mongo-driver/v2/bson"
      	v2mongo "go.mongodb.org/mongo-driver/v2/mongo"
      	v2options "go.mongodb.org/mongo-driver/v2/mongo/options"
      )
      
      func TestV1(t *testing.T) {
      	ctx := context.Background()
      	atlasClient, err := v1mongo.Connect(ctx, v1options.Client().ApplyURI("mongodb://admin:admin@localhost:20004/?replicaSet=rs0&directConnection=true"))
      	if err != nil {
      		t.Fatalf("Failed to connect to MongoDB Atlas Search: %v", err)
      	}
      	defer func() {
      		if err := atlasClient.Disconnect(ctx); err != nil {
      			t.Logf("Failed to disconnect: %v", err)
      		}
      	}()
      
      	collection := atlasClient.Database("testdb").Collection("testCollection")
      
      	documents := []interface{}{
      		v1bson.M{
      			"string_field": "test1 test1",
      		},
      	}
      
      	collection.InsertMany(ctx, documents)
      
      	idx := v1bson.D{
      		// Define custom analyzer for case-insensitive keyword matching
      		{"mappings", v1bson.D{
      			{"dynamic", true},
      		}},
      	}
      
      	// Create search index with explicit field mappings
      	// Index fields with multiple types (like ES multi-fields):
      	// - type: "string" for text/wildcard search
      	// - type: "token" for exact matching with equals operator
      	searchIndexModel := v1mongo.SearchIndexModel{
      		Definition: idx,
      		Options:    nil,
      	}
      
      	indexName, err := collection.SearchIndexes().CreateOne(ctx, searchIndexModel)
      
      	if err != nil {
      		t.Fatalf("Failed to create search index: %v", err)
      	} else {
      		fmt.Printf("Successfully created index: %v\n", indexName)
      	}
      
      }
      
      func TestV2(t *testing.T) {
      	ctx := context.Background()
      	atlasClient, err := v2mongo.Connect(v2options.Client().ApplyURI("mongodb://admin:admin@localhost:20004/?replicaSet=rs0&directConnection=true"))
      	if err != nil {
      		t.Fatalf("Failed to connect to MongoDB Atlas Search: %v", err)
      	}
      	defer func() {
      		if err := atlasClient.Disconnect(ctx); err != nil {
      			t.Logf("Failed to disconnect: %v", err)
      		}
      	}()
      
      	collection := atlasClient.Database("testdb").Collection("testCollection2")
      
      	documents := []interface{}{
      		v2bson.M{
      			"string_field": "test1 test1",
      		},
      	}
      
      	collection.InsertMany(ctx, documents)
      
      	//opts := v2options.SearchIndexes().SetName("test")
      
      	idx := v2bson.D{
      		// Define custom analyzer for case-insensitive keyword matching
      		{"mappings", v2bson.D{
      			{"dynamic", true},
      		}},
      	}
      
      	// Create search index with explicit field mappings
      	// Index fields with multiple types (like ES multi-fields):
      	// - type: "string" for text/wildcard search
      	// - type: "token" for exact matching with equals operator
      	searchIndexModel := v2mongo.SearchIndexModel{
      		Definition: idx,
      	}
      
      	indexName, err := collection.SearchIndexes().CreateOne(ctx, searchIndexModel)
      
      	if err != nil {
      		t.Fatalf("Failed to create search index: %v", err)
      	} else {
      		fmt.Printf("Successfully created index: %v\n", indexName)
      	}
      
      }
      

      The test fails if the Options is not given on the index model.

      Definition of done: what must be done to consider the task complete?

      The exact Go version used, with patch level:

      $ go version

      The exact version of the Go driver used:

      v2

      Describe how MongoDB is set up. Local vs Hosted, version, topology, load balanced, etc.

      The operating system and version (e.g. Windows 7, OSX 10.8, ...)

      Security Vulnerabilities

      If you’ve identified a security vulnerability in a driver or any other MongoDB project, please report it according to the instructions here

            Assignee:
            Qingyang Hu
            Reporter:
            Qingyang Hu
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: