Skip to content

Commit 1b3d574

Browse files
authored
CLOUDP-181547: search index working for arrays (#1992)
1 parent 9990292 commit 1b3d574

File tree

4 files changed

+71
-6
lines changed

4 files changed

+71
-6
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ require (
4040
github.com/tangzero/inflector v1.0.0
4141
github.com/withfig/autocomplete-tools/packages/cobra v1.2.0
4242
go.mongodb.org/atlas v0.27.0
43-
go.mongodb.org/atlas-sdk v0.9.0
43+
go.mongodb.org/atlas-sdk v0.10.0
4444
go.mongodb.org/mongo-driver v1.11.4
4545
go.mongodb.org/ops-manager v0.50.0
4646
golang.org/x/crypto v0.9.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,8 @@ github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1
438438
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
439439
go.mongodb.org/atlas v0.27.0 h1:wGajyHOCDMucvk/REu0ZU3bIgRExVMB6qkUoFaBNoFs=
440440
go.mongodb.org/atlas v0.27.0/go.mod h1:L4BKwVx/OeEhOVjCSdgo90KJm4469iv7ZLzQms/EPTg=
441-
go.mongodb.org/atlas-sdk v0.9.0 h1:6vmtZ5I+zeucpfDWmU3alFZxLTSsJxvJ8INVL5UrqnI=
442-
go.mongodb.org/atlas-sdk v0.9.0/go.mod h1:GzQWxevVUwtUOk9I4DlYLZVvl17TEjTQsGNsH07JQpI=
441+
go.mongodb.org/atlas-sdk v0.10.0 h1:gsaHxHCkkWgRrIOvlpWvPHstYkekNi9Ecmy/HW5wkNk=
442+
go.mongodb.org/atlas-sdk v0.10.0/go.mod h1:GzQWxevVUwtUOk9I4DlYLZVvl17TEjTQsGNsH07JQpI=
443443
go.mongodb.org/mongo-driver v1.11.4 h1:4ayjakA013OdpGyL2K3ZqylTac/rMjrJOMZ1EHizXas=
444444
go.mongodb.org/mongo-driver v1.11.4/go.mod h1:PTSz5yu21bkT/wXpkS7WR5f0ddqw5quethTUn9WM+2g=
445445
go.mongodb.org/ops-manager v0.50.0 h1:H6OX1OJjNaUjO+mu+2XtPGPZHrwHBb4/Hn4ln+QF7uk=

internal/cli/atlas/search/index_opts.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,11 @@ func (opts *IndexOpts) newSearchIndex() (*atlasv2.FTSIndex, error) {
103103
// indexFieldParts index field should be fieldName:analyzer:fieldType.
104104
const indexFieldParts = 2
105105

106-
func (opts *IndexOpts) indexFields() (map[string]map[string]interface{}, error) {
106+
func (opts *IndexOpts) indexFields() (map[string]interface{}, error) {
107107
if len(opts.fields) == 0 {
108108
return nil, nil
109109
}
110-
fields := make(map[string]map[string]interface{})
110+
fields := make(map[string]interface{})
111111
for _, p := range opts.fields {
112112
f := strings.Split(p, ":")
113113
if len(f) != indexFieldParts {

test/e2e/atlas/search_test.go

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ func TestSearch(t *testing.T) {
312312
})
313313

314314
t.Run("Create staticMapping", func(t *testing.T) {
315-
fileName := fmt.Sprintf("create_index_search_test-%v.json", n)
315+
fileName := fmt.Sprintf("create_index_search_test-array-%v.json", n)
316316

317317
file, err := os.Create(fileName)
318318
r.NoError(err)
@@ -392,4 +392,69 @@ func TestSearch(t *testing.T) {
392392
assert.Equal(t, index.Name, indexName)
393393
}
394394
})
395+
396+
t.Run("Create array mapping", func(t *testing.T) {
397+
n, err := e2e.RandInt(1000)
398+
r.NoError(err)
399+
indexName := fmt.Sprintf("index-array-%v", n)
400+
fileName := fmt.Sprintf("create_index_search_test-array-%v.json", n)
401+
402+
file, err := os.Create(fileName)
403+
r.NoError(err)
404+
t.Cleanup(func() {
405+
if e := os.Remove(fileName); e != nil {
406+
t.Errorf("error deleting file '%v': %v", fileName, e)
407+
}
408+
})
409+
410+
tpl := template.Must(template.New("").Parse(`
411+
{
412+
"collectionName": "posts",
413+
"database": "sample_training",
414+
"name": "{{ .indexName }}",
415+
"analyzer": "lucene.standard",
416+
"searchAnalyzer": "lucene.standard",
417+
"mappings": {
418+
"dynamic": false,
419+
"fields": {
420+
"comments": [
421+
{
422+
"dynamic": true,
423+
"type": "document"
424+
},
425+
{
426+
"type": "string"
427+
}]
428+
}
429+
}
430+
}`))
431+
err = tpl.Execute(file, map[string]string{
432+
"indexName": indexName,
433+
})
434+
if err != nil {
435+
t.Fatalf("unexpected error: %v", err)
436+
}
437+
438+
cmd := exec.Command(cliPath,
439+
clustersEntity,
440+
searchEntity,
441+
indexEntity,
442+
"create",
443+
"--clusterName", g.clusterName,
444+
"--file",
445+
fileName,
446+
"--projectId", g.projectID,
447+
"-o=json")
448+
449+
cmd.Env = os.Environ()
450+
resp, err := cmd.CombinedOutput()
451+
452+
if err != nil {
453+
t.Fatalf("unexpected error: %v, resp: %v", err, string(resp))
454+
}
455+
var index atlasv2.FTSIndex
456+
if err := json.Unmarshal(resp, &index); assert.NoError(t, err) {
457+
assert.Equal(t, index.Name, indexName)
458+
}
459+
})
395460
}

0 commit comments

Comments
 (0)