-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add validations for Personalize input and configurations
Description: =========== This commit adds following changes: * Change scoring logic to consider ranks instead of scores * Add validation for Personalize inputs and configurations * Add E2E tests in the form of unit tests for Personalize ranking Signed-off-by: Ketan Kulkarni <[email protected]>
- Loading branch information
Showing
10 changed files
with
594 additions
and
488 deletions.
There are no files selected for viewing
This file contains 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 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 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
65 changes: 65 additions & 0 deletions
65
...arch/search/relevance/transformer/personalizeintelligentranking/utils/ValidationUtil.java
This file contains 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,65 @@ | ||
/* | ||
* 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.search.relevance.transformer.personalizeintelligentranking.utils; | ||
import com.amazonaws.arn.Arn; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.opensearch.ingest.ConfigurationUtils; | ||
import org.opensearch.search.relevance.transformer.personalizeintelligentranking.configuration.PersonalizeIntelligentRankerConfiguration; | ||
|
||
import java.util.Arrays; | ||
import java.util.Set; | ||
import java.util.HashSet; | ||
|
||
import static org.opensearch.search.relevance.transformer.personalizeintelligentranking.configuration.Constants.AMAZON_PERSONALIZED_RANKING_RECIPE_NAME; | ||
|
||
public class ValidationUtil { | ||
Check warning on line 20 in src/main/java/org/opensearch/search/relevance/transformer/personalizeintelligentranking/utils/ValidationUtil.java
|
||
private static Set<String> SUPPORTED_PERSONALIZE_RECIPES = new HashSet<>(Arrays.asList(AMAZON_PERSONALIZED_RANKING_RECIPE_NAME)); | ||
|
||
/** | ||
* Validate Personalize configuration for calling Personalize service. | ||
* Throws OpenSearchParseException type exception if validation fails. | ||
* @param config Personalize intelligent ranker configuration | ||
* @param processorType Name of search pipeline processor | ||
* @param processorTag Name of processor tag | ||
*/ | ||
public static void validatePersonalizeIntelligentRankerConfiguration (PersonalizeIntelligentRankerConfiguration config, | ||
String processorType, | ||
String processorTag | ||
) { | ||
// Validate weight value | ||
if (config.getWeight() < 0.0 || config.getWeight() > 1.0) { | ||
throw ConfigurationUtils.newConfigurationException(processorType, processorTag, "weight", "invalid value for weight"); | ||
} | ||
// Validate Personalize campaign ARN | ||
if(!isValidCampaignOrRoleArn(config.getPersonalizeCampaign(), "personalize")) { | ||
throw ConfigurationUtils.newConfigurationException(processorType, processorTag, "campaign_arn", "invalid format for Personalize campaign arn"); | ||
} | ||
// Validate IAM Role Arn for Personalize access | ||
String iamRoleArn = config.getIamRoleArn(); | ||
if(!StringUtils.isEmpty(iamRoleArn) && !isValidCampaignOrRoleArn(iamRoleArn, "iam")) { | ||
throw ConfigurationUtils.newConfigurationException(processorType, processorTag, "iam_role_arn", "invalid format for Personalize iam role arn"); | ||
} | ||
// Validate Personalize recipe | ||
if(!SUPPORTED_PERSONALIZE_RECIPES.contains(config.getRecipe())) { | ||
throw ConfigurationUtils.newConfigurationException(processorType, processorTag, "recipe", "not supported recipe provided"); | ||
Check warning on line 49 in src/main/java/org/opensearch/search/relevance/transformer/personalizeintelligentranking/utils/ValidationUtil.java
|
||
} | ||
} | ||
|
||
private static boolean isValidCampaignOrRoleArn(String arn, String expectedService) { | ||
try { | ||
Arn arnObj = Arn.fromString(arn); | ||
String arnService = arnObj.getService(); | ||
if (arnObj.getResource() == null || !arnService.equals(expectedService)) { | ||
return false; | ||
} | ||
} catch (IllegalArgumentException iae) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
} |
Oops, something went wrong.