Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions solr/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ New Features

* SOLR-17814: Add support for PatienceKnnVectorQuery. (Ilaria Petreti via Alessandro Benedetti)

* SOLR-17948: Support indexing primitive float[] values for DenseVectorField via JavaBin (Puneet Ahuja)

Improvements
---------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ protected void parseVector() {
}

protected void parseIndexVector() {
if (inputValue instanceof float[] fa) {
checkVectorDimension(fa.length);
for (int i = 0; i < dimension; i++) {
addNumberElement(fa[i]);
}
return;
}
if (inputValue instanceof double[] da) {
checkVectorDimension(da.length);
for (int i = 0; i < dimension; i++) {
addNumberElement((float) da[i]);
}
return;
}
if (!(inputValue instanceof List<?> inputVector)) {
throw new SolrException(
SolrException.ErrorCode.BAD_REQUEST, "incorrect vector format. " + errorMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,26 @@

import static org.hamcrest.core.Is.is;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.apache.lucene.index.VectorEncoding;
import org.apache.lucene.index.VectorSimilarityFunction;
import org.apache.solr.client.solrj.request.JavaBinUpdateRequestCodec;
import org.apache.solr.client.solrj.request.UpdateRequest;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.util.ContentStreamBase;
import org.apache.solr.core.AbstractBadConfigTestBase;
import org.apache.solr.handler.loader.JavabinLoader;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.response.SolrQueryResponse;
import org.apache.solr.update.CommitUpdateCommand;
import org.apache.solr.update.processor.UpdateRequestProcessor;
import org.apache.solr.update.processor.UpdateRequestProcessorChain;
import org.apache.solr.util.vector.DenseVectorParser;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -760,4 +771,67 @@ public void denseVectorByteEncoding_shouldRaiseExceptionWithFloatValues() throws
deleteCore();
}
}

private void addDocWithJavaBin(SolrInputDocument doc) throws Exception {
UpdateRequest ur = new UpdateRequest();
ur.add(doc);

ByteArrayOutputStream bos = new ByteArrayOutputStream();
JavaBinUpdateRequestCodec codec = new JavaBinUpdateRequestCodec();
codec.marshal(ur, bos);

byte[] payload = bos.toByteArray();

ContentStreamBase.ByteArrayStream cs =
new ContentStreamBase.ByteArrayStream(payload, "application/javabin");

try (SolrQueryRequest sreq = req()) {
SolrQueryResponse srsp = new SolrQueryResponse();
UpdateRequestProcessorChain chain =
h.getCore().getUpdateProcessorChain(new ModifiableSolrParams());
try (UpdateRequestProcessor proc = chain.createProcessor(sreq, srsp)) {
new JavabinLoader().load(sreq, srsp, cs, proc);
proc.finish();
}
h.getCore().getUpdateHandler().commit(new CommitUpdateCommand(sreq, false));
}
}

@Test
public void indexing_floatPrimitiveArray_viaJavaBin_shouldIndexAndReturnStored()
Copy link
Contributor

@chatman chatman Oct 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests in Solr usually don't have the "_shouldIndexAndReturnStored" part in the method name. Also, camel casing is preferable for test names. I think for sake of consistency, we should name this test testIndexingFloatPrimitiveArrayViaJavaBin, or simply "testIndexingViaJavaBin" and make sure both float[] and List works (maybe through a randomization). Also, can we fold both the double[] and float[] tests into a single test?

throws Exception {
try {
initCore("solrconfig-basic.xml", "schema-densevector.xml");

SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", "pf_jb");
doc.addField("vector", new float[] {1.1f, 2.2f, 3.3f, 4.4f});

addDocWithJavaBin(doc);

assertJQ(
req("q", "id:pf_jb", "fl", "vector"), "/response/docs/[0]/vector==[1.1,2.2,3.3,4.4]");
} finally {
deleteCore();
}
}

@Test
public void indexing_doublePrimitiveArray_viaJavaBin_shouldIndexAndReturnStored()
throws Exception {
try {
initCore("solrconfig-basic.xml", "schema-densevector.xml");

SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", "pd_jb");
doc.addField("vector", new double[] {1.1d, 2.2d, 3.3d, 4.4d});

addDocWithJavaBin(doc);

assertJQ(
req("q", "id:pd_jb", "fl", "vector"), "/response/docs/[0]/vector==[1.1,2.2,3.3,4.4]");
} finally {
deleteCore();
}
}
}
Loading