Skip to content

Commit

Permalink
Synonyms work with hardcoded index name
Browse files Browse the repository at this point in the history
  • Loading branch information
kderusso committed Mar 18, 2024
1 parent 544d9d7 commit ba9c585
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 70 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, AppliedQueryRules appliedRules, Map<String, Object> matchCriteria) {
public AppliedQueryRules applyRule(Client client, String index, 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, AppliedQueryRules appliedRules
final String criteriaMetadata = criterion.criteriaMetadata();

if (criteriaType == ALWAYS || (criteriaMetadata != null && criteriaMetadata.equals(match))) {
boolean singleCriterionMatches = criterion.isMatch(client, matchValue, criteriaType, false);
boolean singleCriterionMatches = criterion.isMatch(client, index, matchValue, criteriaType, false);
isRuleMatch = (isRuleMatch == null) ? singleCriterionMatches : isRuleMatch && singleCriterionMatches;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.logging.LogManager;
import org.elasticsearch.logging.Logger;
import org.elasticsearch.xcontent.ConstructingObjectParser;
import org.elasticsearch.xcontent.ParseField;
import org.elasticsearch.xcontent.ToXContentObject;
Expand Down Expand Up @@ -48,8 +46,6 @@ public class QueryRuleCriteria implements Writeable, ToXContentObject {
private final List<Object> criteriaValues;
private final Map<String, Object> criteriaProperties;

private static final Logger logger = LogManager.getLogger(QueryRuleCriteria.class);

/**
*
* @param criteriaType The {@link QueryRuleCriteriaType}, indicating how the criteria is matched
Expand Down Expand Up @@ -82,7 +78,7 @@ public QueryRuleCriteria(
this.criteriaType = criteriaType;

this.criteriaProperties = criteriaProperties == null ? Map.of() : criteriaProperties;
// TODO criteriaType.validateProperties(criteriaProperties);
// TODO validate properties

}

Expand Down Expand Up @@ -225,11 +221,11 @@ public String toString() {
return Strings.toString(this);
}

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) {
return isMatch(client, index, matchValue, matchType, true);
}

public boolean isMatch(Client client, Object matchValue, QueryRuleCriteriaType matchType, boolean throwOnInvalidInput) {
public boolean isMatch(Client client, String index, Object matchValue, QueryRuleCriteriaType matchType, boolean throwOnInvalidInput) {
if (matchType == ALWAYS) {
return true;
}
Expand All @@ -240,7 +236,7 @@ public boolean isMatch(Client client, Object matchValue, QueryRuleCriteriaType m
return false;
}
QueryRulesAnalysisService analysisService = new QueryRulesAnalysisService(client);
boolean matchFound = matchType.isMatch(analysisService, matchString, criteriaValue, criteriaProperties);
boolean matchFound = matchType.isMatch(analysisService, index, matchString, criteriaValue, criteriaProperties);
if (matchFound) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,8 @@ public boolean isMatch(Object input, Object criteriaValue, Map<String, Object> c
EXACT {
@Override
public boolean isMatch(Object input, Object criteriaValue, Map<String, Object> criteriaProperties) {
throw new UnsupportedOperationException("[" + this + "] criteria type requires analysis service");
}

@Override
public boolean isMatch(
QueryRulesAnalysisService analysisService,
Object input,
Object criteriaValue,
Map<String, Object> criteriaProperties
) {
if (input instanceof String && criteriaValue instanceof String) {

if (criteriaProperties.containsKey("analysis")) {
@SuppressWarnings("unchecked")
List<Map<String, Object>> analysisChain = (List<Map<String, Object>>) criteriaProperties.get("analysis");
String analyzedInput = (String) input;
String analyzedCriteriaValue = (String) criteriaValue;
for (Map<String, Object> analysisConfig : analysisChain) {
String tokenizer = analysisConfig.containsKey("tokenizer") ? (String) analysisConfig.get("tokenizer") : "keyword";
String filter = analysisConfig.containsKey("filter") ? (String) analysisConfig.get("filter") : "lowercase";
QueryRulesAnalysisConfig config = new QueryRulesAnalysisConfig(null, tokenizer, List.of(filter));
analyzedInput = analysisService.analyze(analyzedInput, config);
analyzedCriteriaValue = analysisService.analyze(analyzedCriteriaValue, config);
}
return analyzedInput.equals(analyzedCriteriaValue);
} else {
return input.equals(criteriaValue);
}

return input.equals(criteriaValue);
} else {
return parseDouble(input) == parseDouble(criteriaValue);
}
Expand Down Expand Up @@ -131,11 +104,24 @@ public void validateInput(Object input) {

public boolean isMatch(
QueryRulesAnalysisService analysisService,
String index,
Object input,
Object criteriaValue,
Map<String, Object> criteriaProperties
) {
return isMatch(input, criteriaValue, criteriaProperties);
if (criteriaProperties.containsKey("analysis")) {
@SuppressWarnings("unchecked")
List<Map<String, Object>> analysisChain = (List<Map<String, Object>>) criteriaProperties.get("analysis");
QueryRulesAnalysisService.AnalyzedContent analyzedContent = analysisService.analyzeContent(
analysisChain,
index,
(String) input,
(String) criteriaValue
);
return isMatch(analyzedContent.analyzedInput(), analyzedContent.analyzedCriteriaValue(), criteriaProperties);
} else {
return isMatch(input, criteriaValue, criteriaProperties);
}
}

public static QueryRuleCriteriaType type(String criteriaType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,23 @@
package org.elasticsearch.xpack.application.rules;

import java.util.List;
import java.util.Map;

public class QueryRulesAnalysisConfig {

private final String analyzer;
private final String tokenizer;
private final List<String> filters;
public record QueryRulesAnalysisConfig(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;
}

public String analyzer() {
return analyzer;
this.filters = filters == null ? List.of() : filters;
}

public String tokenizer() {
return tokenizer;
}
public static QueryRulesAnalysisConfig fromMap(Map<String, Object> configurationAttributes) {
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);

public List<String> filters() {
return filters;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import org.elasticsearch.logging.Logger;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.elasticsearch.xpack.core.ClientHelper.ENT_SEARCH_ORIGIN;

Expand All @@ -30,27 +32,37 @@ public QueryRulesAnalysisService(Client client) {
this.clientWithOrigin = new OriginSettingClient(client, ENT_SEARCH_ORIGIN);
}

public String analyze(String text, QueryRulesAnalysisConfig analysisConfig) {

logger.info("Analyzing original text [" + text + "]");

public String analyze(String index, String text, QueryRulesAnalysisConfig analysisConfig) {
String analyzer = analysisConfig.analyzer();
String tokenizer = analysisConfig.tokenizer();
List<String> filters = analysisConfig.filters();

AnalyzeAction.Request analyzeRequest = new AnalyzeAction.Request().analyzer(analyzer)
.tokenizer(tokenizer)
.addTokenFilters(filters)
.text(text);
AnalyzeAction.Request analyzeRequest = new AnalyzeAction.Request().text(text).index(index);
if (analyzer != null) {
analyzeRequest.analyzer(analyzer);
}
if (tokenizer != null) {
analyzeRequest.tokenizer(tokenizer);
}
if (filters != null) {
analyzeRequest.addTokenFilters(filters);
}
AnalyzeAction.Response analyzeResponse = clientWithOrigin.execute(AnalyzeAction.INSTANCE, analyzeRequest).actionGet(TIMEOUT_MS);
List<AnalyzeAction.AnalyzeToken> analyzeTokens = analyzeResponse.getTokens();
StringBuilder sb = new StringBuilder();
for (AnalyzeAction.AnalyzeToken analyzeToken : analyzeTokens) {
logger.info("Analyzed term: [" + analyzeToken.getTerm() + "]");
sb.append(analyzeToken.getTerm()).append(" ");
return analyzeTokens.stream().map(AnalyzeAction.AnalyzeToken::getTerm).collect(Collectors.joining(" "));
}

public AnalyzedContent analyzeContent(List<Map<String, Object>> analysisChain, String index, 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);
logger.info("analyzedInput: " + analyzedInput + "; analyzedCriteriaValue: " + analyzedCriteriaValue);
}
return sb.toString().trim();
return new AnalyzedContent(analyzedInput, analyzedCriteriaValue);
}

public record AnalyzedContent(String analyzedInput, String analyzedCriteriaValue) {}

}
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, appliedRules, matchCriteria);
rule.applyRule(client, "test", appliedRules, matchCriteria);
}
pinnedIdsSetOnce.set(appliedRules.pinnedIds().stream().distinct().toList());
pinnedDocsSetOnce.set(appliedRules.pinnedDocs().stream().distinct().toList());
Expand Down

0 comments on commit ba9c585

Please sign in to comment.