From 205542004e31fd6a2c9fec12ea74312874029263 Mon Sep 17 00:00:00 2001 From: Kathleen DeRusso Date: Mon, 18 Mar 2024 16:47:42 -0400 Subject: [PATCH] Add index as a property --- .../xpack/application/rules/QueryRule.java | 4 ++-- .../application/rules/QueryRuleCriteria.java | 8 ++++---- .../application/rules/QueryRuleCriteriaType.java | 2 -- .../rules/QueryRulesAnalysisConfig.java | 16 +++++++++------- .../rules/QueryRulesAnalysisService.java | 13 ++++++++----- .../application/rules/RuleQueryBuilder.java | 2 +- 6 files changed, 24 insertions(+), 21 deletions(-) diff --git a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/QueryRule.java b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/QueryRule.java index 7fa8554ebb93c..9a7bd28533c13 100644 --- a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/QueryRule.java +++ b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/QueryRule.java @@ -278,7 +278,7 @@ public String toString() { } @SuppressWarnings("unchecked") - public AppliedQueryRules applyRule(Client client, String index, AppliedQueryRules appliedRules, Map matchCriteria) { + public AppliedQueryRules applyRule(Client client, AppliedQueryRules appliedRules, Map matchCriteria) { if (type != QueryRule.QueryRuleType.PINNED) { throw new UnsupportedOperationException("Only pinned query rules are supported"); } @@ -295,7 +295,7 @@ public AppliedQueryRules applyRule(Client client, String index, AppliedQueryRule final String criteriaMetadata = criterion.criteriaMetadata(); if (criteriaType == ALWAYS || (criteriaMetadata != null && criteriaMetadata.equals(match))) { - boolean singleCriterionMatches = criterion.isMatch(client, index, matchValue, criteriaType, false); + boolean singleCriterionMatches = criterion.isMatch(client, matchValue, criteriaType, false); isRuleMatch = (isRuleMatch == null) ? singleCriterionMatches : isRuleMatch && singleCriterionMatches; } } diff --git a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/QueryRuleCriteria.java b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/QueryRuleCriteria.java index 4dfbbfbc02dbe..8df11465167bf 100644 --- a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/QueryRuleCriteria.java +++ b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/QueryRuleCriteria.java @@ -221,11 +221,11 @@ public String toString() { return Strings.toString(this); } - public boolean isMatch(Client client, String index, Object matchValue, QueryRuleCriteriaType matchType) { - return isMatch(client, index, matchValue, matchType, true); + public boolean isMatch(Client client, Object matchValue, QueryRuleCriteriaType matchType) { + return isMatch(client, matchValue, matchType, true); } - public boolean isMatch(Client client, String index, Object matchValue, QueryRuleCriteriaType matchType, boolean throwOnInvalidInput) { + public boolean isMatch(Client client, Object matchValue, QueryRuleCriteriaType matchType, boolean throwOnInvalidInput) { if (matchType == ALWAYS) { return true; } @@ -236,7 +236,7 @@ public boolean isMatch(Client client, String index, Object matchValue, QueryRule return false; } QueryRulesAnalysisService analysisService = new QueryRulesAnalysisService(client); - boolean matchFound = matchType.isMatch(analysisService, index, matchString, criteriaValue, criteriaProperties); + boolean matchFound = matchType.isMatch(analysisService, matchString, criteriaValue, criteriaProperties); if (matchFound) { return true; } diff --git a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/QueryRuleCriteriaType.java b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/QueryRuleCriteriaType.java index 7702e9aa9603b..35d2c7cac6235 100644 --- a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/QueryRuleCriteriaType.java +++ b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/QueryRuleCriteriaType.java @@ -104,7 +104,6 @@ public void validateInput(Object input) { public boolean isMatch( QueryRulesAnalysisService analysisService, - String index, Object input, Object criteriaValue, Map criteriaProperties @@ -114,7 +113,6 @@ public boolean isMatch( List> analysisChain = (List>) criteriaProperties.get("analysis"); QueryRulesAnalysisService.AnalyzedContent analyzedContent = analysisService.analyzeContent( analysisChain, - index, (String) input, (String) criteriaValue ); diff --git a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/QueryRulesAnalysisConfig.java b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/QueryRulesAnalysisConfig.java index bcc64835e9a33..04cda22755ed5 100644 --- a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/QueryRulesAnalysisConfig.java +++ b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/QueryRulesAnalysisConfig.java @@ -10,20 +10,22 @@ import java.util.List; import java.util.Map; -public record QueryRulesAnalysisConfig(String analyzer, String tokenizer, List filters) { +public record QueryRulesAnalysisConfig(String index, String analyzer, String tokenizer, List filters) { - public QueryRulesAnalysisConfig(String analyzer, String tokenizer, List filters) { - this.analyzer = analyzer; - this.tokenizer = tokenizer; - this.filters = filters == null ? List.of() : filters; - } + // public QueryRulesAnalysisConfig(String index, String analyzer, String tokenizer, List filters) { + // this.index = index; + // this.analyzer = analyzer; + // this.tokenizer = tokenizer; + // this.filters = filters == null ? List.of() : filters; + // } public static QueryRulesAnalysisConfig fromMap(Map configurationAttributes) { + String index = (String) configurationAttributes.get("index"); String analyzer = (String) configurationAttributes.get("analyzer"); String tokenizer = (String) configurationAttributes.get("tokenizer"); @SuppressWarnings("unchecked") List filters = (List) configurationAttributes.getOrDefault("filters", List.of()); - return new QueryRulesAnalysisConfig(analyzer, tokenizer, filters); + return new QueryRulesAnalysisConfig(index, analyzer, tokenizer, filters); } diff --git a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/QueryRulesAnalysisService.java b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/QueryRulesAnalysisService.java index 91f082ef1cfe3..f592bdab03c48 100644 --- a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/QueryRulesAnalysisService.java +++ b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/QueryRulesAnalysisService.java @@ -32,11 +32,14 @@ public QueryRulesAnalysisService(Client client) { this.clientWithOrigin = new OriginSettingClient(client, ENT_SEARCH_ORIGIN); } - public String analyze(String index, String text, QueryRulesAnalysisConfig analysisConfig) { + public String analyze(String text, QueryRulesAnalysisConfig analysisConfig) { String analyzer = analysisConfig.analyzer(); String tokenizer = analysisConfig.tokenizer(); List filters = analysisConfig.filters(); - AnalyzeAction.Request analyzeRequest = new AnalyzeAction.Request().text(text).index(index); + AnalyzeAction.Request analyzeRequest = new AnalyzeAction.Request().text(text); + if (analysisConfig.index() != null) { + analyzeRequest.index(analysisConfig.index()); + } if (analyzer != null) { analyzeRequest.analyzer(analyzer); } @@ -51,13 +54,13 @@ public String analyze(String index, String text, QueryRulesAnalysisConfig analys return analyzeTokens.stream().map(AnalyzeAction.AnalyzeToken::getTerm).collect(Collectors.joining(" ")); } - public AnalyzedContent analyzeContent(List> analysisChain, String index, String input, String criteriaValue) { + public AnalyzedContent analyzeContent(List> analysisChain, String input, String criteriaValue) { String analyzedInput = input; String analyzedCriteriaValue = criteriaValue; for (Map analysisConfig : analysisChain) { QueryRulesAnalysisConfig analysisConfigObj = QueryRulesAnalysisConfig.fromMap(analysisConfig); - analyzedInput = analyze(index, analyzedInput, analysisConfigObj); - analyzedCriteriaValue = analyze(index, analyzedCriteriaValue, analysisConfigObj); + analyzedInput = analyze(analyzedInput, analysisConfigObj); + analyzedCriteriaValue = analyze(analyzedCriteriaValue, analysisConfigObj); logger.info("analyzedInput: " + analyzedInput + "; analyzedCriteriaValue: " + analyzedCriteriaValue); } return new AnalyzedContent(analyzedInput, analyzedCriteriaValue); diff --git a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/RuleQueryBuilder.java b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/RuleQueryBuilder.java index 876a3829c4867..5208921a93426 100644 --- a/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/RuleQueryBuilder.java +++ b/x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/rules/RuleQueryBuilder.java @@ -213,7 +213,7 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) { QueryRuleset queryRuleset = QueryRuleset.fromXContentBytes(rulesetId, getResponse.getSourceAsBytesRef(), XContentType.JSON); for (QueryRule rule : queryRuleset.rules()) { - rule.applyRule(client, "test", appliedRules, matchCriteria); + rule.applyRule(client, appliedRules, matchCriteria); } pinnedIdsSetOnce.set(appliedRules.pinnedIds().stream().distinct().toList()); pinnedDocsSetOnce.set(appliedRules.pinnedDocs().stream().distinct().toList());