Skip to content

Commit f95dbba

Browse files
committed
update the rule schema
Signed-off-by: Ruirui Zhang <mariazrr@amazon.com>
1 parent aa1f8b7 commit f95dbba

File tree

9 files changed

+676
-292
lines changed

9 files changed

+676
-292
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.autotagging;
10+
11+
import org.opensearch.core.common.io.stream.StreamInput;
12+
import org.opensearch.core.common.io.stream.StreamOutput;
13+
import org.opensearch.core.common.io.stream.Writeable;
14+
15+
import java.io.IOException;
16+
17+
/**
18+
* Represents an attribute within the auto-tagging feature. Attributes define characteristics that can
19+
* be used for tagging and classification. Implementations of this interface are responsible for
20+
* registering attributes in {@link AutoTaggingRegistry}. Implementations must ensure that attributes
21+
* are uniquely identifiable by their class and name.
22+
*
23+
* Implementers should follow these guidelines:
24+
* Attributes should be singletons and managed centrally to avoid duplicates.
25+
* {@link #registerAttribute()} must be called during initialization to ensure the attribute is available.
26+
*
27+
* @opensearch.experimental
28+
*/
29+
public interface Attribute extends Writeable {
30+
String getName();
31+
32+
void registerAttribute();
33+
34+
@Override
35+
default void writeTo(StreamOutput out) throws IOException {
36+
out.writeString(getClass().getName());
37+
out.writeString(getName());
38+
}
39+
40+
static Attribute from(StreamInput in) throws IOException {
41+
return AutoTaggingRegistry.getAttribute(in.readString(), in.readString());
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.autotagging;
10+
11+
import org.opensearch.ResourceNotFoundException;
12+
import org.opensearch.common.collect.Tuple;
13+
14+
import java.util.HashMap;
15+
import java.util.Map;
16+
17+
/**
18+
* Registry for managing auto-tagging attributes and feature types.
19+
* This class provides functionality to register and retrieve {@link Attribute} and {@link FeatureType} instances
20+
* used for auto-tagging.
21+
*
22+
* @opensearch.experimental
23+
*/
24+
public class AutoTaggingRegistry {
25+
public static final Map<Tuple<String, String>, FeatureType> featureTypesRegistryMap = new HashMap<>();
26+
public static final Map<Tuple<String, String>, Attribute> attributeRegistryMap = new HashMap<>();
27+
28+
public static void registerFeatureType(FeatureType featureType) {
29+
if (featureType == null) {
30+
throw new IllegalStateException("Feature type is not initialized and can't be registered");
31+
}
32+
featureTypesRegistryMap.put(new Tuple<>(featureType.getClass().getName(), featureType.getName()), featureType);
33+
}
34+
35+
public static void registerAttribute(Attribute attribute) {
36+
if (attribute == null) {
37+
throw new IllegalStateException("Attribute is not initialized and can't be registered");
38+
}
39+
attributeRegistryMap.put(new Tuple<>(attribute.getClass().getName(), attribute.getName()), attribute);
40+
}
41+
42+
/**
43+
* Retrieves the registered {@link FeatureType} instance based on class name and feature type name.
44+
* This method assumes that FeatureTypes are singletons, meaning that each unique
45+
* (className, featureTypeName) pair corresponds to a single, globally shared instance.
46+
*
47+
* @param className The name of the class associated with the feature type.
48+
* @param featureTypeName The name of the feature type.
49+
*/
50+
public static FeatureType getFeatureType(String className, String featureTypeName) {
51+
FeatureType featureType = featureTypesRegistryMap.get(new Tuple<>(className, featureTypeName));
52+
if (featureType == null) {
53+
throw new ResourceNotFoundException(
54+
"Couldn't find a feature type with name: "
55+
+ featureTypeName
56+
+ " under the class: "
57+
+ className
58+
+ ". Make sure you have registered it."
59+
);
60+
}
61+
return featureType;
62+
}
63+
64+
/**
65+
* Retrieves the registered {@link Attribute} instance based on the class name and attribute name.
66+
* This method assumes that Attributes are singletons, meaning that each unique
67+
* (className, attributeName) pair corresponds to a single, globally shared instance.
68+
*
69+
* @param className The name of the class associated with the attribute.
70+
* @param attributeName The name of the attribute.
71+
*/
72+
public static Attribute getAttribute(String className, String attributeName) {
73+
Attribute attribute = attributeRegistryMap.get(new Tuple<>(className, attributeName));
74+
if (attribute == null) {
75+
throw new ResourceNotFoundException(
76+
"Couldn't find a attribute with name: "
77+
+ attributeName
78+
+ " under the class: "
79+
+ className
80+
+ ". Make sure you have registered it."
81+
);
82+
}
83+
return attribute;
84+
}
85+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.autotagging;
10+
11+
import org.opensearch.core.common.io.stream.StreamInput;
12+
import org.opensearch.core.common.io.stream.StreamOutput;
13+
import org.opensearch.core.common.io.stream.Writeable;
14+
15+
import java.io.IOException;
16+
import java.util.Set;
17+
18+
/**
19+
* Represents a feature type within the auto-tagging feature. Feature types define different categories of
20+
* characteristics that can be used for tagging and classification. Implementations of this interface are
21+
* responsible for registering feature types in {@link AutoTaggingRegistry}. Implementations must ensure that
22+
* feature types are uniquely identifiable by their class and name.
23+
*
24+
* Implementers should follow these guidelines:
25+
* Feature types should be singletons and managed centrally to avoid duplicates.
26+
* {@link #registerFeatureType()} must be called during initialization to ensure the feature type is available.
27+
*
28+
* @opensearch.experimental
29+
*/
30+
public interface FeatureType extends Writeable {
31+
String getName();
32+
33+
int getMaxNumberOfValuesPerAttribute();
34+
35+
int getMaxCharLengthPerAttributeValue();
36+
37+
Set<Attribute> getAllowedAttributes();
38+
39+
void registerFeatureType();
40+
41+
default boolean isValidAttribute(Attribute attribute) {
42+
return getAllowedAttributes().contains(attribute);
43+
}
44+
45+
default Attribute getAttributeFromName(String fieldName) {
46+
return getAllowedAttributes().stream().filter(attr -> attr.getName().equals(fieldName)).findFirst().orElse(null);
47+
}
48+
49+
@Override
50+
default void writeTo(StreamOutput out) throws IOException {
51+
out.writeString(getClass().getName());
52+
out.writeString(getName());
53+
}
54+
55+
static FeatureType from(StreamInput in) throws IOException {
56+
return AutoTaggingRegistry.getFeatureType(in.readString(), in.readString());
57+
}
58+
}

0 commit comments

Comments
 (0)