Skip to content

Commit 0c6a090

Browse files
authored
CLOUDP-98679: Add support for synonyms (#245)
* CLOUDP-98679: Add support for synonyms * Convert spaces to tabs * Add unit tests * Fix lint
1 parent 2812432 commit 0c6a090

File tree

2 files changed

+153
-80
lines changed

2 files changed

+153
-80
lines changed

mongodbatlas/search.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ type SearchIndex struct {
251251
Name string `json:"name"`
252252
SearchAnalyzer string `json:"searchAnalyzer,omitempty"`
253253
Status string `json:"status,omitempty"`
254+
Synonyms []map[string]interface{} `json:"synonyms,omitempty"`
254255
}
255256

256257
// IndexMapping containing index specifications for the collection fields.

mongodbatlas/search_test.go

Lines changed: 152 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -35,34 +35,52 @@ func TestSearch_ListIndexes(t *testing.T) {
3535
mux.HandleFunc(fmt.Sprintf("/api/atlas/v1.0/groups/%s/clusters/%s/fts/indexes/%s/%s", groupID, clusterName, databaseName, collectionName), func(w http.ResponseWriter, r *http.Request) {
3636
testMethod(t, r, http.MethodGet)
3737
fmt.Fprint(w, `[
38-
{
39-
"collectionName": "movies",
40-
"database": "sample_mflix",
41-
"indexID": "5d114a3587d9d65de99e7371",
42-
"mappings": {
43-
"dynamic": true
44-
},
45-
"name": "default"
46-
},
47-
{
48-
"collectionName": "movies",
49-
"database": "sample_mflix",
50-
"indexID": "5d1268a980eef518dac0cf41",
51-
"mappings": {
52-
"dynamic": false,
53-
"fields": {
54-
"genres" : {
55-
"analyzer": "lucene.standard",
56-
"type": "string"
38+
{
39+
"collectionName": "movies",
40+
"database": "sample_mflix",
41+
"indexID": "5d114a3587d9d65de99e7371",
42+
"mappings": {
43+
"dynamic": true
5744
},
58-
"plot": {
59-
"analyzer": "lucene.standard",
60-
"type": "string"
61-
}
62-
}
45+
"name": "default",
46+
"synonyms": [
47+
{
48+
"analyzer": "lucene.english",
49+
"name": "mySynonyms",
50+
"source": {
51+
"collection": "synonyms"
52+
}
53+
}
54+
]
6355
},
64-
"name": "SearchIndex1"
65-
}
56+
{
57+
"collectionName": "movies",
58+
"database": "sample_mflix",
59+
"indexID": "5d1268a980eef518dac0cf41",
60+
"mappings": {
61+
"dynamic": false,
62+
"fields": {
63+
"genres" : {
64+
"analyzer": "lucene.standard",
65+
"type": "string"
66+
},
67+
"plot": {
68+
"analyzer": "lucene.standard",
69+
"type": "string"
70+
}
71+
}
72+
},
73+
"name": "SearchIndex1",
74+
"synonyms": [
75+
{
76+
"analyzer": "lucene.english",
77+
"name": "mySynonyms",
78+
"source": {
79+
"collection": "synonyms"
80+
}
81+
}
82+
]
83+
}
6684
]`)
6785
})
6886

@@ -80,6 +98,15 @@ func TestSearch_ListIndexes(t *testing.T) {
8098
Dynamic: true,
8199
},
82100
Name: "default",
101+
Synonyms: []map[string]interface{}{
102+
{
103+
"analyzer": "lucene.english",
104+
"name": "mySynonyms",
105+
"source": map[string]interface{}{
106+
"collection": "synonyms",
107+
},
108+
},
109+
},
83110
},
84111
{
85112
CollectionName: "movies",
@@ -99,6 +126,15 @@ func TestSearch_ListIndexes(t *testing.T) {
99126
},
100127
},
101128
Name: "SearchIndex1",
129+
Synonyms: []map[string]interface{}{
130+
{
131+
"analyzer": "lucene.english",
132+
"name": "mySynonyms",
133+
"source": map[string]interface{}{
134+
"collection": "synonyms",
135+
},
136+
},
137+
},
102138
},
103139
}
104140

@@ -118,23 +154,32 @@ func TestSearch_GetIndex(t *testing.T) {
118154
mux.HandleFunc(fmt.Sprintf("/api/atlas/v1.0/groups/%s/clusters/%s/fts/indexes/%s", projectID, clusterName, indexID), func(w http.ResponseWriter, r *http.Request) {
119155
testMethod(t, r, http.MethodGet)
120156
fmt.Fprint(w, `{
121-
"collectionName": "movies",
122-
"database": "sample_mflix",
123-
"indexID": "5d1268a980eef518dac0cf41",
124-
"mappings": {
125-
"dynamic": false,
126-
"fields": {
127-
"genres": {
128-
"analyzer": "lucene.standard",
129-
"type": "string"
130-
},
131-
"plot": {
132-
"analyzer": "lucene.standard",
133-
"type": "string"
134-
}
135-
}
136-
},
137-
"name": "SearchIndex1"
157+
"collectionName": "movies",
158+
"database": "sample_mflix",
159+
"indexID": "5d1268a980eef518dac0cf41",
160+
"mappings": {
161+
"dynamic": false,
162+
"fields": {
163+
"genres": {
164+
"analyzer": "lucene.standard",
165+
"type": "string"
166+
},
167+
"plot": {
168+
"analyzer": "lucene.standard",
169+
"type": "string"
170+
}
171+
}
172+
},
173+
"name": "SearchIndex1",
174+
"synonyms": [
175+
{
176+
"analyzer": "lucene.english",
177+
"name": "mySynonyms",
178+
"source": {
179+
"collection": "synonyms"
180+
}
181+
}
182+
]
138183
}`)
139184
})
140185

@@ -161,6 +206,15 @@ func TestSearch_GetIndex(t *testing.T) {
161206
},
162207
},
163208
Name: "SearchIndex1",
209+
Synonyms: []map[string]interface{}{
210+
{
211+
"analyzer": "lucene.english",
212+
"name": "mySynonyms",
213+
"source": map[string]interface{}{
214+
"collection": "synonyms",
215+
},
216+
},
217+
},
164218
}
165219

