|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.esql.plan.logical.inference; |
| 9 | + |
| 10 | +import org.elasticsearch.common.io.stream.NamedWriteableRegistry; |
| 11 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 12 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 13 | +import org.elasticsearch.inference.TaskType; |
| 14 | +import org.elasticsearch.xpack.esql.core.capabilities.Resolvables; |
| 15 | +import org.elasticsearch.xpack.esql.core.expression.Alias; |
| 16 | +import org.elasticsearch.xpack.esql.core.expression.AttributeSet; |
| 17 | +import org.elasticsearch.xpack.esql.core.expression.Expressions; |
| 18 | +import org.elasticsearch.xpack.esql.core.tree.NodeInfo; |
| 19 | +import org.elasticsearch.xpack.esql.core.tree.Source; |
| 20 | +import org.elasticsearch.xpack.esql.io.stream.PlanStreamInput; |
| 21 | +import org.elasticsearch.xpack.esql.plan.logical.LogicalPlan; |
| 22 | +import org.elasticsearch.xpack.esql.plan.logical.UnaryPlan; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Objects; |
| 27 | + |
| 28 | +import static org.elasticsearch.xpack.esql.core.expression.Expressions.asAttributes; |
| 29 | + |
| 30 | +public class Rerank extends InferencePlan { |
| 31 | + |
| 32 | + public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry(LogicalPlan.class, "Rerank", Rerank::new); |
| 33 | + private final String queryText; |
| 34 | + private final List<Alias> rerankFields; |
| 35 | + |
| 36 | + public Rerank(Source source, LogicalPlan child, String inferenceId, String queryText, List<Alias> rerankFields) { |
| 37 | + super(source, child, inferenceId); |
| 38 | + this.queryText = queryText; |
| 39 | + this.rerankFields = rerankFields; |
| 40 | + } |
| 41 | + |
| 42 | + public Rerank(StreamInput in) throws IOException { |
| 43 | + this( |
| 44 | + Source.readFrom((PlanStreamInput) in), |
| 45 | + in.readNamedWriteable(LogicalPlan.class), |
| 46 | + in.readString(), |
| 47 | + in.readString(), |
| 48 | + in.readCollectionAsList(Alias::new) |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void writeTo(StreamOutput out) throws IOException { |
| 54 | + super.writeTo(out); |
| 55 | + out.writeString(queryText); |
| 56 | + out.writeCollection(rerankFields()); |
| 57 | + } |
| 58 | + |
| 59 | + public String queryText() { |
| 60 | + return queryText; |
| 61 | + } |
| 62 | + |
| 63 | + public List<Alias> rerankFields() { |
| 64 | + return rerankFields; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public TaskType taskType() { |
| 69 | + return TaskType.RERANK; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public String getWriteableName() { |
| 74 | + return ENTRY.name; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public UnaryPlan replaceChild(LogicalPlan newChild) { |
| 79 | + return new Rerank(source(), newChild, inferenceId(), queryText, rerankFields); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + protected AttributeSet computeReferences() { |
| 84 | + return computeReferences(rerankFields); |
| 85 | + } |
| 86 | + |
| 87 | + public static AttributeSet computeReferences(List<Alias> fields) { |
| 88 | + AttributeSet generated = new AttributeSet(asAttributes(fields)); |
| 89 | + return Expressions.references(fields).subtract(generated); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public boolean expressionsResolved() { |
| 94 | + return Resolvables.resolved(rerankFields); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + protected NodeInfo<? extends LogicalPlan> info() { |
| 99 | + return NodeInfo.create(this, Rerank::new, child(), inferenceId(), queryText, rerankFields); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public boolean equals(Object o) { |
| 104 | + if (this == o) return true; |
| 105 | + if (o == null || getClass() != o.getClass()) return false; |
| 106 | + if (super.equals(o) == false) return false; |
| 107 | + Rerank rerank = (Rerank) o; |
| 108 | + return Objects.equals(queryText, rerank.queryText) && Objects.equals(rerankFields, rerank.rerankFields); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public int hashCode() { |
| 113 | + return Objects.hash(super.hashCode(), queryText, rerankFields); |
| 114 | + } |
| 115 | +} |
0 commit comments