Skip to content

Commit 7572936

Browse files
authored
Synonyms API - PUT Synonym Rule request (#96865)
1 parent f69094f commit 7572936

File tree

18 files changed

+680
-193
lines changed

18 files changed

+680
-193
lines changed

modules/analysis-common/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ esplugin {
1919

2020
restResources {
2121
restApi {
22-
include '_common', 'indices', 'index', 'cluster', 'search', 'nodes', 'bulk', 'termvectors', 'explain', 'count', 'synonyms.put', 'synonyms.delete'
22+
include '_common', 'indices', 'index', 'cluster', 'search', 'nodes', 'bulk', 'termvectors', 'explain', 'count', 'synonyms.put', 'synonyms.delete', 'synonym_rule.put'
2323
}
2424
}
2525

modules/analysis-common/src/yamlRestTest/resources/rest-api-spec/test/analysis-common/70_synonyms_from_index.yml

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
---
2-
"Load and auto-reload synonyms from index for an analyzer":
1+
setup:
32
- skip:
43
version: " - 8.8.99"
54
reason: Loading synonyms from index is introduced in 8.9.0
@@ -11,8 +10,9 @@
1110
body:
1211
synonyms_set:
1312
- synonyms: "hello, hi"
13+
id: "synonym-rule-1"
1414
- synonyms: "bye => goodbye"
15-
- match: { result: "created" }
15+
id: "synonym-rule-2"
1616

1717
# Create an index with synonym_filter that uses that synonyms set
1818
- do:
@@ -47,6 +47,8 @@
4747
- '{"my_field": "hello"}'
4848
- '{"index": {"_index": "my_index", "_id": "2"}}'
4949
- '{"my_field": "goodbye"}'
50+
---
51+
"Load synonyms from index for an analyzer":
5052

5153
# Confirm that synonyms from the synonyms set are used
5254
- do:
@@ -69,7 +71,8 @@
6971
query: bye
7072
- match: { hits.total.value: 1 }
7173

72-
# Update synonyms and check reload status
74+
---
75+
"Update the synonym set and auto-reload analyzer":
7376
- do:
7477
synonyms.put:
7578
synonyms_set: set1
@@ -103,7 +106,43 @@
103106
query: ciao
104107
- match: { hits.total.value: 1 }
105108

106-
# Delete the synonyms set and confirm failed reload analyzers details
109+
---
110+
"Update a single synonym rule and auto-reload analyzer":
111+
- do:
112+
synonym_rule.put:
113+
synonyms_set: set1
114+
synonym_rule: "synonym-rule-1"
115+
body:
116+
synonyms: "hello, hola"
117+
- match: { result: "updated" }
118+
- gte: { reload_analyzers_details._shards.total: 1 }
119+
- match: { reload_analyzers_details.reload_details.0.index: "my_index" }
120+
- match: { reload_analyzers_details.reload_details.0.reloaded_analyzers.0 : "my_analyzer" }
121+
122+
# Confirm that the index analyzers are reloaded
123+
- do:
124+
search:
125+
index: my_index
126+
body:
127+
query:
128+
match:
129+
my_field:
130+
query: hola
131+
- match: { hits.total.value: 1 }
132+
133+
# Other synonym rules are unchanged
134+
- do:
135+
search:
136+
index: my_index
137+
body:
138+
query:
139+
match:
140+
my_field:
141+
query: bye
142+
- match: { hits.total.value: 1 }
143+
144+
---
145+
"Delete the synonyms set and confirm failed reload analyzers details":
107146
- do:
108147
synonyms.delete:
109148
synonyms_set: set1
@@ -122,7 +161,7 @@
122161
query:
123162
match:
124163
my_field:
125-
query: salute
164+
query: hi
126165
- match: { hits.total.value: 1 }
127166

128167
- do:
@@ -132,18 +171,14 @@
132171
query:
133172
match:
134173
my_field:
135-
query: ciao
174+
query: bye
136175
- match: { hits.total.value: 1 }
137176

138177
---
139178
"Fail loading synonyms from index if synonyms_set doesn't exist":
140-
- skip:
141-
version: " - 8.8.99"
142-
reason: Loading synonyms from index is introduced in 8.9.0
143-
144179
- do:
145180
indices.create:
146-
index: my_index
181+
index: another_index
147182
body:
148183
settings:
149184
index:
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"synonym_rule.put": {
3+
"documentation": {
4+
"url": "https://www.elastic.co/guide/en/elasticsearch/reference/master/put-synonym-rule.html",
5+
"description": "Creates or updates a synonym rule in a synonym set"
6+
},
7+
"stability": "experimental",
8+
"visibility": "feature_flag",
9+
"feature_flag": "es.synonyms_feature_flag_enabled",
10+
"headers": {
11+
"accept": [
12+
"application/json"
13+
],
14+
"content_type": [
15+
"application/json"
16+
]
17+
},
18+
"url": {
19+
"paths": [
20+
{
21+
"path": "/_synonyms/{synonyms_set}/{synonym_rule}",
22+
"methods": [
23+
"PUT"
24+
],
25+
"parts": {
26+
"synonyms_set": {
27+
"type": "string",
28+
"description": "The id of the synonym set to be updated with the synonym rule"
29+
},
30+
"synonym_rule": {
31+
"type": "string",
32+
"description": "The id of the synonym rule to be updated or created"
33+
}
34+
}
35+
}
36+
]
37+
},
38+
"body": {
39+
"description": "Synonym rule",
40+
"required": true
41+
}
42+
}
43+
}

rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/synonyms/10_synonyms_put.yml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
---
2-
"Create and Update synonyms set":
1+
setup:
32
- skip:
43
version: " - 8.8.99"
54
reason: Introduced in 8.9.0
5+
6+
---
7+
"Create and Update synonyms set":
68
- do:
79
synonyms.put:
810
synonyms_set: test-update-synonyms
@@ -29,9 +31,6 @@
2931

3032
---
3133
"Validation fails tests":
32-
- skip:
33-
version: " - 8.8.99"
34-
reason: Introduced in 8.9.0
3534
- do:
3635
catch: /\[synonyms\] field can't be empty/
3736
synonyms.put:
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
setup:
2+
- skip:
3+
version: " - 8.8.99"
4+
reason: Introduced in 8.9.0
5+
- do:
6+
synonyms.put:
7+
synonyms_set: test-synonyms
8+
body:
9+
synonyms_set:
10+
- synonyms: "hello, hi"
11+
id: "test-id-1"
12+
- synonyms: "bye => goodbye"
13+
id: "test-id-2"
14+
- synonyms: "test => check"
15+
id: "test-id-3"
16+
17+
18+
---
19+
"Update a synonyms rule":
20+
- do:
21+
synonym_rule.put:
22+
synonyms_set: "test-synonyms"
23+
synonym_rule: "test-id-2"
24+
body:
25+
synonyms: "bye, goodbye, seeya"
26+
27+
- match: { result: "updated" }
28+
- match: { reload_analyzers_details._shards.total: 0 }
29+
- length: { reload_analyzers_details.reload_details: 0 }
30+
31+
- do:
32+
synonyms.get:
33+
synonyms_set: test-synonyms
34+
35+
- match:
36+
synonyms_set:
37+
- synonyms: "hello, hi"
38+
id: "test-id-1"
39+
- synonyms: "bye, goodbye, seeya"
40+
id: "test-id-2"
41+
- synonyms: "test => check"
42+
id: "test-id-3"
43+
44+
45+
---
46+
"Create a new synonym rule":
47+
- do:
48+
synonym_rule.put:
49+
synonyms_set: "test-synonyms"
50+
synonym_rule: "test-id-0"
51+
body:
52+
synonyms: "i-phone, iphone"
53+
54+
- match: { result: "created" }
55+
- match: { reload_analyzers_details._shards.total: 0 }
56+
- length: { reload_analyzers_details.reload_details: 0 }
57+
58+
- do:
59+
synonyms.get:
60+
synonyms_set: test-synonyms
61+
62+
- match:
63+
synonyms_set:
64+
- synonyms: "i-phone, iphone"
65+
id: "test-id-0"
66+
- synonyms: "hello, hi"
67+
id: "test-id-1"
68+
- synonyms: "bye => goodbye"
69+
id: "test-id-2"
70+
- synonyms: "test => check"
71+
id: "test-id-3"
72+
73+
---
74+
"Update synonym rule for non existing synonym set":
75+
76+
- do:
77+
catch: missing
78+
synonym_rule.put:
79+
synonyms_set: "test-synonyms-non-existent"
80+
synonym_rule: "test-id-0"
81+
body:
82+
synonyms: "i-phone, iphone"

server/src/main/java/org/elasticsearch/action/ActionModule.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,12 @@
254254
import org.elasticsearch.action.synonyms.DeleteSynonymsAction;
255255
import org.elasticsearch.action.synonyms.GetSynonymsAction;
256256
import org.elasticsearch.action.synonyms.GetSynonymsSetsAction;
257+
import org.elasticsearch.action.synonyms.PutSynonymRuleAction;
257258
import org.elasticsearch.action.synonyms.PutSynonymsAction;
258259
import org.elasticsearch.action.synonyms.TransportDeleteSynonymsAction;
259260
import org.elasticsearch.action.synonyms.TransportGetSynonymsAction;
260261
import org.elasticsearch.action.synonyms.TransportGetSynonymsSetsAction;
262+
import org.elasticsearch.action.synonyms.TransportPutSynonymRuleAction;
261263
import org.elasticsearch.action.synonyms.TransportPutSynonymsAction;
262264
import org.elasticsearch.action.termvectors.MultiTermVectorsAction;
263265
import org.elasticsearch.action.termvectors.TermVectorsAction;
@@ -446,6 +448,7 @@
446448
import org.elasticsearch.rest.action.synonyms.RestDeleteSynonymsAction;
447449
import org.elasticsearch.rest.action.synonyms.RestGetSynonymsAction;
448450
import org.elasticsearch.rest.action.synonyms.RestGetSynonymsSetsAction;
451+
import org.elasticsearch.rest.action.synonyms.RestPutSynonymRuleAction;
449452
import org.elasticsearch.rest.action.synonyms.RestPutSynonymsAction;
450453
import org.elasticsearch.synonyms.SynonymsAPI;
451454
import org.elasticsearch.tasks.Task;
@@ -795,6 +798,7 @@ public <Request extends ActionRequest, Response extends ActionResponse> void reg
795798
actions.register(GetSynonymsAction.INSTANCE, TransportGetSynonymsAction.class);
796799
actions.register(DeleteSynonymsAction.INSTANCE, TransportDeleteSynonymsAction.class);
797800
actions.register(GetSynonymsSetsAction.INSTANCE, TransportGetSynonymsSetsAction.class);
801+
actions.register(PutSynonymRuleAction.INSTANCE, TransportPutSynonymRuleAction.class);
798802
}
799803

800804
return unmodifiableMap(actions.getRegistry());
@@ -1012,6 +1016,7 @@ public void initRestHandlers(Supplier<DiscoveryNodes> nodesInCluster) {
10121016
registerHandler.accept(new RestGetSynonymsAction());
10131017
registerHandler.accept(new RestDeleteSynonymsAction());
10141018
registerHandler.accept(new RestGetSynonymsSetsAction());
1019+
registerHandler.accept(new RestPutSynonymRuleAction());
10151020
}
10161021
}
10171022

0 commit comments

Comments
 (0)