Skip to content

Commit

Permalink
Add index as a property
Browse files Browse the repository at this point in the history
  • Loading branch information
kderusso committed Mar 18, 2024
1 parent ba9c585 commit 2055420
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ public String toString() {
}

@SuppressWarnings("unchecked")
public AppliedQueryRules applyRule(Client client, String index, AppliedQueryRules appliedRules, Map<String, Object> matchCriteria) {
public AppliedQueryRules applyRule(Client client, AppliedQueryRules appliedRules, Map<String, Object> matchCriteria) {
if (type != QueryRule.QueryRuleType.PINNED) {
throw new UnsupportedOperationException("Only pinned query rules are supported");
}
Expand All @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ public void validateInput(Object input) {

public boolean isMatch(
QueryRulesAnalysisService analysisService,
String index,
Object input,
Object criteriaValue,
Map<String, Object> criteriaProperties
Expand All @@ -114,7 +113,6 @@ public boolean isMatch(
List<Map<String, Object>> analysisChain = (List<Map<String, Object>>) criteriaProperties.get("analysis");
QueryRulesAnalysisService.AnalyzedContent analyzedContent = analysisService.analyzeContent(
analysisChain,
index,
(String) input,
(String) criteriaValue
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,22 @@
import java.util.List;
import java.util.Map;

public record QueryRulesAnalysisConfig(String analyzer, String tokenizer, List<String> filters) {
public record QueryRulesAnalysisConfig(String index, String analyzer, String tokenizer, List<String> filters) {

public QueryRulesAnalysisConfig(String analyzer, String tokenizer, List<String> filters) {
this.analyzer = analyzer;
this.tokenizer = tokenizer;
this.filters = filters == null ? List.of() : filters;
}
// public QueryRulesAnalysisConfig(String index, String analyzer, String tokenizer, List<String> filters) {
// this.index = index;
// this.analyzer = analyzer;
// this.tokenizer = tokenizer;
// this.filters = filters == null ? List.of() : filters;
// }

public static QueryRulesAnalysisConfig fromMap(Map<String, Object> configurationAttributes) {
String index = (String) configurationAttributes.get("index");
String analyzer = (String) configurationAttributes.get("analyzer");
String tokenizer = (String) configurationAttributes.get("tokenizer");
@SuppressWarnings("unchecked")
List<String> filters = (List<String>) configurationAttributes.getOrDefault("filters", List.of());
return new QueryRulesAnalysisConfig(analyzer, tokenizer, filters);
return new QueryRulesAnalysisConfig(index, analyzer, tokenizer, filters);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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);
}
Expand All @@ -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<Map<String, Object>> analysisChain, String index, String input, String criteriaValue) {
public AnalyzedContent analyzeContent(List<Map<String, Object>> analysisChain, String input, String criteriaValue) {
String analyzedInput = input;
String analyzedCriteriaValue = criteriaValue;
for (Map<String, Object> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit 2055420

Please sign in to comment.