-
Notifications
You must be signed in to change notification settings - Fork 2.3k
[rule based autotagging] Add Create Rule API Logic #17792
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jainankitk
merged 20 commits into
opensearch-project:main
from
ruai0511:create-rule-api-PR
Jun 5, 2025
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
7d6d919
add get rule api logic
ruai0511 4be74e4
modify based on comments
ruai0511 1816b0b
rebase from main after the schema merged
ruai0511 cdf8e19
modify based on comments
ruai0511 28ca8b1
extract common logics to libs
ruai0511 7de0cd2
Add javadocs for libs
ruai0511 dc1a8b8
modify based on comments
ruai0511 35abfd3
rebase
ruai0511 5b48e3c
modify based on comments
ruai0511 1ab33c1
Add create rule api
ruai0511 b888ec4
modify based on comments
ruai0511 1706b8a
rebase issues
1104137
rebase after get api is merged
ruai0511 6b0497b
modify based on comments
ruai0511 8dbb658
address comments
ruai0511 1af4bb7
revise based on comments
ruai0511 8dfab4c
addrss comments
ruai0511 74beb60
change create rule api to run only on master node to avoid race condi…
ruai0511 656e01c
customize transport layer to route to primary shard's node
ruai0511 778645c
adding plugin-specific routing layer
ruai0511 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
modules/autotagging-commons/common/src/main/java/org/opensearch/rule/CreateRuleRequest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.rule; | ||
|
||
import org.opensearch.action.ActionRequest; | ||
import org.opensearch.action.ActionRequestValidationException; | ||
import org.opensearch.core.common.io.stream.StreamInput; | ||
import org.opensearch.core.common.io.stream.StreamOutput; | ||
import org.opensearch.rule.autotagging.Rule; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* A request for create Rule | ||
* Example request: | ||
* curl -X PUT "localhost:9200/_rules/{featureType}/" -H 'Content-Type: application/json' -d ' | ||
* { | ||
* "description": "description1", | ||
* "attribute_name": ["log*", "event*"], | ||
* "feature_type": "poOiU851RwyLYvV5lbvv5w" | ||
* }' | ||
* @opensearch.experimental | ||
*/ | ||
public class CreateRuleRequest extends ActionRequest { | ||
private final Rule rule; | ||
|
||
/** | ||
* constructor for CreateRuleRequest | ||
* @param rule the rule to create | ||
*/ | ||
public CreateRuleRequest(Rule rule) { | ||
this.rule = rule; | ||
} | ||
|
||
/** | ||
* Constructs a CreateRuleRequest from a StreamInput for deserialization | ||
* @param in - The {@link StreamInput} instance to read from. | ||
*/ | ||
public CreateRuleRequest(StreamInput in) throws IOException { | ||
super(in); | ||
rule = new Rule(in); | ||
} | ||
|
||
@Override | ||
public ActionRequestValidationException validate() { | ||
try { | ||
rule.getFeatureType().getFeatureValueValidator().validate(rule.getFeatureValue()); | ||
return null; | ||
} catch (Exception e) { | ||
ActionRequestValidationException validationException = new ActionRequestValidationException(); | ||
validationException.addValidationError("Validation failed: " + e.getMessage()); | ||
return validationException; | ||
Check warning on line 58 in modules/autotagging-commons/common/src/main/java/org/opensearch/rule/CreateRuleRequest.java
|
||
} | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
super.writeTo(out); | ||
rule.writeTo(out); | ||
} | ||
|
||
/** | ||
* rule getter | ||
*/ | ||
public Rule getRule() { | ||
return rule; | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
modules/autotagging-commons/common/src/main/java/org/opensearch/rule/CreateRuleResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.rule; | ||
|
||
import org.opensearch.core.action.ActionResponse; | ||
import org.opensearch.core.common.io.stream.StreamInput; | ||
import org.opensearch.core.common.io.stream.StreamOutput; | ||
import org.opensearch.core.xcontent.ToXContent; | ||
import org.opensearch.core.xcontent.ToXContentObject; | ||
import org.opensearch.core.xcontent.XContentBuilder; | ||
import org.opensearch.rule.autotagging.Rule; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
import static org.opensearch.rule.autotagging.Rule._ID_STRING; | ||
|
||
/** | ||
* Response for the create API for Rule | ||
* Example response: | ||
* { | ||
* "_id":"wi6VApYBoX5wstmtU_8l", | ||
* "description":"description1", | ||
* "index_pattern":["log*", "uvent*"], | ||
* "workload_group":"poOiU851RwyLYvV5lbvv5w", | ||
* "updated_at":"2025-04-04T20:54:22.406Z" | ||
* } | ||
* @opensearch.experimental | ||
*/ | ||
public class CreateRuleResponse extends ActionResponse implements ToXContent, ToXContentObject { | ||
private final String _id; | ||
private final Rule rule; | ||
|
||
/** | ||
* contructor for CreateRuleResponse | ||
* @param id - the id for the rule created | ||
* @param rule - the rule created | ||
*/ | ||
public CreateRuleResponse(String id, final Rule rule) { | ||
this._id = id; | ||
this.rule = rule; | ||
} | ||
|
||
/** | ||
* Constructs a CreateRuleResponse from a StreamInput for deserialization | ||
* @param in - The {@link StreamInput} instance to read from. | ||
*/ | ||
public CreateRuleResponse(StreamInput in) throws IOException { | ||
_id = in.readString(); | ||
rule = new Rule(in); | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
out.writeString(_id); | ||
rule.writeTo(out); | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
return rule.toXContent(builder, new MapParams(Map.of(_ID_STRING, _id))); | ||
} | ||
|
||
/** | ||
* rule getter | ||
*/ | ||
public Rule getRule() { | ||
return rule; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
modules/autotagging-commons/common/src/main/java/org/opensearch/rule/RuleRoutingService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.rule; | ||
|
||
import org.opensearch.core.action.ActionListener; | ||
|
||
/** | ||
* Interface that handles rule routing logic | ||
* @opensearch.experimental | ||
*/ | ||
public interface RuleRoutingService { | ||
ruai0511 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Handles a create rule request by routing it to the appropriate node. | ||
* @param request the create rule request | ||
* @param listener listener to handle the final response | ||
*/ | ||
void handleCreateRuleRequest(CreateRuleRequest request, ActionListener<CreateRuleResponse> listener); | ||
} |
67 changes: 67 additions & 0 deletions
67
modules/autotagging-commons/common/src/main/java/org/opensearch/rule/RuleUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.rule; | ||
|
||
import org.opensearch.common.annotation.ExperimentalApi; | ||
import org.opensearch.rule.autotagging.Attribute; | ||
import org.opensearch.rule.autotagging.Rule; | ||
|
||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
/** | ||
* Utility class for operations related to {@link Rule} objects. | ||
* @opensearch.experimental | ||
*/ | ||
@ExperimentalApi | ||
public class RuleUtils { | ||
|
||
/** | ||
* constructor for RuleUtils | ||
*/ | ||
public RuleUtils() {} | ||
|
||
/** | ||
* Checks if a duplicate rule exists and returns its id. | ||
* Two rules are considered to be duplicate when meeting all the criteria below | ||
* 1. They have the same feature type | ||
* 2. They have the exact same attributes | ||
* 3. For each attribute, the sets of values must intersect — i.e., at least one common value must exist | ||
* between the current rule and the one being checked. | ||
* | ||
* @param rule The rule to be validated against ruleMap. | ||
* @param ruleMap This map contains existing rules to be checked | ||
*/ | ||
public static Optional<String> getDuplicateRuleId(Rule rule, Map<String, Rule> ruleMap) { | ||
Map<Attribute, Set<String>> targetAttributeMap = rule.getAttributeMap(); | ||
for (Map.Entry<String, Rule> entry : ruleMap.entrySet()) { | ||
Rule currRule = entry.getValue(); | ||
Map<Attribute, Set<String>> existingAttributeMap = currRule.getAttributeMap(); | ||
|
||
if (rule.getFeatureType() != currRule.getFeatureType() || targetAttributeMap.size() != existingAttributeMap.size()) { | ||
continue; | ||
} | ||
boolean allAttributesIntersect = true; | ||
for (Attribute attribute : targetAttributeMap.keySet()) { | ||
Set<String> targetAttributeValues = targetAttributeMap.get(attribute); | ||
Set<String> existingAttributeValues = existingAttributeMap.get(attribute); | ||
if (existingAttributeValues == null || Collections.disjoint(targetAttributeValues, existingAttributeValues)) { | ||
ruai0511 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
allAttributesIntersect = false; | ||
break; | ||
} | ||
} | ||
if (allAttributesIntersect) { | ||
return Optional.of(entry.getKey()); | ||
} | ||
} | ||
return Optional.empty(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...g-commons/common/src/main/java/org/opensearch/rule/autotagging/FeatureValueValidator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.rule.autotagging; | ||
|
||
/** | ||
* Interface for validating a feature value against pre-defined values (such as | ||
* values from the index, cluster state, etc.) for a specific feature type. | ||
* @opensearch.experimental | ||
*/ | ||
public interface FeatureValueValidator { | ||
/** | ||
* Validates the given feature value. | ||
* @param featureValue the value to validate | ||
*/ | ||
void validate(String featureValue); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.