Skip to content

Commit

Permalink
CLOUDP-181547: search index working for arrays (#1992)
Browse files Browse the repository at this point in the history
  • Loading branch information
wtrocki committed Jun 7, 2023
1 parent 9990292 commit 1b3d574
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 6 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ require (
github.com/tangzero/inflector v1.0.0
github.com/withfig/autocomplete-tools/packages/cobra v1.2.0
go.mongodb.org/atlas v0.27.0
go.mongodb.org/atlas-sdk v0.9.0
go.mongodb.org/atlas-sdk v0.10.0
go.mongodb.org/mongo-driver v1.11.4
go.mongodb.org/ops-manager v0.50.0
golang.org/x/crypto v0.9.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -438,8 +438,8 @@ github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.mongodb.org/atlas v0.27.0 h1:wGajyHOCDMucvk/REu0ZU3bIgRExVMB6qkUoFaBNoFs=
go.mongodb.org/atlas v0.27.0/go.mod h1:L4BKwVx/OeEhOVjCSdgo90KJm4469iv7ZLzQms/EPTg=
go.mongodb.org/atlas-sdk v0.9.0 h1:6vmtZ5I+zeucpfDWmU3alFZxLTSsJxvJ8INVL5UrqnI=
go.mongodb.org/atlas-sdk v0.9.0/go.mod h1:GzQWxevVUwtUOk9I4DlYLZVvl17TEjTQsGNsH07JQpI=
go.mongodb.org/atlas-sdk v0.10.0 h1:gsaHxHCkkWgRrIOvlpWvPHstYkekNi9Ecmy/HW5wkNk=
go.mongodb.org/atlas-sdk v0.10.0/go.mod h1:GzQWxevVUwtUOk9I4DlYLZVvl17TEjTQsGNsH07JQpI=
go.mongodb.org/mongo-driver v1.11.4 h1:4ayjakA013OdpGyL2K3ZqylTac/rMjrJOMZ1EHizXas=
go.mongodb.org/mongo-driver v1.11.4/go.mod h1:PTSz5yu21bkT/wXpkS7WR5f0ddqw5quethTUn9WM+2g=
go.mongodb.org/ops-manager v0.50.0 h1:H6OX1OJjNaUjO+mu+2XtPGPZHrwHBb4/Hn4ln+QF7uk=
Expand Down
4 changes: 2 additions & 2 deletions internal/cli/atlas/search/index_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ func (opts *IndexOpts) newSearchIndex() (*atlasv2.FTSIndex, error) {
// indexFieldParts index field should be fieldName:analyzer:fieldType.
const indexFieldParts = 2

func (opts *IndexOpts) indexFields() (map[string]map[string]interface{}, error) {
func (opts *IndexOpts) indexFields() (map[string]interface{}, error) {
if len(opts.fields) == 0 {
return nil, nil
}
fields := make(map[string]map[string]interface{})
fields := make(map[string]interface{})
for _, p := range opts.fields {
f := strings.Split(p, ":")
if len(f) != indexFieldParts {
Expand Down
67 changes: 66 additions & 1 deletion test/e2e/atlas/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ func TestSearch(t *testing.T) {
})

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

file, err := os.Create(fileName)
r.NoError(err)
Expand Down Expand Up @@ -392,4 +392,69 @@ func TestSearch(t *testing.T) {
assert.Equal(t, index.Name, indexName)
}
})

t.Run("Create array mapping", func(t *testing.T) {
n, err := e2e.RandInt(1000)
r.NoError(err)
indexName := fmt.Sprintf("index-array-%v", n)
fileName := fmt.Sprintf("create_index_search_test-array-%v.json", n)

file, err := os.Create(fileName)
r.NoError(err)
t.Cleanup(func() {
if e := os.Remove(fileName); e != nil {
t.Errorf("error deleting file '%v': %v", fileName, e)
}
})

tpl := template.Must(template.New("").Parse(`
{
"collectionName": "posts",
"database": "sample_training",
"name": "{{ .indexName }}",
"analyzer": "lucene.standard",
"searchAnalyzer": "lucene.standard",
"mappings": {
"dynamic": false,
"fields": {
"comments": [
{
"dynamic": true,
"type": "document"
},
{
"type": "string"
}]
}
}
}`))
err = tpl.Execute(file, map[string]string{
"indexName": indexName,
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

cmd := exec.Command(cliPath,
clustersEntity,
searchEntity,
indexEntity,
"create",
"--clusterName", g.clusterName,
"--file",
fileName,
"--projectId", g.projectID,
"-o=json")

cmd.Env = os.Environ()
resp, err := cmd.CombinedOutput()

if err != nil {
t.Fatalf("unexpected error: %v, resp: %v", err, string(resp))
}
var index atlasv2.FTSIndex
if err := json.Unmarshal(resp, &index); assert.NoError(t, err) {
assert.Equal(t, index.Name, indexName)
}
})
}

0 comments on commit 1b3d574

Please sign in to comment.