2020import org .elasticsearch .xcontent .XContentBuilder ;
2121import org .elasticsearch .xcontent .XContentFactory ;
2222import org .elasticsearch .xpack .esql .action .AbstractEsqlIntegTestCase ;
23+ import org .hamcrest .Matchers ;
2324import org .junit .Before ;
2425
2526import java .io .IOException ;
4041import static org .elasticsearch .index .mapper .SourceFieldMapper .Mode .SYNTHETIC ;
4142import static org .elasticsearch .test .hamcrest .ElasticsearchAssertions .assertAcked ;
4243import static org .elasticsearch .xpack .esql .action .EsqlCapabilities .Cap .L2_NORM_VECTOR_SIMILARITY_FUNCTION ;
44+ import static org .hamcrest .Matchers .hasKey ;
4345
4446public class DenseVectorFieldTypeIT extends AbstractEsqlIntegTestCase {
4547
@@ -68,7 +70,6 @@ private enum VectorSourceOptions {
6870 @ ParametersFactory
6971 public static Iterable <Object []> parameters () throws Exception {
7072 List <Object []> params = new ArrayList <>();
71-
7273 for (ElementType elementType : List .of (ElementType .BYTE , ElementType .FLOAT , ElementType .BIT )) {
7374 // Test all similarities
7475 for (DenseVectorFieldMapper .VectorSimilarity similarity : DenseVectorFieldMapper .VectorSimilarity .values ()) {
@@ -156,7 +157,6 @@ public void testRetrieveDenseVectorFieldData() {
156157 try (var resp = run (query )) {
157158 List <List <Object >> valuesList = EsqlTestUtils .getValuesList (resp );
158159 assertEquals (valuesList .size (), indexedVectors .size ());
159- // print all values for debugging
160160 valuesList .forEach (value -> {
161161 assertEquals (2 , value .size ());
162162 Integer id = (Integer ) value .get (0 );
@@ -180,6 +180,33 @@ public void testRetrieveDenseVectorFieldData() {
180180 }
181181 }
182182
183+ @ SuppressWarnings ("unchecked" )
184+ public void testDenseVectorsIncludedInSource () {
185+ var query = """
186+ FROM test METADATA _source
187+ | KEEP _source
188+ """ ;
189+
190+ try (var resp = run (query )) {
191+ List <List <Object >> valuesList = EsqlTestUtils .getValuesList (resp );
192+ assertEquals (valuesList .size (), indexedVectors .size ());
193+ valuesList .forEach (value -> {
194+ assertEquals (1 , value .size ());
195+ Map <String , Object > source = (Map <String , Object >) value .get (0 );
196+ assertThat (source , hasKey ("id" ));
197+ assertNotNull (source .get ("id" ));
198+ Integer id = Integer .valueOf (source .get ("id" ).toString ());
199+ // Vectors should be in _source if they are included in the index settings, and the vector is not null
200+ if (sourceOptions == VectorSourceOptions .INCLUDE_SOURCE_VECTORS && indexedVectors .get (id ) != null ) {
201+ assertThat (source , hasKey ("vector" ));
202+ assertNotNull (source .get ("vector" ));
203+ } else {
204+ assertThat (source , Matchers .aMapWithSize (1 ));
205+ }
206+ });
207+ }
208+ }
209+
183210 public void testNonIndexedDenseVectorField () throws IOException {
184211 createIndexWithDenseVector ("no_dense_vectors" , 64 );
185212
0 commit comments