Skip to content

Commit e0d622d

Browse files
authored
Fix Rule-based auto tagging label resolving logic (opensearch-project#19599)
Signed-off-by: Ruirui Zhang <mariazrr@amazon.com>
1 parent 2695c93 commit e0d622d

4 files changed

Lines changed: 23 additions & 16 deletions

File tree

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@
1111
import org.opensearch.rule.MatchLabel;
1212

1313
import java.util.ArrayList;
14-
import java.util.HashSet;
1514
import java.util.List;
1615
import java.util.Optional;
17-
import java.util.Set;
1816

1917
/**
2018
* This interface provides apis to store Rule attribute values
@@ -56,8 +54,8 @@ default List<MatchLabel<V>> getMatches(K key) {
5654
* Returns the values that exactly match the given key.
5755
* @param key the key to look up
5856
*/
59-
default Set<V> getExactMatch(K key) {
60-
return new HashSet<>();
57+
default List<MatchLabel<V>> getExactMatch(K key) {
58+
return new ArrayList<>();
6159
}
6260

6361
/**

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import org.opensearch.rule.MatchLabel;
1313

1414
import java.util.ArrayList;
15-
import java.util.Collections;
1615
import java.util.HashSet;
1716
import java.util.List;
1817
import java.util.Optional;
@@ -80,12 +79,12 @@ public Optional<V> get(K key) {
8079
}
8180

8281
@Override
83-
public Set<V> getExactMatch(K key) {
82+
public List<MatchLabel<V>> getExactMatch(K key) {
8483
readLock.lock();
8584
try {
86-
Set<V> results = new HashSet<>();
87-
results.addAll(trie.getOrDefault(key, Collections.emptySet()));
88-
results.addAll(trie.getOrDefault("", Collections.emptySet()));
85+
List<MatchLabel<V>> results = new ArrayList<>();
86+
addMatches(results, trie.get(key), 1f);
87+
addMatches(results, trie.get(""), 0f);
8988
return results;
9089
} finally {
9190
readLock.unlock();
@@ -100,13 +99,10 @@ public List<MatchLabel<V>> getMatches(String key) {
10099
StringBuilder prefixBuilder = new StringBuilder(key);
101100

102101
for (int i = key.length(); i >= 0; i--) {
103-
String prefix = prefixBuilder.toString();
104-
Set<V> value = trie.get(prefix);
105-
if (value != null && !value.isEmpty()) {
106-
float matchScore = (float) prefixBuilder.length() / key.length();
107-
for (V label : value) {
108-
results.add(new MatchLabel<>(label, matchScore));
109-
}
102+
Set<V> values = trie.get(prefixBuilder.toString());
103+
if (values != null && !values.isEmpty()) {
104+
float score = (float) prefixBuilder.length() / key.length();
105+
addMatches(results, values, score);
110106
}
111107
if (!prefixBuilder.isEmpty()) {
112108
prefixBuilder.deleteCharAt(prefixBuilder.length() - 1);
@@ -118,6 +114,15 @@ public List<MatchLabel<V>> getMatches(String key) {
118114
}
119115
}
120116

117+
private void addMatches(List<MatchLabel<V>> results, Set<V> values, float score) {
118+
if (values == null || values.isEmpty()) {
119+
return;
120+
}
121+
for (V label : values) {
122+
results.add(new MatchLabel<>(label, score));
123+
}
124+
}
125+
121126
@Override
122127
public void clear() {
123128
writeLock.lock();

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,5 +174,8 @@ public int size() {
174174
List<MatchLabel<String>> result = store.getMatches("foo");
175175
assertNotNull(result);
176176
assertTrue(result.isEmpty());
177+
List<MatchLabel<String>> exactMatches = store.getExactMatch("foo");
178+
assertNotNull(exactMatches);
179+
assertTrue(exactMatches.isEmpty());
177180
}
178181
}

release-notes/opensearch.release-notes-3.3.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ Compatible with OpenSearch and OpenSearch Dashboards version 3.3.0
111111
* Remove unnecessary iteration per-shard in request cache cleanup ([#19263](https://github.com/opensearch-project/OpenSearch/pull/19263))
112112
* Fix derived field rewrite to handle range queries ([#19496](https://github.com/opensearch-project/OpenSearch/pull/19496))
113113
* Fix incorrect rewriting of terms query with more than two consecutive whole numbers ([#19587](https://github.com/opensearch-project/OpenSearch/pull/19587))
114+
* Fix Rule-based auto tagging label resolving logic ([#19599](https://github.com/opensearch-project/OpenSearch/pull/19599))
114115

115116
### Dependencies
116117
* Bump `com.gradleup.shadow:shadow-gradle-plugin` from 8.3.5 to 8.3.9 ([#19400](https://github.com/opensearch-project/OpenSearch/pull/19400))

0 commit comments

Comments
 (0)