|
| 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.common.unit.Fuzziness; |
| 11 | +import org.opensearch.index.query.AbstractQueryBuilder; |
| 12 | +import org.opensearch.index.query.FuzzyQueryBuilder; |
| 13 | +import org.opensearch.protobufs.FuzzyQuery; |
| 14 | + |
| 15 | +/** |
| 16 | + * Utility class for converting FuzzyQuery Protocol Buffers to OpenSearch objects. |
| 17 | + * This class provides methods to transform Protocol Buffer representations of fuzzy queries |
| 18 | + * into their corresponding OpenSearch FuzzyQueryBuilder implementations for search operations. |
| 19 | + */ |
| 20 | +public class FuzzyQueryBuilderProtoUtils { |
| 21 | + |
| 22 | + private FuzzyQueryBuilderProtoUtils() { |
| 23 | + // Utility class, no instances |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Converts a Protocol Buffer FuzzyQuery to an OpenSearch FuzzyQueryBuilder. |
| 28 | + * Similar to {@link FuzzyQueryBuilder#fromXContent(org.opensearch.core.xcontent.XContentParser)}, this method |
| 29 | + * parses the Protocol Buffer representation and creates a properly configured |
| 30 | + * FuzzyQueryBuilder with the appropriate field name, value, fuzziness, prefix length, |
| 31 | + * max expansions, transpositions, rewrite method, boost, and query name. |
| 32 | + * |
| 33 | + * @param fuzzyQueryProto The Protocol Buffer FuzzyQuery object |
| 34 | + * @return A configured FuzzyQueryBuilder instance |
| 35 | + * @throws IllegalArgumentException if the field name or value is null or empty |
| 36 | + */ |
| 37 | + public static FuzzyQueryBuilder fromProto(FuzzyQuery fuzzyQueryProto) { |
| 38 | + String fieldName = null; |
| 39 | + String value = null; |
| 40 | + String queryName = null; |
| 41 | + float boost = AbstractQueryBuilder.DEFAULT_BOOST; |
| 42 | + String fuzziness = null; |
| 43 | + int prefixLength = FuzzyQueryBuilder.DEFAULT_PREFIX_LENGTH; |
| 44 | + int maxExpansions = FuzzyQueryBuilder.DEFAULT_MAX_EXPANSIONS; |
| 45 | + boolean transpositions = FuzzyQueryBuilder.DEFAULT_TRANSPOSITIONS; |
| 46 | + String rewrite = null; |
| 47 | + |
| 48 | + // Extract field name from the protobuf |
| 49 | + if (!fuzzyQueryProto.getField().isEmpty()) { |
| 50 | + fieldName = fuzzyQueryProto.getField(); |
| 51 | + } else { |
| 52 | + throw new IllegalArgumentException("Field name cannot be null or empty for fuzzy query"); |
| 53 | + } |
| 54 | + |
| 55 | + // Extract value from the protobuf |
| 56 | + if (fuzzyQueryProto.hasValue()) { |
| 57 | + switch (fuzzyQueryProto.getValue().getValueCase()) { |
| 58 | + case STRING_VALUE: |
| 59 | + value = fuzzyQueryProto.getValue().getStringValue(); |
| 60 | + break; |
| 61 | + case BOOL_VALUE: |
| 62 | + value = String.valueOf(fuzzyQueryProto.getValue().getBoolValue()); |
| 63 | + break; |
| 64 | + case GENERAL_NUMBER: |
| 65 | + // Handle GeneralNumber case |
| 66 | + switch (fuzzyQueryProto.getValue().getGeneralNumber().getValueCase()) { |
| 67 | + case INT32_VALUE: |
| 68 | + value = String.valueOf(fuzzyQueryProto.getValue().getGeneralNumber().getInt32Value()); |
| 69 | + break; |
| 70 | + case INT64_VALUE: |
| 71 | + value = String.valueOf(fuzzyQueryProto.getValue().getGeneralNumber().getInt64Value()); |
| 72 | + break; |
| 73 | + case FLOAT_VALUE: |
| 74 | + value = String.valueOf(fuzzyQueryProto.getValue().getGeneralNumber().getFloatValue()); |
| 75 | + break; |
| 76 | + case DOUBLE_VALUE: |
| 77 | + value = String.valueOf(fuzzyQueryProto.getValue().getGeneralNumber().getDoubleValue()); |
| 78 | + break; |
| 79 | + default: |
| 80 | + throw new IllegalArgumentException("Unsupported GeneralNumber type for fuzzy query"); |
| 81 | + } |
| 82 | + break; |
| 83 | + default: |
| 84 | + throw new IllegalArgumentException("Unsupported value type for fuzzy query"); |
| 85 | + } |
| 86 | + } else { |
| 87 | + throw new IllegalArgumentException("Value cannot be null for fuzzy query"); |
| 88 | + } |
| 89 | + |
| 90 | + // Extract optional parameters |
| 91 | + if (fuzzyQueryProto.hasXName() && !fuzzyQueryProto.getXName().isEmpty()) { |
| 92 | + queryName = fuzzyQueryProto.getXName(); |
| 93 | + } |
| 94 | + |
| 95 | + if (fuzzyQueryProto.hasBoost()) { |
| 96 | + boost = fuzzyQueryProto.getBoost(); |
| 97 | + } |
| 98 | + |
| 99 | + // Extract fuzziness |
| 100 | + if (fuzzyQueryProto.hasFuzziness()) { |
| 101 | + switch (fuzzyQueryProto.getFuzziness().getFuzzinessCase()) { |
| 102 | + case STRING_VALUE: |
| 103 | + fuzziness = fuzzyQueryProto.getFuzziness().getStringValue(); |
| 104 | + break; |
| 105 | + case INT32_VALUE: |
| 106 | + fuzziness = String.valueOf(fuzzyQueryProto.getFuzziness().getInt32Value()); |
| 107 | + break; |
| 108 | + default: |
| 109 | + // Use default fuzziness |
| 110 | + break; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + // Extract prefix length |
| 115 | + if (fuzzyQueryProto.getPrefixLength() != 0) { |
| 116 | + prefixLength = fuzzyQueryProto.getPrefixLength(); |
| 117 | + } |
| 118 | + |
| 119 | + // Extract max expansions |
| 120 | + if (fuzzyQueryProto.getMaxExpansions() != 0) { |
| 121 | + maxExpansions = fuzzyQueryProto.getMaxExpansions(); |
| 122 | + } |
| 123 | + |
| 124 | + // Extract transpositions |
| 125 | + transpositions = fuzzyQueryProto.getTranspositions(); |
| 126 | + |
| 127 | + // Extract rewrite method if specified |
| 128 | + if (fuzzyQueryProto.getRewrite() != FuzzyQuery.MultiTermQueryRewrite.MULTI_TERM_QUERY_REWRITE_UNSPECIFIED) { |
| 129 | + switch (fuzzyQueryProto.getRewrite()) { |
| 130 | + case MULTI_TERM_QUERY_REWRITE_CONSTANT_SCORE: |
| 131 | + rewrite = "constant_score"; |
| 132 | + break; |
| 133 | + case MULTI_TERM_QUERY_REWRITE_CONSTANT_SCORE_BOOLEAN: |
| 134 | + rewrite = "constant_score_boolean"; |
| 135 | + break; |
| 136 | + case MULTI_TERM_QUERY_REWRITE_SCORING_BOOLEAN: |
| 137 | + rewrite = "scoring_boolean"; |
| 138 | + break; |
| 139 | + case MULTI_TERM_QUERY_REWRITE_TOP_TERMS_N: |
| 140 | + rewrite = "top_terms_N"; |
| 141 | + break; |
| 142 | + case MULTI_TERM_QUERY_REWRITE_TOP_TERMS_BLENDED_FREQS_N: |
| 143 | + rewrite = "top_terms_blended_freqs_N"; |
| 144 | + break; |
| 145 | + case MULTI_TERM_QUERY_REWRITE_TOP_TERMS_BOOST_N: |
| 146 | + rewrite = "top_terms_boost_N"; |
| 147 | + break; |
| 148 | + default: |
| 149 | + // Use default rewrite method |
| 150 | + break; |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + // Create and configure FuzzyQueryBuilder |
| 155 | + FuzzyQueryBuilder fuzzyQuery = new FuzzyQueryBuilder(fieldName, value); |
| 156 | + fuzzyQuery.boost(boost); |
| 157 | + |
| 158 | + if (fuzziness != null) { |
| 159 | + fuzzyQuery.fuzziness(Fuzziness.build(fuzziness)); |
| 160 | + } |
| 161 | + |
| 162 | + fuzzyQuery.prefixLength(prefixLength); |
| 163 | + fuzzyQuery.maxExpansions(maxExpansions); |
| 164 | + fuzzyQuery.transpositions(transpositions); |
| 165 | + |
| 166 | + if (queryName != null) { |
| 167 | + fuzzyQuery.queryName(queryName); |
| 168 | + } |
| 169 | + |
| 170 | + if (rewrite != null) { |
| 171 | + fuzzyQuery.rewrite(rewrite); |
| 172 | + } |
| 173 | + |
| 174 | + return fuzzyQuery; |
| 175 | + } |
| 176 | +} |
0 commit comments