Skip to content

Commit f7ebe22

Browse files
authored
bug fix for update rule api (opensearch-project#18488)
Signed-off-by: Ruirui Zhang <[email protected]>
1 parent 8a59ca1 commit f7ebe22

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1414
- Bump OpenSearch Core main branch to 3.0.0 ([#18039](https://github.com/opensearch-project/OpenSearch/pull/18039))
1515
- [Rule based Auto-tagging] Add wlm `ActionFilter` ([#17791](https://github.com/opensearch-project/OpenSearch/pull/17791))
1616
- [Rule based auto-tagging] Add update rule API ([#17797](https://github.com/opensearch-project/OpenSearch/pull/17797))
17+
- [Rule based auto-tagging] Bug fix for update rule api ([#18488](https://github.com/opensearch-project/OpenSearch/pull/18488))
1718
- Update API of Message in index to add the timestamp for lag calculation in ingestion polling ([#17977](https://github.com/opensearch-project/OpenSearch/pull/17977/))
1819
- Add Warm Disk Threshold Allocation Decider for Warm shards ([#18082](https://github.com/opensearch-project/OpenSearch/pull/18082))
1920
- Add composite directory factory ([#17988](https://github.com/opensearch-project/OpenSearch/pull/17988))

modules/autotagging-commons/common/src/main/java/org/opensearch/rule/RuleUtils.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,12 @@ public static String computeRuleHash(
6767
public static Optional<String> getDuplicateRuleId(Rule rule, List<Rule> ruleList) {
6868
Map<Attribute, Set<String>> targetAttributeMap = rule.getAttributeMap();
6969
for (Rule currRule : ruleList) {
70-
Map<Attribute, Set<String>> existingAttributeMap = currRule.getAttributeMap();
70+
String currRuleId = currRule.getId();
71+
if (currRuleId.equals(rule.getId())) {
72+
continue;
73+
}
7174

75+
Map<Attribute, Set<String>> existingAttributeMap = currRule.getAttributeMap();
7276
if (rule.getFeatureType() != currRule.getFeatureType() || targetAttributeMap.size() != existingAttributeMap.size()) {
7377
continue;
7478
}
@@ -82,7 +86,7 @@ public static Optional<String> getDuplicateRuleId(Rule rule, List<Rule> ruleList
8286
}
8387
}
8488
if (allAttributesIntersect) {
85-
return Optional.of(currRule.getId());
89+
return Optional.of(currRuleId);
8690
}
8791
}
8892
return Optional.empty();

modules/autotagging-commons/common/src/test/java/org/opensearch/rule/RuleUtilsTests.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Optional;
2020
import java.util.Set;
2121

22+
import static org.opensearch.rule.utils.RuleTestUtils.ATTRIBUTE_MAP;
2223
import static org.opensearch.rule.utils.RuleTestUtils.ATTRIBUTE_VALUE_ONE;
2324
import static org.opensearch.rule.utils.RuleTestUtils.ATTRIBUTE_VALUE_TWO;
2425
import static org.opensearch.rule.utils.RuleTestUtils.DESCRIPTION_ONE;
@@ -28,15 +29,25 @@
2829
import static org.opensearch.rule.utils.RuleTestUtils.MockRuleAttributes;
2930
import static org.opensearch.rule.utils.RuleTestUtils.TIMESTAMP_ONE;
3031
import static org.opensearch.rule.utils.RuleTestUtils._ID_ONE;
32+
import static org.opensearch.rule.utils.RuleTestUtils._ID_TWO;
3133
import static org.opensearch.rule.utils.RuleTestUtils.ruleOne;
3234
import static org.opensearch.rule.utils.RuleTestUtils.ruleTwo;
3335

3436
public class RuleUtilsTests extends OpenSearchTestCase {
3537

3638
public void testDuplicateRuleFound() {
37-
Optional<String> result = RuleUtils.getDuplicateRuleId(ruleOne, List.of(ruleOne, ruleTwo));
39+
Rule testRule = Rule.builder()
40+
.id(_ID_TWO)
41+
.description(DESCRIPTION_ONE)
42+
.featureType(RuleTestUtils.MockRuleFeatureType.INSTANCE)
43+
.featureValue(FEATURE_VALUE_ONE)
44+
.attributeMap(ATTRIBUTE_MAP)
45+
.updatedAt(TIMESTAMP_ONE)
46+
.build();
47+
48+
Optional<String> result = RuleUtils.getDuplicateRuleId(ruleOne, List.of(testRule));
3849
assertTrue(result.isPresent());
39-
assertEquals(_ID_ONE, result.get());
50+
assertEquals(_ID_TWO, result.get());
4051
}
4152

4253
public void testNoAttributeIntersection() {
@@ -66,7 +77,7 @@ public void testAttributeSizeMismatch() {
6677

6778
public void testPartialAttributeValueIntersection() {
6879
Rule ruleWithPartialOverlap = Rule.builder()
69-
.id(_ID_ONE)
80+
.id(_ID_TWO)
7081
.description(DESCRIPTION_ONE)
7182
.featureType(RuleTestUtils.MockRuleFeatureType.INSTANCE)
7283
.featureValue(FEATURE_VALUE_ONE)
@@ -79,6 +90,11 @@ public void testPartialAttributeValueIntersection() {
7990
assertEquals(_ID_ONE, result.get());
8091
}
8192

93+
public void testDuplicateRuleWithSameId() {
94+
Optional<String> result = RuleUtils.getDuplicateRuleId(ruleOne, List.of(ruleOne));
95+
assertFalse(result.isPresent());
96+
}
97+
8298
public void testDifferentFeatureTypes() {
8399
Rule differentFeatureTypeRule = Rule.builder()
84100
.id(_ID_ONE)

0 commit comments

Comments
 (0)