|
19 | 19 | import org.elasticsearch.search.retriever.CompoundRetrieverBuilder; |
20 | 20 | import org.elasticsearch.search.retriever.RetrieverBuilder; |
21 | 21 | import org.elasticsearch.search.retriever.StandardRetrieverBuilder; |
| 22 | +import org.elasticsearch.xpack.rank.linear.LinearRetrieverBuilder; |
22 | 23 |
|
23 | 24 | import java.util.ArrayList; |
24 | 25 | import java.util.Collection; |
|
32 | 33 | import static org.elasticsearch.action.ValidateActions.addValidationError; |
33 | 34 | import static org.elasticsearch.index.IndexSettings.DEFAULT_FIELD_SETTING; |
34 | 35 |
|
| 36 | +/** |
| 37 | + * A utility class for managing and validating the multi-fields query format for the {@link LinearRetrieverBuilder} retriever. |
| 38 | + */ |
35 | 39 | public class MultiFieldsInnerRetrieverUtils { |
36 | 40 | private MultiFieldsInnerRetrieverUtils() {} |
37 | 41 |
|
38 | 42 | public record WeightedRetrieverSource(CompoundRetrieverBuilder.RetrieverSource retrieverSource, float weight) {} |
39 | 43 |
|
| 44 | + /** |
| 45 | + * Validates the parameters related to the multi-fields query format. |
| 46 | + * |
| 47 | + * @param innerRetrievers The custom inner retrievers already defined |
| 48 | + * @param fields The fields to query |
| 49 | + * @param query The query text |
| 50 | + * @param retrieverName The containing retriever name |
| 51 | + * @param retrieversParamName The parameter name for custom inner retrievers |
| 52 | + * @param fieldsParamName The parameter name for the fields to query |
| 53 | + * @param queryParamName The parameter name for the query text |
| 54 | + * @param validationException The validation exception |
| 55 | + * @return The validation exception with any multi-fields query format validation errors appended |
| 56 | + */ |
40 | 57 | public static ActionRequestValidationException validateParams( |
41 | 58 | List<CompoundRetrieverBuilder.RetrieverSource> innerRetrievers, |
42 | 59 | List<String> fields, |
@@ -86,6 +103,33 @@ public static ActionRequestValidationException validateParams( |
86 | 103 | return validationException; |
87 | 104 | } |
88 | 105 |
|
| 106 | + /** |
| 107 | + * Generate the inner retriever tree for the given fields, weights, and query. The tree follows this structure: |
| 108 | + * |
| 109 | + * <pre> |
| 110 | + * multi_match query on all lexical fields |
| 111 | + * normalizer retriever |
| 112 | + * match query on semantic_text field A |
| 113 | + * match query on semantic_text field B |
| 114 | + * ... |
| 115 | + * match query on semantic_text field Z |
| 116 | + * </pre> |
| 117 | + * |
| 118 | + * <p> |
| 119 | + * Where the normalizer retriever is constructed by the {@code innerNormalizerGenerator} function. |
| 120 | + * </p> |
| 121 | + * <p> |
| 122 | + * This tree structure is repeated for each index in {@code indicesMetadata}. That is to say, that for each index in |
| 123 | + * {@code indicesMetadata}, (up to) a pair of retrievers will be added to the returned {@code RetrieverBuilder} list. |
| 124 | + * </p> |
| 125 | + * |
| 126 | + * @param fieldsAndWeights The fields to query and their respective weights, in "field^weight" format |
| 127 | + * @param query The query text |
| 128 | + * @param indicesMetadata The metadata for the indices to search |
| 129 | + * @param innerNormalizerGenerator The inner normalizer retriever generator function |
| 130 | + * @param weightValidator The field weight validator |
| 131 | + * @return The inner retriever tree as a {@code RetrieverBuilder} list |
| 132 | + */ |
89 | 133 | public static List<RetrieverBuilder> generateInnerRetrievers( |
90 | 134 | List<String> fieldsAndWeights, |
91 | 135 | String query, |
|
0 commit comments