|
| 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", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.search.retriever; |
| 11 | + |
| 12 | +import org.elasticsearch.action.ActionRequestValidationException; |
| 13 | +import org.elasticsearch.index.query.QueryBuilder; |
| 14 | +import org.elasticsearch.index.query.QueryRewriteContext; |
| 15 | +import org.elasticsearch.search.builder.SearchSourceBuilder; |
| 16 | +import org.elasticsearch.search.rank.RankDoc; |
| 17 | +import org.elasticsearch.xcontent.XContentBuilder; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +/** |
| 23 | + * A wrapper that can be used to modify the behaviour of an existing {@link RetrieverBuilder}. |
| 24 | + */ |
| 25 | +public abstract class RetrieverBuilderWrapper<T extends RetrieverBuilder> extends RetrieverBuilder { |
| 26 | + protected final RetrieverBuilder in; |
| 27 | + |
| 28 | + protected RetrieverBuilderWrapper(RetrieverBuilder in) { |
| 29 | + this.in = in; |
| 30 | + } |
| 31 | + |
| 32 | + protected abstract T clone(RetrieverBuilder sub); |
| 33 | + |
| 34 | + @Override |
| 35 | + public RetrieverBuilder rewrite(QueryRewriteContext ctx) throws IOException { |
| 36 | + var inRewrite = in.rewrite(ctx); |
| 37 | + if (inRewrite != in) { |
| 38 | + return clone(inRewrite); |
| 39 | + } |
| 40 | + return this; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public QueryBuilder topDocsQuery() { |
| 45 | + return in.topDocsQuery(); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public RetrieverBuilder minScore(Float minScore) { |
| 50 | + return in.minScore(minScore); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public List<QueryBuilder> getPreFilterQueryBuilders() { |
| 55 | + return in.preFilterQueryBuilders; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public ActionRequestValidationException validate( |
| 60 | + SearchSourceBuilder source, |
| 61 | + ActionRequestValidationException validationException, |
| 62 | + boolean isScroll, |
| 63 | + boolean allowPartialSearchResults |
| 64 | + ) { |
| 65 | + return in.validate(source, validationException, isScroll, allowPartialSearchResults); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public RetrieverBuilder retrieverName(String retrieverName) { |
| 70 | + return in.retrieverName(retrieverName); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void setRankDocs(RankDoc[] rankDocs) { |
| 75 | + in.setRankDocs(rankDocs); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public RankDoc[] getRankDocs() { |
| 80 | + return in.getRankDocs(); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public boolean isCompound() { |
| 85 | + return in.isCompound(); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public QueryBuilder explainQuery() { |
| 90 | + return in.explainQuery(); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public Float minScore() { |
| 95 | + return in.minScore(); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public boolean isFragment() { |
| 100 | + return in.isFragment(); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public String toString() { |
| 105 | + return in.toString(); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public String retrieverName() { |
| 110 | + return in.retrieverName(); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public void extractToSearchSourceBuilder(SearchSourceBuilder searchSourceBuilder, boolean compoundUsed) { |
| 115 | + in.extractToSearchSourceBuilder(searchSourceBuilder, compoundUsed); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public String getName() { |
| 120 | + return in.getName(); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + protected void doToXContent(XContentBuilder builder, Params params) throws IOException { |
| 125 | + in.doToXContent(builder, params); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + protected boolean doEquals(Object o) { |
| 130 | + // Handle the edge case where we need to unwrap the incoming retriever |
| 131 | + if (o instanceof RetrieverBuilderWrapper<?> wrapper) { |
| 132 | + return in.doEquals(wrapper.in); |
| 133 | + } else { |
| 134 | + return in.doEquals(o); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + protected int doHashCode() { |
| 140 | + return in.doHashCode(); |
| 141 | + } |
| 142 | +} |
0 commit comments