|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | +package org.opensearch.transport.grpc.proto.request.search.query; |
| 9 | + |
| 10 | +import org.opensearch.index.query.AbstractQueryBuilder; |
| 11 | +import org.opensearch.index.query.BoostingQueryBuilder; |
| 12 | +import org.opensearch.index.query.QueryBuilder; |
| 13 | +import org.opensearch.protobufs.BoostingQuery; |
| 14 | +import org.opensearch.protobufs.QueryContainer; |
| 15 | +import org.opensearch.transport.grpc.spi.QueryBuilderProtoConverterRegistry; |
| 16 | + |
| 17 | +/** |
| 18 | + * Utility class for converting BoostingQuery Protocol Buffers to OpenSearch query objects. |
| 19 | + * This class provides methods to transform Protocol Buffer representations of boosting queries |
| 20 | + * into their corresponding OpenSearch BoostingQueryBuilder implementations for search operations. |
| 21 | + */ |
| 22 | +class BoostingQueryBuilderProtoUtils { |
| 23 | + |
| 24 | + private BoostingQueryBuilderProtoUtils() { |
| 25 | + // Utility class, no instances |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Converts a Protocol Buffer BoostingQuery to an OpenSearch BoostingQueryBuilder. |
| 30 | + * Similar to {@link BoostingQueryBuilder#fromXContent(org.opensearch.core.xcontent.XContentParser)}, this method |
| 31 | + * parses the Protocol Buffer representation and creates a properly configured |
| 32 | + * BoostingQueryBuilder with the appropriate positive, negative, negative_boost, boost, and name settings. |
| 33 | + * |
| 34 | + * @param boostingQueryProto The Protocol Buffer BoostingQuery to convert |
| 35 | + * @param registry The registry to use for converting nested queries |
| 36 | + * @return A configured BoostingQueryBuilder instance |
| 37 | + * @throws IllegalArgumentException if required fields are missing |
| 38 | + */ |
| 39 | + static BoostingQueryBuilder fromProto(BoostingQuery boostingQueryProto, QueryBuilderProtoConverterRegistry registry) { |
| 40 | + // Extract fields in the order they appear in fromXContent |
| 41 | + QueryBuilder positiveQuery = null; |
| 42 | + QueryBuilder negativeQuery = null; |
| 43 | + float negativeBoost = -1; |
| 44 | + float boost = AbstractQueryBuilder.DEFAULT_BOOST; |
| 45 | + String queryName = null; |
| 46 | + |
| 47 | + // Process positive query (required in proto) |
| 48 | + QueryContainer positiveContainer = boostingQueryProto.getPositive(); |
| 49 | + positiveQuery = registry.fromProto(positiveContainer); |
| 50 | + if (positiveQuery == null) { |
| 51 | + throw new IllegalArgumentException(BoostingQueryBuilder.POSITIVE_QUERY_REQUIRED); |
| 52 | + } |
| 53 | + |
| 54 | + // Process negative query (required in proto) |
| 55 | + QueryContainer negativeContainer = boostingQueryProto.getNegative(); |
| 56 | + negativeQuery = registry.fromProto(negativeContainer); |
| 57 | + if (negativeQuery == null) { |
| 58 | + throw new IllegalArgumentException(BoostingQueryBuilder.NEGATIVE_QUERY_REQUIRED); |
| 59 | + } |
| 60 | + |
| 61 | + // Process negative_boost (required in proto, but validate it's positive) |
| 62 | + negativeBoost = boostingQueryProto.getNegativeBoost(); |
| 63 | + if (negativeBoost < 0) { |
| 64 | + throw new IllegalArgumentException(BoostingQueryBuilder.NEGATIVE_BOOST_POSITIVE_VALUE_REQUIRED); |
| 65 | + } |
| 66 | + |
| 67 | + // Process boost (optional) |
| 68 | + if (boostingQueryProto.hasBoost()) { |
| 69 | + boost = boostingQueryProto.getBoost(); |
| 70 | + } |
| 71 | + |
| 72 | + // Process queryName (optional) |
| 73 | + if (boostingQueryProto.hasXName()) { |
| 74 | + queryName = boostingQueryProto.getXName(); |
| 75 | + } |
| 76 | + |
| 77 | + // Build the query in the same order as fromXContent |
| 78 | + BoostingQueryBuilder boostingQuery = new BoostingQueryBuilder(positiveQuery, negativeQuery); |
| 79 | + boostingQuery.negativeBoost(negativeBoost); |
| 80 | + boostingQuery.boost(boost); |
| 81 | + boostingQuery.queryName(queryName); |
| 82 | + |
| 83 | + return boostingQuery; |
| 84 | + } |
| 85 | +} |
0 commit comments