166220
if diff := deep.Equal(index, expected); diff != nil {
@@ -203,13 +257,22 @@ func TestSearchServiceOp_CreateIndex(t *testing.T) {
203257
}
204258

205259
jsonBlob := `{
206-
"collectionName" : "orders",
207-
"database" : "fiscalYear2018",
208-
"indexID" : "5d12990380eef5303341accd",
209-
"mappings" : {
210-
"dynamic" : true
211-
},
212-
"name" : "default"
260+
"collectionName" : "orders",
261+
"database" : "fiscalYear2018",
262+
"indexID" : "5d12990380eef5303341accd",
263+
"mappings" : {
264+
"dynamic" : true
265+
},
266+
"name" : "default",
267+
"synonyms": [
268+
{
269+
"analyzer": "lucene.english",
270+
"name": "mySynonyms",
271+
"source": {
272+
"collection": "synonyms"
273+
}
274+
}
275+
]
213276
}`
214277
fmt.Fprint(w, jsonBlob)
215278
})
@@ -225,6 +288,15 @@ func TestSearchServiceOp_CreateIndex(t *testing.T) {
225288
IndexID: "5d12990380eef5303341accd",
226289
Mappings: &IndexMapping{Dynamic: true},
227290
Name: "default",
291+
Synonyms: []map[string]interface{}{
292+
{
293+
"analyzer": "lucene.english",
294+
"name": "mySynonyms",
295+
"source": map[string]interface{}{
296+
"collection": "synonyms",
297+
},
298+
},
299+
},
228300
}
229301

230302
if diff := deep.Equal(index, expected); diff != nil {
@@ -272,15 +344,15 @@ func TestSearchServiceOp_UpdateIndex(t *testing.T) {
272344
}
273345

274346
jsonBlob := `{
275-
"analyzer" : "lucene.swedish",
276-
"searchAnalyzer" : "lucene.whitespace",
277-
"collectionName" : "orders",
278-
"database" : "fiscalYear2018",
279-
"indexID" : "5d129aef87d9d64f310bd79f",
280-
"mappings" : {
281-
"dynamic" : true
282-
},
283-
"name" : "2018ordersIndex"
347+
"analyzer" : "lucene.swedish",
348+
"searchAnalyzer" : "lucene.whitespace",
349+
"collectionName" : "orders",
350+
"database" : "fiscalYear2018",
351+
"indexID" : "5d129aef87d9d64f310bd79f",
352+
"mappings" : {
353+
"dynamic" : true
354+
},
355+
"name" : "2018ordersIndex"
284356
}`
285357
fmt.Fprint(w, jsonBlob)
286358
})
@@ -330,16 +402,16 @@ func TestSearch_ListAnalyzers(t *testing.T) {
330402
mux.HandleFunc(fmt.Sprintf("/api/atlas/v1.0/groups/%s/clusters/%s/fts/analyzers", groupID, clusterName), func(w http.ResponseWriter, r *http.Request) {
331403
testMethod(t, r, http.MethodGet)
332404
fmt.Fprint(w, `[
333-
{
334-
"baseAnalyzer" : "lucene.standard",
335-
"maxTokenLength" : 32,
336-
"name" : "my_new_analyzer"
337-
},
338-
{
339-
"baseAnalyzer" : "lucene.english",
340-
"name" : "my_new_analyzer2",
341-
"stopwords" : [ "first", "second", "third", "etc" ]
342-
}
405+
{
406+
"baseAnalyzer" : "lucene.standard",
407+
"maxTokenLength" : 32,
408+
"name" : "my_new_analyzer"
409+
},
410+
{
411+
"baseAnalyzer" : "lucene.english",
412+
"name" : "my_new_analyzer2",
413+
"stopwords" : [ "first", "second", "third", "etc" ]
414+
}
343415
]`)
344416
})
345417

@@ -374,17 +446,17 @@ func TestSearch_UpdateAllAnalyzers(t *testing.T) {
374446
mux.HandleFunc(fmt.Sprintf("/api/atlas/v1.0/groups/%s/clusters/%s/fts/analyzers", groupID, clusterName), func(w http.ResponseWriter, r *http.Request) {
375447
testMethod(t, r, http.MethodPut)
376448
fmt.Fprint(w, `[
377-
{
378-
"baseAnalyzer" : "lucene.standard",
379-
"maxTokenLength" : 32,
380-
"name" : "my_new_analyzer",
381-
"ignoreCase": true
382-
},
383-
{
384-
"baseAnalyzer" : "lucene.english",
385-
"name" : "my_new_analyzer2",
386-
"stopwords" : [ "first", "second", "third", "etc" ]
387-
}
449+
{
450+
"baseAnalyzer" : "lucene.standard",
451+
"maxTokenLength" : 32,
452+
"name" : "my_new_analyzer",
453+
"ignoreCase": true
454+
},
455+
{
456+
"baseAnalyzer" : "lucene.english",
457+
"name" : "my_new_analyzer2",
458+
"stopwords" : [ "first", "second", "third", "etc" ]
459+
}
388460
]`)
389461
})
390462

0 commit comments

Comments
 (0)