Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

Minor refactoring of the TextToVectorQParserPlugin (#3651)

Ilaria Petretia9178f5db9d8
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index d935a3dec5c..0e8bf284cee 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -57,6 +57,8 @@ Improvements
* SOLR-17867: Export tool should properly output exported documents in json, json w/ lines, and javabin formats. (Eric Pugh)
+* Minor refactoring of the TextToVectorQParserPlugin (Ilaria Petreti via Alessandro Benedetti)
+
Optimizations
---------------------
* SOLR-17568: The CLI bin/solr export tool now contacts the appropriate nodes directly for data instead of proxying through one.
diff --git a/solr/core/src/java/org/apache/solr/search/neural/AbstractVectorQParserBase.java b/solr/core/src/java/org/apache/solr/search/neural/AbstractVectorQParserBase.java
index 4cafb45744e..d7ab9c72938 100644
--- a/solr/core/src/java/org/apache/solr/search/neural/AbstractVectorQParserBase.java
+++ b/solr/core/src/java/org/apache/solr/search/neural/AbstractVectorQParserBase.java
@@ -40,7 +40,7 @@ public abstract class AbstractVectorQParserBase extends QParser {
static final String INCLUDE_TAGS = "includeTags";
private final String denseVectorFieldName;
- private final String vectorToSearch;
+ protected String vectorToSearch;
public AbstractVectorQParserBase(
String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {
diff --git a/solr/modules/llm/src/java/org/apache/solr/llm/textvectorisation/search/TextToVectorQParserPlugin.java b/solr/modules/llm/src/java/org/apache/solr/llm/textvectorisation/search/TextToVectorQParserPlugin.java
index 449b4b6dd76..3780da9972c 100644
--- a/solr/modules/llm/src/java/org/apache/solr/llm/textvectorisation/search/TextToVectorQParserPlugin.java
+++ b/solr/modules/llm/src/java/org/apache/solr/llm/textvectorisation/search/TextToVectorQParserPlugin.java
@@ -17,8 +17,8 @@
package org.apache.solr.llm.textvectorisation.search;
import java.io.IOException;
+import java.util.Arrays;
import org.apache.lucene.index.VectorEncoding;
-import org.apache.lucene.search.KnnFloatVectorQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.util.ResourceLoader;
import org.apache.lucene.util.ResourceLoaderAware;
@@ -31,8 +31,6 @@ import org.apache.solr.llm.textvectorisation.store.rest.ManagedTextToVectorModel
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.rest.ManagedResource;
import org.apache.solr.rest.ManagedResourceObserver;
-import org.apache.solr.schema.DenseVectorField;
-import org.apache.solr.schema.SchemaField;
import org.apache.solr.search.QParser;
import org.apache.solr.search.QParserPlugin;
import org.apache.solr.search.SyntaxError;
@@ -78,6 +76,18 @@ public class TextToVectorQParserPlugin extends QParserPlugin
super(queryString, localParams, params, req);
}
+ private void checkVectorEncoding() {
+ final VectorEncoding vectorEncoding =
+ getCheckedFieldType(req.getCore().getLatestSchema().getField(getFieldName()))
+ .getVectorEncoding();
+
+ if (vectorEncoding != VectorEncoding.FLOAT32) {
+ throw new SolrException(
+ SolrException.ErrorCode.SERVER_ERROR,
+ "Vector Encoding not supported : " + vectorEncoding);
+ }
+ }
+
@Override
public Query parse() throws SyntaxError {
checkParam(qstr, "Query string is empty, nothing to vectorise");
@@ -86,25 +96,9 @@ public class TextToVectorQParserPlugin extends QParserPlugin
SolrTextToVectorModel textToVector = modelStore.getModel(embeddingModelName);
if (textToVector != null) {
- final SchemaField schemaField = req.getCore().getLatestSchema().getField(getFieldName());
- final DenseVectorField denseVectorType = getCheckedFieldType(schemaField);
- int fieldDimensions = denseVectorType.getDimension();
- VectorEncoding vectorEncoding = denseVectorType.getVectorEncoding();
- final int topK = localParams.getInt(TOP_K, DEFAULT_TOP_K);
-
- switch (vectorEncoding) {
- case FLOAT32:
- {
- float[] vectorToSearch = textToVector.vectorise(qstr);
- checkVectorDimension(vectorToSearch.length, fieldDimensions);
- return new KnnFloatVectorQuery(
- schemaField.getName(), vectorToSearch, topK, getFilterQuery());
- }
- default:
- throw new SolrException(
- SolrException.ErrorCode.SERVER_ERROR,
- "Vector Encoding not supported : " + vectorEncoding);
- }
+ checkVectorEncoding();
+ super.vectorToSearch = Arrays.toString(textToVector.vectorise(qstr));
+ return super.parse();
} else {
throw new SolrException(
SolrException.ErrorCode.BAD_REQUEST,
@@ -116,18 +110,6 @@ public class TextToVectorQParserPlugin extends QParserPlugin
}
}
- private void checkVectorDimension(int inputVectorDimension, int fieldVectorDimension) {
- if (inputVectorDimension != fieldVectorDimension) {
- throw new SolrException(
- SolrException.ErrorCode.BAD_REQUEST,
- "incorrect vector dimension."
- + " The vector value has size "
- + inputVectorDimension
- + " while it is expected a vector with size "
- + fieldVectorDimension);
- }
- }
-
private void checkParam(String value, String message) {
if (value == null || value.isBlank()) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, message);