|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | +package org.opensearch.neuralsearch.mapper.dto; |
| 6 | + |
| 7 | +import lombok.Builder; |
| 8 | +import lombok.Getter; |
| 9 | +import lombok.NonNull; |
| 10 | +import org.apache.commons.lang.builder.EqualsBuilder; |
| 11 | +import org.apache.commons.lang.builder.HashCodeBuilder; |
| 12 | +import org.opensearch.core.xcontent.XContentBuilder; |
| 13 | +import org.opensearch.index.mapper.Mapper; |
| 14 | +import org.opensearch.index.mapper.MapperParsingException; |
| 15 | +import org.opensearch.neuralsearch.util.prune.PruneType; |
| 16 | +import org.opensearch.neuralsearch.util.prune.PruneUtils; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.Locale; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +import static org.opensearch.neuralsearch.constants.SemanticFieldConstants.SPARSE_ENCODING_CONFIG; |
| 24 | +import static org.opensearch.neuralsearch.util.prune.PruneUtils.PRUNE_RATIO_FIELD; |
| 25 | +import static org.opensearch.neuralsearch.util.prune.PruneUtils.PRUNE_TYPE_FIELD; |
| 26 | + |
| 27 | +@Builder |
| 28 | +@Getter |
| 29 | +public class SparseEncodingConfig { |
| 30 | + private PruneType pruneType; |
| 31 | + private Float pruneRatio; |
| 32 | + |
| 33 | + /** |
| 34 | + * Parse the json input when we define the sparse encoding config in the index mappings |
| 35 | + * @param name parameter name |
| 36 | + * @param ctx parse context |
| 37 | + * @param value parameter value |
| 38 | + * @return parsed SparseEncodingConfig |
| 39 | + */ |
| 40 | + public static SparseEncodingConfig parse(@NonNull final String name, final Mapper.TypeParser.ParserContext ctx, final Object value) { |
| 41 | + if (value instanceof Map == false) { |
| 42 | + throw new MapperParsingException(String.format(Locale.ROOT, "[%s] must be a Map", name)); |
| 43 | + } |
| 44 | + final Map<String, Object> config = new HashMap<>((Map<String, Object>) value); |
| 45 | + final PruneType pruneType = readPruneType(config, true); |
| 46 | + final Float pruneRatio = readPruneRatio(config, true); |
| 47 | + |
| 48 | + // Check for any unrecognized parameters |
| 49 | + if (config.isEmpty() == false) { |
| 50 | + throw new MapperParsingException( |
| 51 | + String.format(Locale.ROOT, "Unsupported parameters %s in %s", String.join(",", config.keySet()), name) |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + // Case: pruneType is null and pruneRatio is null → nothing configured |
| 56 | + if (pruneType == null && pruneRatio == null) { |
| 57 | + return null; |
| 58 | + } |
| 59 | + |
| 60 | + // Case: pruneRatio is set but pruneType is null or NONE → invalid |
| 61 | + if (pruneRatio != null && (pruneType == null | PruneType.NONE.equals(pruneType))) { |
| 62 | + throw new MapperParsingException( |
| 63 | + String.format( |
| 64 | + Locale.ROOT, |
| 65 | + "%s should not be defined when %s is %s or null", |
| 66 | + PRUNE_RATIO_FIELD, |
| 67 | + PRUNE_TYPE_FIELD, |
| 68 | + PruneType.NONE.getValue() |
| 69 | + ) |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + // Case: pruneType is defined and not NONE and pruneRatio is null → missing pruneRatio |
| 74 | + if (pruneRatio == null && PruneType.NONE.equals(pruneType) == false) { |
| 75 | + throw new MapperParsingException( |
| 76 | + String.format( |
| 77 | + Locale.ROOT, |
| 78 | + "%s is required when %s is defined and not %s", |
| 79 | + PRUNE_RATIO_FIELD, |
| 80 | + PRUNE_TYPE_FIELD, |
| 81 | + PruneType.NONE.getValue() |
| 82 | + ) |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + // Case: pruneType is NONE and pruneRatio is null |
| 87 | + if (pruneRatio == null) { |
| 88 | + return SparseEncodingConfig.builder().pruneType(pruneType).build(); |
| 89 | + } |
| 90 | + |
| 91 | + // Case: pruneType is not NONE or null and pruneRatio is not null |
| 92 | + if (PruneUtils.isValidPruneRatio(pruneType, pruneRatio) == false) { |
| 93 | + throw new MapperParsingException( |
| 94 | + String.format( |
| 95 | + Locale.ROOT, |
| 96 | + "Invalid %s and %s combo. Check %s for the valid combos.", |
| 97 | + PRUNE_RATIO_FIELD, |
| 98 | + PRUNE_TYPE_FIELD, |
| 99 | + "https://docs.opensearch.org/docs/latest/ingest-pipelines/processors/sparse-encoding/#pruning-sparse-vectors" |
| 100 | + ) |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + return SparseEncodingConfig.builder().pruneType(pruneType).pruneRatio(pruneRatio).build(); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Parse the config of the semantic field to build the SparseEncodingConfig if it is defined. Only should be used |
| 109 | + * to parse the valid semantic field config. |
| 110 | + * @param fieldConfig semantic field config |
| 111 | + * @return SparseEncodingConfig or null |
| 112 | + */ |
| 113 | + public static SparseEncodingConfig parse(@NonNull final Map<String, Object> fieldConfig) { |
| 114 | + if (fieldConfig.containsKey(SPARSE_ENCODING_CONFIG) == false) { |
| 115 | + return null; |
| 116 | + } |
| 117 | + final Map<String, Object> sparseEncodingConfig = (Map<String, Object>) fieldConfig.get(SPARSE_ENCODING_CONFIG); |
| 118 | + final PruneType pruneType = readPruneType(sparseEncodingConfig, false); |
| 119 | + final Float pruneRatio = readPruneRatio(sparseEncodingConfig, false); |
| 120 | + if (pruneType == null && pruneRatio == null) { |
| 121 | + return null; |
| 122 | + } |
| 123 | + return SparseEncodingConfig.builder().pruneType(pruneType).pruneRatio(pruneRatio).build(); |
| 124 | + } |
| 125 | + |
| 126 | + private static Float readPruneRatio(@NonNull final Map<String, Object> config, final boolean shouldRemoveIt) { |
| 127 | + if (config.containsKey(PRUNE_RATIO_FIELD)) { |
| 128 | + try { |
| 129 | + return Float.parseFloat(config.get(PRUNE_RATIO_FIELD).toString()); |
| 130 | + } catch (Exception e) { |
| 131 | + throw new MapperParsingException(String.format(Locale.ROOT, "[%s] must be a Float", PRUNE_RATIO_FIELD)); |
| 132 | + } finally { |
| 133 | + if (shouldRemoveIt) { |
| 134 | + config.remove(PRUNE_RATIO_FIELD); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + return null; |
| 139 | + } |
| 140 | + |
| 141 | + private static PruneType readPruneType(@NonNull final Map<String, Object> config, final boolean shouldRemoveIt) { |
| 142 | + if (config.containsKey(PRUNE_TYPE_FIELD)) { |
| 143 | + try { |
| 144 | + return PruneType.fromString((String) config.get(PRUNE_TYPE_FIELD)); |
| 145 | + } catch (Exception e) { |
| 146 | + throw new MapperParsingException( |
| 147 | + String.format(Locale.ROOT, "Invalid [%s]. Valid values are [%s].", PRUNE_TYPE_FIELD, PruneType.getValidValues()) |
| 148 | + ); |
| 149 | + } finally { |
| 150 | + if (shouldRemoveIt) { |
| 151 | + config.remove(PRUNE_TYPE_FIELD); |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + return null; |
| 156 | + } |
| 157 | + |
| 158 | + public void toXContent(@NonNull final XContentBuilder builder, String name, @NonNull final SparseEncodingConfig sparseEncodingConfig) |
| 159 | + throws IOException { |
| 160 | + builder.startObject(name); |
| 161 | + if (sparseEncodingConfig.pruneType != null) { |
| 162 | + builder.field(PRUNE_TYPE_FIELD, sparseEncodingConfig.pruneType.getValue()); |
| 163 | + } |
| 164 | + if (sparseEncodingConfig.pruneRatio != null) { |
| 165 | + builder.field(PRUNE_RATIO_FIELD, sparseEncodingConfig.pruneRatio.floatValue()); |
| 166 | + } |
| 167 | + builder.endObject(); |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public String toString() { |
| 172 | + final Map<String, Object> config = new HashMap<>(); |
| 173 | + if (pruneType != null) { |
| 174 | + config.put(PRUNE_TYPE_FIELD, pruneType.getValue()); |
| 175 | + } |
| 176 | + if (pruneRatio != null) { |
| 177 | + config.put(PRUNE_RATIO_FIELD, pruneRatio); |
| 178 | + } |
| 179 | + return config.toString(); |
| 180 | + } |
| 181 | + |
| 182 | + @Override |
| 183 | + public boolean equals(Object obj) { |
| 184 | + if (this == obj) { |
| 185 | + return true; |
| 186 | + } else if (obj != null && this.getClass() == obj.getClass()) { |
| 187 | + SparseEncodingConfig other = (SparseEncodingConfig) obj; |
| 188 | + EqualsBuilder equalsBuilder = new EqualsBuilder(); |
| 189 | + equalsBuilder.append(this.pruneType, other.pruneType); |
| 190 | + equalsBuilder.append(this.pruneRatio, other.pruneRatio); |
| 191 | + return equalsBuilder.isEquals(); |
| 192 | + } else { |
| 193 | + return false; |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + @Override |
| 198 | + public int hashCode() { |
| 199 | + return (new HashCodeBuilder()).append(this.pruneType).append(this.pruneRatio).toHashCode(); |
| 200 | + } |
| 201 | +} |
0 commit comments