Skip to content

Commit d60e698

Browse files
authored
Synonyms - Fixes error when no ID is specified (#97426)
1 parent d45fdbd commit d60e698

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,28 @@ setup:
33
version: " - 8.8.99"
44
reason: Introduced in 8.9.0
55

6+
---
7+
"Create synonyms with no ID creates ID automatically":
8+
- skip:
9+
version: " - 8.9.99"
10+
reason: Fixed for 8.10.0
11+
- do:
12+
synonyms.put:
13+
synonyms_set: test-update-synonyms
14+
body:
15+
synonyms_set:
16+
- synonyms: "hello, hi"
17+
- synonyms: "bye => goodbye"
18+
19+
- match: { result: "created" }
20+
21+
- do:
22+
synonyms.get:
23+
synonyms_set: test-update-synonyms
24+
25+
- is_true: synonyms_set.0.id
26+
- is_true: synonyms_set.1.id
27+
628
---
729
"Create and Update synonyms set":
830
- do:

server/src/main/java/org/elasticsearch/synonyms/SynonymRule.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
package org.elasticsearch.synonyms;
1010

11+
import org.elasticsearch.common.UUIDs;
1112
import org.elasticsearch.common.io.stream.StreamInput;
1213
import org.elasticsearch.common.io.stream.StreamOutput;
1314
import org.elasticsearch.common.io.stream.Writeable;
@@ -43,7 +44,7 @@ public class SynonymRule implements Writeable, ToXContentObject {
4344
private final String id;
4445

4546
public SynonymRule(@Nullable String id, String synonyms) {
46-
this.id = id;
47+
this.id = Objects.requireNonNullElse(id, UUIDs.base64UUID());
4748
this.synonyms = synonyms;
4849
}
4950

server/src/main/java/org/elasticsearch/synonyms/SynonymsManagementAPIService.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import org.elasticsearch.client.internal.OriginSettingClient;
3131
import org.elasticsearch.cluster.metadata.IndexMetadata;
3232
import org.elasticsearch.cluster.routing.Preference;
33-
import org.elasticsearch.common.UUIDs;
3433
import org.elasticsearch.common.settings.Settings;
3534
import org.elasticsearch.index.IndexNotFoundException;
3635
import org.elasticsearch.index.query.QueryBuilders;
@@ -384,11 +383,7 @@ private <T> void reloadAnalyzers(String resourceName, ActionListener<SynonymsRel
384383
// Retrieves the internal synonym rule ID to store it in the index. As the same synonym rule ID
385384
// can be used in different synonym sets, we prefix the ID with the synonym set to avoid collisions
386385
private static String internalSynonymRuleId(String synonymsSetId, String synonymRuleId) {
387-
if (synonymRuleId == null) {
388-
synonymRuleId = UUIDs.base64UUID();
389-
}
390-
final String id = synonymsSetId + SYNONYM_RULE_ID_SEPARATOR + synonymRuleId;
391-
return id;
386+
return synonymsSetId + SYNONYM_RULE_ID_SEPARATOR + synonymRuleId;
392387
}
393388

394389
static Settings settings() {

0 commit comments

Comments
 (0)