Skip to content

Commit fc9bbc5

Browse files
benwtrentmridula-s109
authored andcommitted
Add more test coverage for nested searches over flat vector indices (elastic#130263)
PR elastic#130251 made me realize we were missing some important coverage. This adds nested vector query (and top level knn) tests for flat indices in our yaml tests.
1 parent 9786740 commit fc9bbc5

File tree

6 files changed

+644
-0
lines changed

6 files changed

+644
-0
lines changed

rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/search.vectors/42_knn_search_bbq_flat.yml

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,3 +350,153 @@ setup:
350350
index: bbq_flat
351351

352352
- match: { bbq_flat.mappings.properties.vector.index_options.rescore_vector.oversample: 3.0 }
353+
---
354+
"Test nested queries":
355+
- do:
356+
indices.create:
357+
index: bbq_flat_nested
358+
body:
359+
settings:
360+
index:
361+
number_of_shards: 1
362+
mappings:
363+
properties:
364+
name:
365+
type: keyword
366+
nested:
367+
type: nested
368+
properties:
369+
paragraph_id:
370+
type: keyword
371+
vector:
372+
type: dense_vector
373+
dims: 64
374+
index: true
375+
similarity: max_inner_product
376+
index_options:
377+
type: bbq_flat
378+
379+
- do:
380+
index:
381+
index: bbq_flat_nested
382+
id: "1"
383+
body:
384+
nested:
385+
- paragraph_id: "1"
386+
vector: [ 0.077, 0.32 , -0.205, 0.63 , 0.032, 0.201, 0.167, -0.313,
387+
0.176, 0.531, -0.375, 0.334, -0.046, 0.078, -0.349, 0.272,
388+
0.307, -0.083, 0.504, 0.255, -0.404, 0.289, -0.226, -0.132,
389+
-0.216, 0.49 , 0.039, 0.507, -0.307, 0.107, 0.09 , -0.265,
390+
-0.285, 0.336, -0.272, 0.369, -0.282, 0.086, -0.132, 0.475,
391+
-0.224, 0.203, 0.439, 0.064, 0.246, -0.396, 0.297, 0.242,
392+
-0.028, 0.321, -0.022, -0.009, -0.001 , 0.031, -0.533, 0.45,
393+
-0.028, 0.321, -0.022, -0.009, -0.001 , 0.031, -0.533, 0.45 ]
394+
- paragraph_id: "2"
395+
vector: [ 0.7, 0.2 , 0.205, 0.63 , 0.032, 0.201, 0.167, 0.313,
396+
0.176, 0.1, 0.375, 0.334, 0.046, 0.078, 0.349, 0.272,
397+
0.307, 0.083, 0.504, 0.255, 0.404, 0.289, 0.226, 0.132,
398+
0.216, 0.49 , 0.039, 0.507, -0.307, 0.107, 0.09 , 0.265,
399+
0.285, 0.336, 0.272, 0.369, -0.282, 0.086, 0.132, 0.475,
400+
0.224, 0.203, 0.439, 0.064, 0.246, 0.396, 0.297, 0.242,
401+
0.224, 0.203, 0.439, 0.064, 0.246, 0.396, 0.297, 0.242,
402+
0.028, 0.321, 0.022, 0.009, 0.001 , 0.031, -0.533, 0.45]
403+
- do:
404+
index:
405+
index: bbq_flat_nested
406+
id: "2"
407+
body:
408+
nested:
409+
- paragraph_id: 0
410+
vector: [ 0.196, 0.514, 0.039, 0.555, -0.042, 0.242, 0.463, -0.348,
411+
-0.08 , 0.442, -0.067, -0.05 , -0.001, 0.298, -0.377, 0.048,
412+
0.307, 0.159, 0.278, 0.119, -0.057, 0.333, -0.289, -0.438,
413+
-0.014, 0.361, -0.169, 0.292, -0.229, 0.123, 0.031, -0.138,
414+
-0.139, 0.315, -0.216, 0.322, -0.445, -0.059, 0.071, 0.429,
415+
-0.602, -0.142, 0.11 , 0.192, 0.259, -0.241, 0.181, -0.166,
416+
0.082, 0.107, -0.05 , 0.155, 0.011, 0.161, -0.486, 0.569,
417+
-0.489, 0.901, 0.208, 0.011, -0.209, -0.153, -0.27, -0.013 ]
418+
- paragraph_id: 2
419+
vector: [ 0.196, 0.514, 0.039, 0.555, 0.042, 0.242, 0.463, -0.348,
420+
-0.08 , 0.442, -0.067, -0.05 , -0.001, 0.298, -0.377, 0.048,
421+
0.307, 0.159, 0.278, 0.119, -0.057, 0.333, -0.289, 0.438,
422+
-0.014, 0.361, -0.169, 0.292, -0.229, 0.123, 0.031, 0.138,
423+
-0.139, 0.315, -0.216, 0.322, -0.445, -0.059, 0.071, 0.429,
424+
-0.602, 0.142, 0.11 , 0.192, 0.259, -0.241, 0.181, 0.166,
425+
0.082, 0.107, -0.05 , 0.155, 0.011, 0.161, -0.486, 0.569,
426+
-0.489, 0.901, 0.208, 0.011, -0.209, -0.153, -0.27, 0.013 ]
427+
- paragraph_id: 3
428+
vector: [ 0.196, 0.514, 0.039, 0.555, 0.042, 0.242, 0.463, -0.348,
429+
0.08 , 0.442, -0.067, -0.05 , 0.001, 0.298, -0.377, 0.048,
430+
0.307, 0.159, 0.278, 0.119, 0.057, 0.333, -0.289, -0.438,
431+
-0.014, 0.361, -0.169, 0.292, 0.229, 0.123, 0.031, -0.138,
432+
-0.139, 0.315, -0.216, 0.322, 0.445, -0.059, 0.071, 0.429,
433+
-0.602, -0.142, 0.11 , 0.192, 0.259, -0.241, 0.181, -0.166,
434+
0.082, 0.107, -0.05 , 0.155, 0.011, 0.161, -0.486, 0.569,
435+
-0.489, 0.901, 0.208, 0.011, 0.209, -0.153, -0.27, -0.013 ]
436+
437+
- do:
438+
index:
439+
index: bbq_flat_nested
440+
id: "3"
441+
body:
442+
nested:
443+
- paragraph_id: 0
444+
vector: [ 0.139, 0.178, -0.117, 0.399, 0.014, -0.139, 0.347, -0.33 ,
445+
0.139, 0.34 , -0.052, -0.052, -0.249, 0.327, -0.288, 0.049,
446+
0.464, 0.338, 0.516, 0.247, -0.104, 0.259, -0.209, -0.246,
447+
-0.11 , 0.323, 0.091, 0.442, -0.254, 0.195, -0.109, -0.058,
448+
-0.279, 0.402, -0.107, 0.308, -0.273, 0.019, 0.082, 0.399,
449+
-0.658, -0.03 , 0.276, 0.041, 0.187, -0.331, 0.165, 0.017,
450+
0.171, -0.203, -0.198, 0.115, -0.007, 0.337, -0.444, 0.615,
451+
-0.657, 1.285, 0.2 , -0.062, 0.038, 0.089, -0.068, -0.058 ]
452+
453+
- do:
454+
indices.flush:
455+
index: bbq_flat_nested
456+
457+
- do:
458+
indices.forcemerge:
459+
index: bbq_flat_nested
460+
max_num_segments: 1
461+
462+
- do:
463+
search:
464+
index: bbq_flat_nested
465+
body:
466+
query:
467+
nested:
468+
path: nested
469+
query:
470+
knn:
471+
field: nested.vector
472+
query_vector: [0.128, 0.067, -0.08 , 0.395, -0.11 , -0.259, 0.473, -0.393,
473+
0.292, 0.571, -0.491, 0.444, -0.288, 0.198, -0.343, 0.015,
474+
0.232, 0.088, 0.228, 0.151, -0.136, 0.236, -0.273, -0.259,
475+
-0.217, 0.359, -0.207, 0.352, -0.142, 0.192, -0.061, -0.17 ,
476+
-0.343, 0.189, -0.221, 0.32 , -0.301, -0.1 , 0.005, 0.232,
477+
-0.344, 0.136, 0.252, 0.157, -0.13 , -0.244, 0.193, -0.034,
478+
-0.12 , -0.193, -0.102, 0.252, -0.185, -0.167, -0.575, 0.582,
479+
-0.426, 0.983, 0.212, 0.204, 0.03 , -0.276, -0.425, -0.158]
480+
num_candidates: 3
481+
k: 2
482+
483+
- match: {hits.hits.0._id: "3"}
484+
485+
- do:
486+
search:
487+
index: bbq_flat_nested
488+
body:
489+
knn:
490+
field: nested.vector
491+
query_vector: [0.128, 0.067, -0.08 , 0.395, -0.11 , -0.259, 0.473, -0.393,
492+
0.292, 0.571, -0.491, 0.444, -0.288, 0.198, -0.343, 0.015,
493+
0.232, 0.088, 0.228, 0.151, -0.136, 0.236, -0.273, -0.259,
494+
-0.217, 0.359, -0.207, 0.352, -0.142, 0.192, -0.061, -0.17 ,
495+
-0.343, 0.189, -0.221, 0.32 , -0.301, -0.1 , 0.005, 0.232,
496+
-0.344, 0.136, 0.252, 0.157, -0.13 , -0.244, 0.193, -0.034,
497+
-0.12 , -0.193, -0.102, 0.252, -0.185, -0.167, -0.575, 0.582,
498+
-0.426, 0.983, 0.212, 0.204, 0.03 , -0.276, -0.425, -0.158]
499+
num_candidates: 3
500+
k: 2
501+
502+
- match: {hits.hits.0._id: "3"}

rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/search.vectors/42_knn_search_flat.yml

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,3 +327,103 @@ setup:
327327
index_options:
328328
type: flat
329329
m: 42
330+
---
331+
"Nested flat search":
332+
- do:
333+
indices.create:
334+
index: flat_nested
335+
body:
336+
settings:
337+
index:
338+
number_of_shards: 1
339+
mappings:
340+
properties:
341+
name:
342+
type: keyword
343+
nested:
344+
type: nested
345+
properties:
346+
paragraph_id:
347+
type: keyword
348+
vector:
349+
type: dense_vector
350+
dims: 5
351+
index: true
352+
similarity: l2_norm
353+
index_options:
354+
type: flat
355+
- do:
356+
index:
357+
index: flat_nested
358+
id: "1"
359+
body:
360+
name: cow.jpg
361+
nested:
362+
- paragraph_id: 0
363+
vector: [230.0, 300.33, -34.8988, 15.555, -200.0]
364+
- paragraph_id: 1
365+
vector: [240.0, 300, -3, 1, -20]
366+
367+
- do:
368+
index:
369+
index: flat_nested
370+
id: "2"
371+
body:
372+
name: moose.jpg
373+
nested:
374+
- paragraph_id: 0
375+
vector: [-0.5, 100.0, -13, 14.8, -156.0]
376+
- paragraph_id: 2
377+
vector: [0, 100.0, 0, 14.8, -156.0]
378+
- paragraph_id: 3
379+
vector: [0, 1.0, 0, 1.8, -15.0]
380+
381+
- do:
382+
index:
383+
index: flat_nested
384+
id: "3"
385+
body:
386+
name: rabbit.jpg
387+
nested:
388+
- paragraph_id: 0
389+
vector: [0.5, 111.3, -13.0, 14.8, -156.0]
390+
391+
- do:
392+
indices.refresh: {}
393+
- do:
394+
search:
395+
index: flat_nested
396+
body:
397+
fields: [ "name" ]
398+
query:
399+
nested:
400+
path: nested
401+
query:
402+
knn:
403+
field: nested.vector
404+
query_vector: [ -0.5, 90.0, -10, 14.8, -156.0 ]
405+
num_candidates: 3
406+
k: 2
407+
408+
- match: { hits.hits.0._id: "2" }
409+
- match: { hits.hits.0.fields.name.0: "moose.jpg" }
410+
411+
- match: { hits.hits.1._id: "3" }
412+
- match: { hits.hits.1.fields.name.0: "rabbit.jpg" }
413+
414+
- do:
415+
search:
416+
index: flat_nested
417+
body:
418+
fields: [ "name" ]
419+
knn:
420+
field: nested.vector
421+
query_vector: [ -0.5, 90.0, -10, 14.8, -156.0 ]
422+
num_candidates: 3
423+
k: 2
424+
425+
- match: { hits.hits.0._id: "2" }
426+
- match: { hits.hits.0.fields.name.0: "moose.jpg" }
427+
428+
- match: { hits.hits.1._id: "3" }
429+
- match: { hits.hits.1.fields.name.0: "rabbit.jpg" }

rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/search.vectors/42_knn_search_int4_flat.yml

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,3 +506,104 @@ setup:
506506
index: int4_flat
507507

508508
- not_exists: int4_flat.mappings.properties.vector.index_options.rescore_vector
509+
---
510+
"Nested flat search":
511+
- do:
512+
indices.create:
513+
index: int4_flat_nested
514+
body:
515+
settings:
516+
index:
517+
number_of_shards: 1
518+
mappings:
519+
properties:
520+
name:
521+
type: keyword
522+
nested:
523+
type: nested
524+
properties:
525+
paragraph_id:
526+
type: keyword
527+
vector:
528+
type: dense_vector
529+
dims: 4
530+
index: true
531+
similarity: l2_norm
532+
index_options:
533+
type: int4_flat
534+
- do:
535+
index:
536+
index: int4_flat_nested
537+
id: "1"
538+
body:
539+
name: cow.jpg
540+
nested:
541+
- paragraph_id: 0
542+
vector: [230.0, 300.33, -34.8988, 15.555 ]
543+
- paragraph_id: 1
544+
vector: [240.0, 300, -3, 1 ]
545+
546+
- do:
547+
index:
548+
index: int4_flat_nested
549+
id: "2"
550+
body:
551+
name: moose.jpg
552+
nested:
553+
- paragraph_id: 0
554+
vector: [-0.5, 100.0, -13, 14.8]
555+
- paragraph_id: 2
556+
vector: [0, 100.0, 0, 14.8]
557+
- paragraph_id: 3
558+
vector: [0, 1.0, 0, 1.8]
559+
560+
- do:
561+
index:
562+
index: int4_flat_nested
563+
id: "3"
564+
body:
565+
name: rabbit.jpg
566+
nested:
567+
- paragraph_id: 0
568+
vector: [0.5, 111.3, -13.0, 14.8]
569+
570+
- do:
571+
indices.refresh: {}
572+
- do:
573+
search:
574+
index: int4_flat_nested
575+
body:
576+
fields: [ "name" ]
577+
query:
578+
nested:
579+
path: nested
580+
query:
581+
knn:
582+
field: nested.vector
583+
query_vector: [ -0.5, 90.0, -10, 14.8]
584+
num_candidates: 3
585+
k: 2
586+
587+
- match: { hits.hits.0._id: "2" }
588+
- match: { hits.hits.0.fields.name.0: "moose.jpg" }
589+
590+
- match: { hits.hits.1._id: "3" }
591+
- match: { hits.hits.1.fields.name.0: "rabbit.jpg" }
592+
593+
- do:
594+
search:
595+
index: int4_flat_nested
596+
body:
597+
fields: [ "name" ]
598+
knn:
599+
field: nested.vector
600+
query_vector: [ -0.5, 90.0, -10, 14.8]
601+
num_candidates: 3
602+
k: 2
603+
604+
- match: { hits.hits.0._id: "2" }
605+
- match: { hits.hits.0.fields.name.0: "moose.jpg" }
606+
607+
- match: { hits.hits.1._id: "3" }
608+
- match: { hits.hits.1.fields.name.0: "rabbit.jpg" }
609+

0 commit comments

Comments
 (0)