forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add specific QueryRuleRankDoc for better explain support
- Loading branch information
Showing
4 changed files
with
107 additions
and
4 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
102 changes: 102 additions & 0 deletions
102
...h/src/main/java/org/elasticsearch/xpack/application/rules/retriever/QueryRuleRankDoc.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,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; | ||
} | ||
} |
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