aruggero commented on code in PR #4259: URL: https://github.com/apache/solr/pull/4259#discussion_r3137649258
########## solr/modules/language-models/src/java/org/apache/solr/languagemodels/documentenrichment/model/SolrFieldGenerationModel.java: ########## @@ -0,0 +1,256 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.solr.languagemodels.documentenrichment.model; + +import dev.langchain4j.data.message.UserMessage; +import dev.langchain4j.model.chat.Capability; +import dev.langchain4j.model.chat.ChatModel; +import dev.langchain4j.model.chat.request.ChatRequest; +import dev.langchain4j.model.chat.request.ResponseFormat; +import dev.langchain4j.model.chat.request.json.JsonArraySchema; +import dev.langchain4j.model.chat.request.json.JsonBooleanSchema; +import dev.langchain4j.model.chat.request.json.JsonIntegerSchema; +import dev.langchain4j.model.chat.request.json.JsonNumberSchema; +import dev.langchain4j.model.chat.request.json.JsonObjectSchema; +import dev.langchain4j.model.chat.request.json.JsonSchemaElement; +import dev.langchain4j.model.chat.request.json.JsonStringSchema; +import java.lang.invoke.MethodHandles; +import java.lang.reflect.Method; +import java.time.Duration; +import java.util.ArrayList; +import java.util.EnumSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import org.apache.lucene.util.Accountable; +import org.apache.lucene.util.RamUsageEstimator; +import org.apache.solr.common.SolrException; +import org.apache.solr.common.util.Utils; +import org.apache.solr.core.SolrResourceLoader; +import org.apache.solr.languagemodels.documentenrichment.update.processor.DocumentEnrichmentUpdateProcessorFactory; +import org.apache.solr.languagemodels.documentenrichment.store.FieldGenerationModelException; +import org.apache.solr.languagemodels.documentenrichment.store.rest.ManagedFieldGenerationModelStore; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This object wraps a {@link dev.langchain4j.model.chat.ChatModel} to produce the content of a + * field based on the content of other fields specified as input. It's meant to be used as a managed + * resource with the {@link ManagedFieldGenerationModelStore} + */ +public class SolrFieldGenerationModel implements Accountable { + private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + private static final long BASE_RAM_BYTES = + RamUsageEstimator.shallowSizeOfInstance(SolrFieldGenerationModel.class); + private static final String TIMEOUT_PARAM = "timeout"; + private static final String MAX_RETRIES_PARAM = "maxRetries"; + private static final String THINKING_BUDGET_TOKENS = "thinkingBudgetTokens"; + private static final String RANDOM_SEED = "randomSeed"; + + private final String name; + private final Map<String, Object> params; + private final ChatModel chatModel; + private final int hashCode; + + public static SolrFieldGenerationModel getInstance( + SolrResourceLoader solrResourceLoader, + String className, + String name, + Map<String, Object> params) + throws FieldGenerationModelException { + try { + /* + * The idea here is to build a {@link dev.langchain4j.model.chat.ChatModel} using inversion + * of control. + * Each model has its own list of parameters we don't know beforehand, but each {@link dev.langchain4j.model.chat.ChatModel} class + * has its own builder that uses setters with the same name of the parameter in input. + * */ + ChatModel chatModel; + Class<?> modelClass = solrResourceLoader.findClass(className, ChatModel.class); + var builder = modelClass.getMethod("builder").invoke(null); + if (params != null) { + /* + * This block of code has the responsibility of instantiate a {@link + * dev.langchain4j.model.chat.ChatModel} using the params provided. Classes have + * params of the specific implementation of {@link + * dev.langchain4j.model.chat.ChatModel}, which is not known beforehand. So we benefit of + * the design choice in langchain4j that each subclass implementing {@link + * dev.langchain4j.model.chat.ChatModel} uses setters with the same name of the + * param. + */ + for (String paramName : params.keySet()) { + /* + * When a param is not primitive, we need to instantiate the object explicitly and then call the + * setter method. + * N.B. when adding support to new models, pay attention to all the parameters they + * support, some of them may require to be handled in here as separate switch cases + */ + switch (paramName) { + case TIMEOUT_PARAM -> builder + .getClass() + .getMethod(paramName, Duration.class) + .invoke(builder, Duration.ofSeconds((Long) params.get(paramName))); + + case MAX_RETRIES_PARAM, THINKING_BUDGET_TOKENS, RANDOM_SEED -> builder + .getClass() + .getMethod(paramName, Integer.class) + .invoke(builder, ((Long) params.get(paramName)).intValue()); + + /* + * For primitive params if there's only one setter available, we call it. + * If there's choice we default to the string one + */ + default -> { + ArrayList<Method> paramNameMatches = new ArrayList<>(); + for (var method : builder.getClass().getMethods()) { + if (paramName.equals(method.getName()) && method.getParameterCount() == 1) { + paramNameMatches.add(method); + } + } + if (paramNameMatches.size() == 1) { + paramNameMatches.getFirst().invoke(builder, params.get(paramName)); + } else { + try { + builder + .getClass() + .getMethod(paramName, String.class) + .invoke(builder, params.get(paramName).toString()); + } catch (NoSuchMethodException e) { + log.error("Parameter {} not supported by model {}", paramName, className); + throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e.getMessage(), e); + } + } + } + } + } + } + + // Always enforce strict schema adherence where supported. For Anthropic and Google it's enabled by default + if (!"dev.langchain4j.model.anthropic.AnthropicChatModel".equals(className) && + !"dev.langchain4j.model.googleai.GoogleAiGeminiChatModel".equals(className)) { + try { + builder + .getClass() + .getMethod("strictJsonSchema", Boolean.class) + .invoke(builder, true); + } catch (NoSuchMethodException ignored) { + log.debug("Model {} does not have strictJsonSchema param, structured output is not enforced", className); + } + } + chatModel = (ChatModel) builder.getClass().getMethod("build").invoke(builder); + return new SolrFieldGenerationModel(name, chatModel, params); + } catch (final Exception e) { + throw new FieldGenerationModelException("Model loading failed for " + className, e); + } + } + + public SolrFieldGenerationModel(String name, ChatModel chatModel, Map<String, Object> params) { + this.name = name; + this.chatModel = chatModel; + this.params = params; + this.hashCode = calculateHashCode(); + } + + /** + * Sends a structured chat request and returns the parsed value from the {@code {"value": ...}} + * JSON object that the model is instructed to produce via {@code responseFormat}. + * + * @return the extracted value: a {@link String}, {@link Number}, {@link Integer}, {@link + * Boolean}, or {@link java.util.List} depending on the Solr output field type + */ + public Object generateFieldValue(String prompt, ResponseFormat responseFormat) { + ChatRequest chatRequest = + ChatRequest.builder() + .responseFormat(responseFormat) + .messages(UserMessage.from(prompt)) + .build(); + String rawJson = chatModel.chat(chatRequest).aiMessage().text(); + Object parsed = Utils.fromJSONString(rawJson); + // It makes sense to keep this due to Ollama support Review Comment: I would change the comment "Ollama support" is not clear to me ########## solr/modules/language-models/src/test-files/solr/collection1/conf/schema-language-models.xml: ########## @@ -36,11 +40,50 @@ <field name="2048_byte_vector" type="high_dimensional_byte_knn_vector" indexed="true" stored="true" /> <field name="2048_float_vector" type="high_dimensional_float_knn_vector" indexed="true" stored="true" /> <field name="string_field" type="string" indexed="true" stored="true" multiValued="false" required="false"/> + <field name="body_field" type="string" indexed="true" stored="true" multiValued="false" required="false"/> + <field name="tags_field" type="string" indexed="true" stored="true" multiValued="true" required="false"/> + <field name="enriched_field" type="string" indexed="true" stored="true" multiValued="false" required="false"/> + <field name="enriched_field_multi" type="string" indexed="true" stored="true" multiValued="true" required="false"/> + + <!-- single-valued output fields for each supported type --> + <field name="output_long" type="plong" indexed="true" stored="true" multiValued="false"/> + <field name="output_int" type="pint" indexed="true" stored="true" multiValued="false"/> + <field name="output_float" type="pfloat" indexed="true" stored="true" multiValued="false"/> + <field name="output_double" type="pdouble" indexed="true" stored="true" multiValued="false"/> + <field name="output_boolean" type="boolean" indexed="true" stored="true" multiValued="false"/> + <field name="output_date" type="pdate" indexed="true" stored="true" multiValued="false"/> + + <!-- multivalued output fields for each supported type --> + <field name="output_long_multi" type="plong" indexed="true" stored="true" multiValued="true"/> + <field name="output_int_multi" type="pint" indexed="true" stored="true" multiValued="true"/> + <field name="output_float_multi" type="pfloat" indexed="true" stored="true" multiValued="true"/> + <field name="output_double_multi" type="pdouble" indexed="true" stored="true" multiValued="true"/> + <field name="output_boolean_multi" type="boolean" indexed="true" stored="true" multiValued="true"/> + <field name="output_date_multi" type="pdate" indexed="true" stored="true" multiValued="true"/> + <field name="output_binary" type="binary" indexed="false" stored="true" multiValued="false"/> + + <!-- output fields for unsupported types --> + <field name="output_bynary" type="binary" indexed="true" stored="true" multiValued="false"/> + + <!-- output fields for types explicitly unsupported (without they are supported via inheritance) --> Review Comment: What does this comment mean? ########## solr/modules/language-models/src/test/org/apache/solr/languagemodels/documentenrichment/model/DummyChatModel.java: ########## @@ -0,0 +1,85 @@ +/* Review Comment: Didn't we remove any "chatModel" reference from the contribution? ########## solr/modules/language-models/src/test/org/apache/solr/languagemodels/documentenrichment/update/processor/DocumentEnrichmentUpdateProcessorFactoryTest.java: ########## @@ -354,64 +325,60 @@ public void init_multivaluedStringOutputField_shouldNotThrowException() throws E createUpdateProcessor( List.of("string_field"), "enriched_field_multi", null, collection1, "model1"); assertNotNull(instance); - restTestHarness.delete(ManagedChatModelStore.REST_END_POINT + "/model1"); + restTestHarness.delete(ManagedFieldGenerationModelStore.REST_END_POINT + "/model1"); } - /* buildResponseFormat tests for field types from the Solr documentation */ + /* buildResponseFormat tests for unsupported field types from the Solr documentation: 1 general (Binary) + 3 via inheritance */ Review Comment: What does this comment mean? ########## solr/modules/language-models/src/test/org/apache/solr/languagemodels/documentenrichment/model/DummyChatModelTest.java: ########## @@ -0,0 +1,48 @@ +/* Review Comment: Didn't we remove any "chatModel" reference from the contribution? ########## solr/modules/language-models/src/test/org/apache/solr/languagemodels/documentenrichment/model/ExceptionThrowingChatModel.java: ########## @@ -0,0 +1,48 @@ +/* Review Comment: Didn't we remove any "chatModel" reference from the contribution? ########## solr/modules/language-models/src/test-files/solr/collection1/conf/schema-language-models.xml: ########## @@ -36,11 +40,50 @@ <field name="2048_byte_vector" type="high_dimensional_byte_knn_vector" indexed="true" stored="true" /> <field name="2048_float_vector" type="high_dimensional_float_knn_vector" indexed="true" stored="true" /> <field name="string_field" type="string" indexed="true" stored="true" multiValued="false" required="false"/> + <field name="body_field" type="string" indexed="true" stored="true" multiValued="false" required="false"/> + <field name="tags_field" type="string" indexed="true" stored="true" multiValued="true" required="false"/> + <field name="enriched_field" type="string" indexed="true" stored="true" multiValued="false" required="false"/> + <field name="enriched_field_multi" type="string" indexed="true" stored="true" multiValued="true" required="false"/> + + <!-- single-valued output fields for each supported type --> + <field name="output_long" type="plong" indexed="true" stored="true" multiValued="false"/> + <field name="output_int" type="pint" indexed="true" stored="true" multiValued="false"/> + <field name="output_float" type="pfloat" indexed="true" stored="true" multiValued="false"/> + <field name="output_double" type="pdouble" indexed="true" stored="true" multiValued="false"/> + <field name="output_boolean" type="boolean" indexed="true" stored="true" multiValued="false"/> + <field name="output_date" type="pdate" indexed="true" stored="true" multiValued="false"/> + + <!-- multivalued output fields for each supported type --> + <field name="output_long_multi" type="plong" indexed="true" stored="true" multiValued="true"/> + <field name="output_int_multi" type="pint" indexed="true" stored="true" multiValued="true"/> + <field name="output_float_multi" type="pfloat" indexed="true" stored="true" multiValued="true"/> + <field name="output_double_multi" type="pdouble" indexed="true" stored="true" multiValued="true"/> + <field name="output_boolean_multi" type="boolean" indexed="true" stored="true" multiValued="true"/> + <field name="output_date_multi" type="pdate" indexed="true" stored="true" multiValued="true"/> + <field name="output_binary" type="binary" indexed="false" stored="true" multiValued="false"/> + + <!-- output fields for unsupported types --> + <field name="output_bynary" type="binary" indexed="true" stored="true" multiValued="false"/> Review Comment: Why do we have both binary and bynary? ########## solr/modules/language-models/src/java/org/apache/solr/languagemodels/documentenrichment/update/processor/DocumentEnrichmentUpdateProcessorFactory.java: ########## @@ -0,0 +1,324 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.solr.languagemodels.documentenrichment.update.processor; + +import dev.langchain4j.model.chat.request.ResponseFormat; +import dev.langchain4j.model.chat.request.ResponseFormatType; +import dev.langchain4j.model.chat.request.json.JsonArraySchema; +import dev.langchain4j.model.chat.request.json.JsonBooleanSchema; +import dev.langchain4j.model.chat.request.json.JsonIntegerSchema; +import dev.langchain4j.model.chat.request.json.JsonNumberSchema; +import dev.langchain4j.model.chat.request.json.JsonObjectSchema; +import dev.langchain4j.model.chat.request.json.JsonSchema; +import dev.langchain4j.model.chat.request.json.JsonSchemaElement; +import dev.langchain4j.model.chat.request.json.JsonStringSchema; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.apache.solr.common.SolrException; +import org.apache.solr.common.params.RequiredSolrParams; +import org.apache.solr.common.params.SolrParams; +import org.apache.solr.common.util.NamedList; +import org.apache.solr.core.SolrCore; +import org.apache.solr.core.SolrResourceLoader; +import org.apache.solr.languagemodels.documentenrichment.model.SolrFieldGenerationModel; +import org.apache.solr.languagemodels.documentenrichment.store.rest.ManagedFieldGenerationModelStore; +import org.apache.solr.request.SolrQueryRequest; +import org.apache.solr.response.SolrQueryResponse; +import org.apache.solr.rest.ManagedResource; +import org.apache.solr.rest.ManagedResourceObserver; +import org.apache.solr.schema.BoolField; +import org.apache.solr.schema.DatePointField; +import org.apache.solr.schema.DenseVectorField; +import org.apache.solr.schema.DoublePointField; +import org.apache.solr.schema.FieldType; +import org.apache.solr.schema.FloatPointField; +import org.apache.solr.schema.IndexSchema; +import org.apache.solr.schema.IntPointField; +import org.apache.solr.schema.LongPointField; +import org.apache.solr.schema.NestPathField; +import org.apache.solr.schema.SchemaField; +import org.apache.solr.schema.StrField; +import org.apache.solr.schema.TextField; +import org.apache.solr.schema.UUIDField; +import org.apache.solr.update.processor.UpdateRequestProcessor; +import org.apache.solr.update.processor.UpdateRequestProcessorFactory; +import org.apache.solr.util.plugin.SolrCoreAware; + +/** + * Generate the content of {@code outputField} based on other fields specified as {@code inputField}s. + * + * <p>The following validation rules are applied: + * + * <ul> + * <li>At least one {@code inputField} must be declared. + * <li>Exactly one of {@code prompt} or {@code promptFile} must be provided. + * <li>Every declared {@code inputField} must have a corresponding {@code {fieldName}} placeholder + * in the prompt. + * <li>Every {@code {placeholder}} in the prompt must correspond to a declared {@code inputField}. + * <li>One and only one {@code outputField} is allowed. + * </ul> + * + * <pre class="prettyprint" > + * <processor class="solr.llm.documentenrichment.update.processor.DocumentEnrichmentUpdateProcessorFactory"> + * <str name="inputField">title_field</str> + * <str name="inputField">body_field</str> + * <str name="outputField">enriched_field</str> + * <str name="prompt">Title: {title_field}. Body: {body_field}.</str> // or <str name="promptFile">prompt.txt</str> + * <str name="model">model-name</str> + * </processor> + * </pre> + * + * <p>Multiple {@code inputField} values can also be declared as an array using {@code arr}: + * + * <pre class="prettyprint" > + * <arr name="inputField"> + * <str>title_field</str> + * <str>body_field</str> + * </arr> + * </pre> + */ +public class DocumentEnrichmentUpdateProcessorFactory extends UpdateRequestProcessorFactory + implements SolrCoreAware, ManagedResourceObserver { + private static final String INPUT_FIELD_PARAM = "inputField"; + private static final String OUTPUT_FIELD_PARAM = "outputField"; + private static final String PROMPT = "prompt"; + private static final String PROMPT_FILE = "promptFile"; + private static final String MODEL_NAME = "model"; + private static final String JSON_SCHEMA_NAME = "output"; + public static final String JSON_FIELD_PROPERTY = "value"; + private static final Pattern PLACEHOLDER_PATTERN = Pattern.compile("\\{([^}]+)\\}"); + + private List<String> inputFields; + private String outputField; + private String promptText; + private String promptFile; + private String modelName; + + @Override + public void init(final NamedList<?> args) { + // removeConfigArgs handles both multiple <str name="inputField"> and <arr name="inputField"> + // and must be called before toSolrParams() since it mutates args in place + Collection<String> fieldNames = args.removeConfigArgs(INPUT_FIELD_PARAM); + if (fieldNames.isEmpty()) { + throw new SolrException( + SolrException.ErrorCode.SERVER_ERROR, "At least one 'inputField' must be provided"); + } + inputFields = List.copyOf(fieldNames); + + Collection<String> outputFields = args.removeConfigArgs(OUTPUT_FIELD_PARAM); + if (outputFields.isEmpty()) { + throw new SolrException( + SolrException.ErrorCode.SERVER_ERROR, "Exactly one 'outputField' must be provided"); + } + if (outputFields.size() > 1) { + throw new SolrException( + SolrException.ErrorCode.SERVER_ERROR, + "Only one 'outputField' can be provided, but found: " + outputFields); + } + outputField = outputFields.iterator().next(); + + SolrParams params = args.toSolrParams(); + RequiredSolrParams required = params.required(); + modelName = required.get(MODEL_NAME); + + String inlinePrompt = params.get(PROMPT); + String promptFilePath = params.get(PROMPT_FILE); + + if (inlinePrompt == null && promptFilePath == null) { + throw new SolrException( + SolrException.ErrorCode.SERVER_ERROR, "Either 'prompt' or 'promptFile' must be provided"); + } + if (inlinePrompt != null && promptFilePath != null) { + throw new SolrException( + SolrException.ErrorCode.SERVER_ERROR, + "Only one of 'prompt' or 'promptFile' can be provided, not both"); + } + if (inlinePrompt != null) { + validatePromptPlaceholders(inlinePrompt, inputFields); + this.promptText = inlinePrompt; + } + this.promptFile = promptFilePath; + } + + @Override + public void inform(SolrCore core) { + final SolrResourceLoader solrResourceLoader = core.getResourceLoader(); + ManagedFieldGenerationModelStore.registerManagedFieldGenerationModelStore(solrResourceLoader, this); + if (promptFile != null) { + try (InputStream is = solrResourceLoader.openResource(promptFile)) { + promptText = new String(is.readAllBytes(), StandardCharsets.UTF_8).trim(); + } catch (IOException e) { + throw new SolrException( + SolrException.ErrorCode.SERVER_ERROR, "Cannot read prompt file: " + promptFile, e); + } + validatePromptPlaceholders(promptText, inputFields); + } + } + + @Override + public void onManagedResourceInitialized(NamedList<?> args, ManagedResource res) + throws SolrException { + if (res instanceof ManagedFieldGenerationModelStore store) { + store.loadStoredModels(); + } + } + + @Override + public UpdateRequestProcessor getInstance( + SolrQueryRequest req, SolrQueryResponse rsp, UpdateRequestProcessor next) { + IndexSchema latestSchema = req.getCore().getLatestSchema(); + + for (String fieldName : inputFields) { + if (!latestSchema.isDynamicField(fieldName) && !latestSchema.hasExplicitField(fieldName)) { + throw new SolrException( + SolrException.ErrorCode.SERVER_ERROR, "undefined field: \"" + fieldName + "\""); + } + } + + final SchemaField outputFieldSchema = latestSchema.getField(outputField); + + ResponseFormat responseFormat = getJsonSchema(outputFieldSchema); + boolean multiValued = outputFieldSchema.multiValued(); + + ManagedFieldGenerationModelStore store = ManagedFieldGenerationModelStore.getManagedModelStore(req.getCore()); + SolrFieldGenerationModel fieldGenerationModel = store.getModel(modelName); + if (fieldGenerationModel == null) { + throw new SolrException( + SolrException.ErrorCode.SERVER_ERROR, + "The model configured in the Update Request Processor '" + + modelName + + "' can't be found in the store: " + + ManagedFieldGenerationModelStore.REST_END_POINT); + } + + return new DocumentEnrichmentUpdateProcessor( + inputFields, outputField, promptText, fieldGenerationModel, multiValued, responseFormat, req, next); + } + + /** + * Builds a {@link ResponseFormat} that instructs the model to return a JSON object {@code + * {"value": ...}} whose value type matches the Solr field type. For multivalued fields the value + * is wrapped in a {@link JsonArraySchema} nested inside the root {@link JsonObjectSchema}. + * + * <p>Nesting {@link JsonArraySchema} inside a {@link JsonObjectSchema} property is supported by + * all langchain4j providers that implement structured outputs with {@link JsonObjectSchema} + * (OpenAI, Azure OpenAI, Google AI, Gemini, Mistral, Ollama, Amazon Bedrock, Watsonx). + */ + static ResponseFormat getJsonSchema(SchemaField schemaField) { + JsonSchemaElement valueElement = toJsonSchemaElement(schemaField.getType()); + JsonSchemaElement valueSchema = + schemaField.multiValued() + ? JsonArraySchema.builder().items(valueElement).build() + : valueElement; + //estrai costanti output e value + return ResponseFormat.builder() + .type(ResponseFormatType.JSON) + .jsonSchema( + JsonSchema.builder() + .name(JSON_SCHEMA_NAME) + .rootElement( + JsonObjectSchema.builder() + .addProperty(JSON_FIELD_PROPERTY, valueSchema) + .required(JSON_FIELD_PROPERTY) + .build()) + .build()) + .build(); + } + + private static JsonSchemaElement toJsonSchemaElement(FieldType fieldType) { + SolrException unsupportedFieldTypeException = new SolrException( + SolrException.ErrorCode.SERVER_ERROR, + "field type is not supported by Document Enrichment: " + + fieldType.getClass().getSimpleName()); + + return switch (fieldType) { + // first check unsupported types and throw SolrException + case DenseVectorField f -> throw unsupportedFieldTypeException; + case UUIDField f -> throw unsupportedFieldTypeException; + case NestPathField f -> throw unsupportedFieldTypeException; + + // build JsonSchemaElement for supported types + case StrField f -> new JsonStringSchema(); + case TextField f -> new JsonStringSchema(); + case DatePointField f -> new JsonStringSchema(); + + case IntPointField f -> new JsonIntegerSchema(); + case LongPointField f -> new JsonIntegerSchema(); + + case FloatPointField f -> new JsonNumberSchema(); + case DoublePointField f -> new JsonNumberSchema(); + + case BoolField f -> new JsonBooleanSchema(); + + // fall-back to SolrException + default -> throw unsupportedFieldTypeException; + }; Review Comment: Can't we use ``` return switch (fieldType) { case StrField _, TextField _, DatePointField _ -> new JsonStringSchema(); case IntPointField _, LongPointField _ -> new JsonIntegerSchema(); case FloatPointField _, DoublePointField _ -> new JsonNumberSchema(); case BoolField _ -> new JsonBooleanSchema(); default -> throw unsupportedFieldTypeException; }; ``` with Java 22+? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
