Skip to content

Commit

Permalink
Add specific QueryRuleRankDoc for better explain support
Browse files Browse the repository at this point in the history
  • Loading branch information
kderusso committed Oct 22, 2024
1 parent 8a3cecb commit f38be79
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ static TransportVersion def(int id) {
public static final TransportVersion REVERT_REMOVE_MIN_COMPATIBLE_SHARD_NODE = def(8_774_00_0);
public static final TransportVersion ESQL_FIELD_ATTRIBUTE_PARENT_SIMPLIFIED = def(8_775_00_0);
public static final TransportVersion INFERENCE_DONT_PERSIST_ON_READ = def(8_776_00_0);
public static final TransportVersion QUERY_RULES_RETRIEVER = def(8_777_00_0);

/*
* STOP! READ THIS FIRST! No, really,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,8 @@ setup:
explain: true

- match: { hits.hits.0._id: foo }
- match: { hits.hits.0._explanation.value: 1 }
- match: { hits.hits.0._explanation.description: "doc [0] with an original score of [1.7014124E38] is at rank [1] from the following source queries." }
- match: { hits.hits.0._explanation.value: 1.7014124430769804E38 }
- match: { hits.hits.0._explanation.description: "query rules evaluated rules from rulesets [test-ruleset] and match criteria {bar=bar, foo=foo}" }
- match: { hits.hits.0._explanation.details.0.value: 1 }
- match: { hits.hits.0._explanation.details.0.description: "doc [0] with an original score of [1.7014124E38] is at rank [1] from the following source queries." }
- match: { hits.hits.0._explanation.details.0.details.0.description: "sum of:" }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

package org.elasticsearch.xpack.application.rules.retriever;

import org.apache.lucene.search.Explanation;
import org.elasticsearch.TransportVersion;
import org.elasticsearch.TransportVersions;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.search.rank.RankDoc;
import org.elasticsearch.xcontent.XContentBuilder;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class QueryRuleRankDoc extends RankDoc {

public static final String NAME = "query_rule_rank_doc";

public final List<String> rulesetIds;
public final Map<String, Object> matchCriteria;

public QueryRuleRankDoc(int doc, float score, int shardIndex, List<String> rulesetIds, Map<String, Object> matchCriteria) {
super(doc, score, shardIndex);
this.rulesetIds = rulesetIds;
this.matchCriteria = matchCriteria;
}

public QueryRuleRankDoc(StreamInput in) throws IOException {
super(in);
rulesetIds = in.readStringCollectionAsImmutableList();
matchCriteria = in.readGenericMap();
}

@Override
public Explanation explain(Explanation[] sources, String[] queryNames) {

return Explanation.match(
score,
"query rules evaluated rules from rulesets " + rulesetIds + " and match criteria " + matchCriteria,
sources
);
}

@Override
public void doWriteTo(StreamOutput out) throws IOException {
out.writeStringCollection(rulesetIds);
out.writeGenericMap(matchCriteria);
}

@Override
public boolean doEquals(RankDoc rd) {
QueryRuleRankDoc qrrd = (QueryRuleRankDoc) rd;
return Objects.equals(rulesetIds, qrrd.rulesetIds) && Objects.equals(matchCriteria, qrrd.matchCriteria);
}

@Override
public int doHashCode() {
return Objects.hash(rulesetIds, matchCriteria);
}

@Override
public String toString() {
return "QueryRuleRankDoc{"
+ "doc="
+ doc
+ ", shardIndex="
+ shardIndex
+ ", score="
+ score
+ ", rulesetIds="
+ rulesetIds
+ ", matchCriteria="
+ matchCriteria
+ "}";
}

@Override
public String getWriteableName() {
return NAME;
}

@Override
protected void doToXContent(XContentBuilder builder, Params params) throws IOException {
builder.array("rulesetIds", rulesetIds.toArray());
builder.startObject("matchCriteria");
builder.mapContents(matchCriteria);
builder.endObject();
}

@Override
public TransportVersion getMinimalSupportedVersion() {
return TransportVersions.QUERY_RULES_RETRIEVER;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,10 @@ protected QueryRuleRetrieverBuilder clone(List<RetrieverSource> newChildRetrieve
protected RankDoc[] combineInnerRetrieverResults(List<ScoreDoc[]> rankResults) {
assert rankResults.size() == 1;
ScoreDoc[] scoreDocs = rankResults.getFirst();
RankDoc[] rankDocs = new RankDoc[scoreDocs.length];
RankDoc[] rankDocs = new QueryRuleRankDoc[scoreDocs.length];
for (int i = 0; i < scoreDocs.length; i++) {
ScoreDoc scoreDoc = scoreDocs[i];
rankDocs[i] = new RankDoc(scoreDoc.doc, scoreDoc.score, scoreDoc.shardIndex);
rankDocs[i] = new QueryRuleRankDoc(scoreDoc.doc, scoreDoc.score, scoreDoc.shardIndex, rulesetIds, matchCriteria);
rankDocs[i].rank = i + 1;
}
return rankDocs;
Expand Down

0 comments on commit f38be79

Please sign in to comment.