For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

MongoDB Vector Search

In this guide, you can learn how to use the MongoDB Vector Search feature in the Scala driver. The Aggregates builders class provides the vectorSearch() helper method that you can use to create a $vectorSearch pipeline stage. This pipeline stage allows you to perform a semantic search on your documents. A semantic search is a type of search which locates information that is similar in meaning, but not necessarily identical, to your provided search term or phrase.

Important

Feature Compatibility

MongoDB Vector Search supports approximate nearest neighbor (ANN) search on Atlas clusters running MongoDB v6.0.11, v7.0.2, or later and exact nearest neighbor (ENN) search on clusters running MongoDB v6.0.16, v7.0.10, v7.3.2, or later. You can also use MongoDB Vector Search with self-managed or local Atlas deployments that you create with the Atlas CLI.

The examples in this guide use the embedded_movies collection in the sample_mflix database from the Atlas sample datasets. To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see the Get Started with the Scala Driver tutorial.

To run vector search queries, you must create a vector search index and index your vector embeddings. To learn about how to programmatically create a vector search index, see the Create Search and Vector Search Indexes section in the Indexes guide. To learn more about vector embeddings, see How to Index Vector Embeddings for Vector Search in the Atlas documentation.

After you create a vector search index on your vector embeddings, you can reference this index in your pipeline stage, as shown in the following section.

The example in this section requires a vector search index on the embedded_movies collection with the following configuration:

  • Index name: vector_index

  • Vector field: plot_embedding, with 1536 dimensions

  • Filter field: year, indexed as the filter type to support the pre-filter on the year field

This MongoDB Vector Search example performs the following actions:

  • Creates a query vector as a Seq[Double]

  • Builds an aggregation pipeline that runs an ENN search on the plot_embedding field

  • Limits results to 1 document

  • Matches documents where the year value is greater than or equal to 2016

  • Projects the vector search score into a field called vectorSearchScore

// Create an instance of the Seq[Double] class as the query vector
val rawVector: Array[Float] = Array( -0.0016261312f, -0.028070757f, -0.011342932f, -0.012775794f, -0.0027440966f, 0.008683807f, -0.02575152f, -0.02020668f, -0.010283281f, -0.0041719596f, 0.021392956f, 0.028657231f, -0.006634482f, 0.007490867f, 0.018593878f, 0.0038187427f, 0.029590257f, -0.01451522f, 0.016061379f, 0.00008528442f, -0.008943722f, 0.01627464f, 0.024311995f, -0.025911469f, 0.00022596726f, -0.008863748f, 0.008823762f, -0.034921836f, 0.007910728f, -0.01515501f, 0.035801545f, -0.0035688248f, -0.020299982f, -0.03145631f, -0.032256044f, -0.028763862f, -0.0071576433f, -0.012769129f, 0.012322609f, -0.006621153f, 0.010583182f, 0.024085402f, -0.001623632f, 0.007864078f, -0.021406285f, 0.002554159f, 0.012229307f, -0.011762793f, 0.0051682983f, 0.0048484034f, 0.018087378f, 0.024325324f, -0.037694257f, -0.026537929f, -0.008803768f, -0.017767483f, -0.012642504f, -0.0062712682f, 0.0009771782f, -0.010409906f, 0.017754154f, -0.004671795f, -0.030469967f, 0.008477209f, -0.005218282f, -0.0058480743f, -0.020153364f, -0.0032805866f, 0.004248601f, 0.0051449724f, 0.006791097f, 0.007650814f, 0.003458861f, -0.0031223053f, -0.01932697f, -0.033615597f, 0.00745088f, 0.006321252f, -0.0038154104f, 0.014555207f, 0.027697546f, -0.02828402f, 0.0066711367f, 0.0077107945f, 0.01794076f, 0.011349596f, -0.0052715978f, 0.014755142f, -0.019753495f, -0.011156326f, 0.011202978f, 0.022126047f, 0.00846388f, 0.030549942f, -0.0041386373f, 0.018847128f, -0.00033655585f, 0.024925126f, -0.003555496f, -0.019300312f, 0.010749794f, 0.0075308536f, -0.018287312f, -0.016567878f, -0.012869096f, -0.015528221f, 0.0078107617f, -0.011156326f, 0.013522214f, -0.020646535f, -0.01211601f, 0.055928253f, 0.011596181f, -0.017247654f, 0.0005939711f, -0.026977783f, -0.003942035f, -0.009583511f, -0.0055248477f, -0.028737204f, 0.023179034f, 0.003995351f, 0.0219661f, -0.008470545f, 0.023392297f, 0.010469886f, -0.015874773f, 0.007890735f, -0.009690142f, -0.00024970944f, 0.012775794f, 0.0114762215f, 0.013422247f, 0.010429899f, -0.03686786f, -0.006717788f, -0.027484283f, 0.011556195f, -0.036068123f, -0.013915418f, -0.0016327957f, 0.0151016945f, -0.020473259f, 0.004671795f, -0.012555866f, 0.0209531f, 0.01982014f, 0.024485271f, 0.0105431955f, -0.005178295f, 0.033162415f, -0.013795458f, 0.007150979f, 0.010243294f, 0.005644808f, 0.017260984f, -0.0045618312f, 0.0024725192f, 0.004305249f, -0.008197301f, 0.0014203656f, 0.0018460588f, 0.005015015f, -0.011142998f, 0.01439526f, 0.022965772f, 0.02552493f, 0.007757446f, -0.0019726837f, 0.009503538f, -0.032042783f, 0.008403899f, -0.04609149f, 0.013808787f, 0.011749465f, 0.036388017f, 0.016314628f, 0.021939443f, -0.0250051f, -0.017354285f, -0.012962398f, 0.00006107364f, 0.019113706f, 0.03081652f, -0.018114036f, -0.0084572155f, 0.009643491f, -0.0034721901f, 0.0072642746f, -0.0090636825f, 0.01642126f, 0.013428912f, 0.027724205f, 0.0071243206f, -0.6858542f, -0.031029783f, -0.014595194f, -0.011449563f, 0.017514233f, 0.01743426f, 0.009950057f, 0.0029706885f, -0.015714826f, -0.001806072f, 0.011856096f, 0.026444625f, -0.0010663156f, -0.006474535f, 0.0016161345f, -0.020313311f, 0.0148351155f, -0.0018393943f, 0.0057347785f, 0.018300641f, -0.018647194f, 0.03345565f, -0.008070676f, 0.0071443142f, 0.014301958f, 0.0044818576f, 0.003838736f, -0.007350913f, -0.024525259f, -0.001142124f, -0.018620536f, 0.017247654f, 0.007037683f, 0.010236629f, 0.06046009f, 0.0138887605f, -0.012122675f, 0.037694257f, 0.0055081863f, 0.042492677f, 0.00021784494f, -0.011656162f, 0.010276617f, 0.022325981f, 0.005984696f, -0.009496873f, 0.013382261f, -0.0010563189f, 0.0026507939f, -0.041639622f, 0.008637156f, 0.026471283f, -0.008403899f, 0.024858482f, -0.00066686375f, -0.0016252982f, 0.027590916f, 0.0051449724f, 0.0058647357f, -0.008743787f, -0.014968405f, 0.027724205f, -0.011596181f, 0.0047650975f, -0.015381602f, 0.0043718936f, 0.002159289f, 0.035908177f, -0.008243952f, -0.030443309f, 0.027564257f, 0.042625964f, -0.0033688906f, 0.01843393f, 0.019087048f, 0.024578573f, 0.03268257f, -0.015608194f, -0.014128681f, -0.0033538956f, -0.0028757197f, -0.004121976f, -0.032389335f, 0.0034322033f, 0.058807302f, 0.010943064f, -0.030523283f, 0.008903735f, 0.017500903f, 0.00871713f, -0.0029406983f, 0.013995391f, -0.03132302f, -0.019660193f, -0.00770413f, -0.0038853872f, 0.0015894766f, -0.0015294964f, -0.006251275f, -0.021099718f, -0.010256623f, -0.008863748f, 0.028550599f, 0.02020668f, -0.0012962399f, -0.003415542f, -0.0022509254f, 0.0119360695f, 0.027590916f, -0.046971202f, -0.0015194997f, -0.022405956f, 0.0016677842f, -0.00018535563f, -0.015421589f, -0.031802863f, 0.03814744f, 0.0065411795f, 0.016567878f, -0.015621523f, 0.022899127f, -0.011076353f, 0.02841731f, -0.002679118f, -0.002342562f, 0.015341615f, 0.01804739f, -0.020566562f, -0.012989056f, -0.002990682f, 0.01643459f, 0.00042527664f, 0.008243952f, -0.013715484f, -0.004835075f, -0.009803439f, 0.03129636f, -0.021432944f, 0.0012087687f, -0.015741484f, -0.0052016205f, 0.00080890034f, -0.01755422f, 0.004811749f, -0.017967418f, -0.026684547f, -0.014128681f, 0.0041386373f, -0.013742141f, -0.010056688f, -0.013268964f, -0.0110630235f, -0.028337335f, 0.015981404f, -0.00997005f, -0.02424535f, -0.013968734f, -0.028310679f, -0.027750863f, -0.020699851f, 0.02235264f, 0.001057985f, 0.00081639783f, -0.0099367285f, 0.013522214f, -0.012016043f, -0.00086471526f, 0.013568865f, 0.0019376953f, -0.019020405f, 0.017460918f, -0.023045745f, 0.008503866f, 0.0064678704f, -0.011509543f, 0.018727167f, -0.003372223f, -0.0028690554f, -0.0027024434f, -0.011902748f, -0.012182655f, -0.015714826f, -0.0098634185f, 0.00593138f, 0.018753825f, 0.0010146659f, 0.013029044f, 0.0003521757f, -0.017620865f, 0.04102649f, 0.00552818f, 0.024485271f, -0.009630162f, -0.015608194f, 0.0006718621f, -0.0008418062f, 0.012395918f, 0.0057980907f, 0.016221326f, 0.010616505f, 0.004838407f, -0.012402583f, 0.019900113f, -0.0034521967f, 0.000247002f, -0.03153628f, 0.0011038032f, -0.020819811f, 0.016234655f, -0.00330058f, -0.0032289368f, 0.00078973995f, -0.021952773f, -0.022459272f, 0.03118973f, 0.03673457f, -0.021472929f, 0.0072109587f, -0.015075036f, 0.004855068f, -0.0008151483f, 0.0069643734f, 0.010023367f, -0.010276617f, -0.023019087f, 0.0068244194f, -0.0012520878f, -0.0015086699f, 0.022046074f, -0.034148756f, -0.0022192693f, 0.002427534f, -0.0027124402f, 0.0060346797f, 0.015461575f, 0.0137554705f, 0.009230294f, -0.009583511f, 0.032629255f, 0.015994733f, -0.019167023f, -0.009203636f, 0.03393549f, -0.017274313f, -0.012042701f, -0.0009930064f, 0.026777849f, -0.013582194f, -0.0027590916f, -0.017594207f, -0.026804507f, -0.0014236979f, -0.022032745f, 0.0091236625f, -0.0042419364f, -0.00858384f, -0.0033905501f, -0.020739838f, 0.016821127f, 0.022539245f, 0.015381602f, 0.015141681f, 0.028817179f, -0.019726837f, -0.0051283115f, -0.011489551f, -0.013208984f, -0.0047017853f, -0.0072309524f, 0.01767418f, 0.0025658219f, -0.010323267f, 0.012609182f, -0.028097415f, 0.026871152f, -0.010276617f, 0.021912785f, 0.0022542577f, 0.005124979f, -0.0019710176f, 0.004518512f, -0.040360045f, 0.010969722f, -0.0031539614f, -0.020366628f, -0.025778178f, -0.0110030435f, -0.016221326f, 0.0036587953f, 0.016207997f, 0.003007343f, -0.0032555948f, 0.0044052163f, -0.022046074f, -0.0008822095f, -0.009363583f, 0.028230704f, -0.024538586f, 0.0029840174f, 0.0016044717f, -0.014181997f, 0.031349678f, -0.014381931f, -0.027750863f, 0.02613806f, 0.0004136138f, -0.005748107f, -0.01868718f, -0.0010138329f, 0.0054348772f, 0.010703143f, -0.003682121f, 0.0030856507f, -0.004275259f, -0.010403241f, 0.021113047f, -0.022685863f, -0.023032416f, 0.031429652f, 0.001792743f, -0.005644808f, -0.011842767f, -0.04078657f, -0.0026874484f, 0.06915057f, -0.00056939584f, -0.013995391f, 0.010703143f, -0.013728813f, -0.022939114f, -0.015261642f, -0.022485929f, 0.016807798f, 0.007964044f, 0.0144219175f, 0.016821127f, 0.0076241563f, 0.005461535f, -0.013248971f, 0.015301628f, 0.0085171955f, -0.004318578f, 0.011136333f, -0.0059047225f, -0.010249958f, -0.018207338f, 0.024645219f, 0.021752838f, 0.0007614159f, -0.013648839f, 0.01111634f, -0.010503208f, -0.0038487327f, -0.008203966f, -0.00397869f, 0.0029740208f, 0.008530525f, 0.005261601f, 0.01642126f, -0.0038753906f, -0.013222313f, 0.026537929f, 0.024671877f, -0.043505676f, 0.014195326f, 0.024778508f, 0.0056914594f, -0.025951454f, 0.017620865f, -0.0021359634f, 0.008643821f, 0.021299653f, 0.0041686273f, -0.009017031f, 0.04044002f, 0.024378639f, -0.027777521f, -0.014208655f, 0.0028623908f, 0.042119466f, 0.005801423f, -0.028124074f, -0.03129636f, 0.022139376f, -0.022179363f, -0.04067994f, 0.013688826f, 0.013328944f, 0.0046184794f, -0.02828402f, -0.0063412455f, -0.0046184794f, -0.011756129f, -0.010383247f, -0.0018543894f, -0.0018593877f, -0.00052024535f, 0.004815081f, 0.014781799f, 0.018007403f, 0.01306903f, -0.020433271f, 0.009043689f, 0.033189073f, -0.006844413f, -0.019766824f, -0.018767154f, 0.00533491f, -0.0024575242f, 0.018727167f, 0.0058080875f, -0.013835444f, 0.0040719924f, 0.004881726f, 0.012029372f, 0.005664801f, 0.03193615f, 0.0058047553f, 0.002695779f, 0.009290274f, 0.02361889f, 0.017834127f, 0.0049017193f, -0.0036388019f, 0.010776452f, -0.019793482f, 0.0067777685f, -0.014208655f, -0.024911797f, 0.002385881f, 0.0034988478f, 0.020899786f, -0.0025858153f, -0.011849431f, 0.033189073f, -0.021312982f, 0.024965113f, -0.014635181f, 0.014048708f, -0.0035921505f, -0.003347231f, 0.030869836f, -0.0017161017f, -0.0061346465f, 0.009203636f, -0.025165047f, 0.0068510775f, 0.021499587f, 0.013782129f, -0.0024475274f, -0.0051149824f, -0.024445284f, 0.006167969f, 0.0068844f, -0.00076183246f, 0.030150073f, -0.0055948244f, -0.011162991f, -0.02057989f, -0.009703471f, -0.020646535f, 0.008004031f, 0.0066378145f, -0.019900113f, -0.012169327f, -0.01439526f, 0.0044252095f, -0.004018677f, 0.014621852f, -0.025085073f, -0.013715484f, -0.017980747f, 0.0071043274f, 0.011456228f, -0.01010334f, -0.0035321703f, -0.03801415f, -0.012036037f, -0.0028990454f, -0.05419549f, -0.024058744f, -0.024272008f, 0.015221654f, 0.027964126f, 0.03182952f, -0.015354944f, 0.004855068f, 0.011522872f, 0.004771762f, 0.0027874154f, 0.023405626f, 0.0004242353f, -0.03132302f, 0.007057676f, 0.008763781f, -0.0027057757f, 0.023005757f, -0.0071176565f, -0.005238275f, 0.029110415f, -0.010989714f, 0.013728813f, -0.009630162f, -0.029137073f, -0.0049317093f, -0.0008630492f, -0.015248313f, 0.0043219104f, -0.0055681667f, -0.013175662f, 0.029723546f, 0.025098402f, 0.012849103f, -0.0009996708f, 0.03118973f, -0.0021709518f, 0.0260181f, -0.020526575f, 0.028097415f, -0.016141351f, 0.010509873f, -0.022965772f, 0.002865723f, 0.0020493253f, 0.0020509914f, -0.0041419696f, -0.00039695262f, 0.017287642f, 0.0038987163f, 0.014795128f, -0.014661839f, -0.008950386f, 0.004431874f, -0.009383577f, 0.0012604183f, -0.023019087f, 0.0029273694f, -0.033135757f, 0.009176978f, -0.011023037f, -0.002102641f, 0.02663123f, -0.03849399f, -0.0044152127f, 0.0004527676f, -0.0026924468f, 0.02828402f, 0.017727496f, 0.035135098f, 0.02728435f, -0.005348239f, -0.001467017f, -0.019766824f, 0.014715155f, 0.011982721f, 0.0045651635f, 0.023458943f, -0.0010046692f, -0.0031373003f, -0.0006972704f, 0.0019043729f, -0.018967088f, -0.024311995f, 0.0011546199f, 0.007977373f, -0.004755101f, -0.010016702f, -0.02780418f, -0.004688456f, 0.013022379f, -0.005484861f, 0.0017227661f, -0.015394931f, -0.028763862f, -0.026684547f, 0.0030589928f, -0.018513903f, 0.028363993f, 0.0044818576f, -0.009270281f, 0.038920518f, -0.016008062f, 0.0093902415f, 0.004815081f, -0.021059733f, 0.01451522f, -0.0051583014f, 0.023765508f, -0.017874114f, -0.016821127f, -0.012522544f, -0.0028390652f, 0.0040886537f, 0.020259995f, -0.031216389f, -0.014115352f, -0.009176978f, 0.010303274f, 0.020313311f, 0.0064112223f, -0.02235264f, -0.022872468f, 0.0052449396f, 0.0005723116f, 0.0037321046f, 0.016807798f, -0.018527232f, -0.009303603f, 0.0024858483f, -0.0012662497f, -0.007110992f, 0.011976057f, -0.007790768f, -0.042999174f, -0.006727785f, -0.011829439f, 0.007024354f, 0.005278262f, -0.017740825f, -0.0041519664f, 0.0085905045f, 0.027750863f, -0.038387362f, 0.024391968f, 0.00087721116f, 0.010509873f, -0.00038508154f, -0.006857742f, 0.0183273f, -0.0037054466f, 0.015461575f, 0.0017394272f, -0.0017944091f, 0.014181997f, -0.0052682655f, 0.009023695f, 0.00719763f, -0.013522214f, 0.0034422f, 0.014941746f, -0.0016711164f, -0.025298337f, -0.017634194f, 0.0058714002f, -0.005321581f, 0.017834127f, 0.0110630235f, -0.03369557f, 0.029190388f, -0.008943722f, 0.009363583f, -0.0034222065f, -0.026111402f, -0.007037683f, -0.006561173f, 0.02473852f, -0.007084334f, -0.010110005f, -0.008577175f, 0.0030439978f, -0.022712521f, 0.0054582027f, -0.0012620845f, -0.0011954397f, -0.015741484f, 0.0129557345f, -0.00042111133f, 0.00846388f, 0.008930393f, 0.016487904f, 0.010469886f, -0.007917393f, -0.011762793f, -0.0214596f, 0.000917198f, 0.021672864f, 0.010269952f, -0.007737452f, -0.010243294f, -0.0067244526f, -0.015488233f, -0.021552904f, 0.017127695f, 0.011109675f, 0.038067464f, 0.00871713f, -0.0025591573f, 0.021312982f, -0.006237946f, 0.034628596f, -0.0045251767f, 0.008357248f, 0.020686522f, 0.0010696478f, 0.0076708077f, 0.03772091f, -0.018700508f, -0.0020676525f, -0.008923728f, -0.023298996f, 0.018233996f, -0.010256623f, 0.0017860786f, 0.009796774f, -0.00897038f, -0.01269582f, -0.018527232f, 0.009190307f, -0.02372552f, -0.042119466f, 0.008097334f, -0.0066778013f, -0.021046404f, 0.0019593548f, 0.011083017f, -0.0016028056f, 0.012662497f, -0.000059095124f, 0.0071043274f, -0.014675168f, 0.024831824f, -0.053582355f, 0.038387362f, 0.0005698124f, 0.015954746f, 0.021552904f, 0.031589597f, -0.009230294f, -0.0006147976f, 0.002625802f, -0.011749465f, -0.034362018f, -0.0067844326f, -0.018793812f, 0.011442899f, -0.008743787f, 0.017474247f, -0.021619547f, 0.01831397f, -0.009037024f, -0.0057247817f, -0.02728435f, 0.010363255f, 0.034415334f, -0.024032086f, -0.0020126705f, -0.0045518344f, -0.019353628f, -0.018340627f, -0.03129636f, -0.0034038792f, -0.006321252f, -0.0016161345f, 0.033642255f, -0.000056075285f, -0.005005019f, 0.004571828f, -0.0024075406f, -0.00010215386f, 0.0098634185f, 0.1980148f, -0.003825407f, -0.025191706f, 0.035161756f, 0.005358236f, 0.025111731f, 0.023485601f, 0.0023342315f, -0.011882754f, 0.018287312f, -0.0068910643f, 0.003912045f, 0.009243623f, -0.001355387f, -0.028603915f, -0.012802451f, -0.030150073f, -0.014795128f, -0.028630573f, -0.0013487226f, 0.002667455f, 0.00985009f, -0.0033972147f, -0.021486258f, 0.009503538f, -0.017847456f, 0.013062365f, -0.014341944f, 0.005078328f, 0.025165047f, -0.015594865f, -0.025924796f, -0.0018177348f, 0.010996379f, -0.02993681f, 0.007324255f, 0.014475234f, -0.028577257f, 0.005494857f, 0.00011725306f, -0.013315615f, 0.015941417f, 0.009376912f, 0.0025158382f, 0.008743787f, 0.023832154f, -0.008084005f, -0.014195326f, -0.008823762f, 0.0033455652f, -0.032362677f, -0.021552904f, -0.0056081535f, 0.023298996f, -0.025444955f, 0.0097301295f, 0.009736794f, 0.015274971f, -0.0012937407f, -0.018087378f, -0.0039387033f, 0.008637156f, -0.011189649f, -0.00023846315f, -0.011582852f, 0.0066411467f, -0.018220667f, 0.0060846633f, 0.0376676f, -0.002709108f, 0.0072776037f, 0.0034188742f, -0.010249958f, -0.0007747449f, -0.00795738f, -0.022192692f, 0.03910712f, 0.032122757f, 0.023898797f, 0.0076241563f, -0.007397564f, -0.003655463f, 0.011442899f, -0.014115352f, -0.00505167f, -0.031163072f, 0.030336678f, -0.006857742f, -0.022259338f, 0.004048667f, 0.02072651f, 0.0030156737f, -0.0042119464f, 0.00041861215f, -0.005731446f, 0.011103011f, 0.013822115f, 0.021512916f, 0.009216965f, -0.006537847f, -0.027057758f, -0.04054665f, 0.010403241f, -0.0056281467f, -0.005701456f, -0.002709108f, -0.00745088f, -0.0024841821f, 0.009356919f, -0.022659205f, 0.004061996f, -0.013175662f, 0.017074378f, -0.006141311f, -0.014541878f, 0.02993681f, -0.00028448965f, -0.025271678f, 0.011689484f, -0.014528549f, 0.004398552f, -0.017274313f, 0.0045751603f, 0.012455898f, 0.004121976f, -0.025458284f, -0.006744446f, 0.011822774f, -0.015035049f, -0.03257594f, 0.014675168f, -0.0039187097f, 0.019726837f, -0.0047251107f, 0.0022825818f, 0.011829439f, 0.005391558f, -0.016781142f, -0.0058747325f, 0.010309938f, -0.013049036f, 0.01186276f, -0.0011246296f, 0.0062112883f, 0.0028190718f, -0.021739509f, 0.009883412f, -0.0073175905f, -0.012715813f, -0.017181009f, -0.016607866f, -0.042492677f, -0.0014478565f, -0.01794076f, 0.012302616f, -0.015194997f, -0.04433207f, -0.020606548f, 0.009696807f, 0.010303274f, -0.01694109f, -0.004018677f, 0.019353628f, -0.001991011f, 0.000058938927f, 0.010536531f, -0.17274313f, 0.010143327f, 0.014235313f, -0.024152048f, 0.025684876f, -0.0012504216f, 0.036601283f, -0.003698782f, 0.0007310093f, 0.004165295f, -0.0029157067f, 0.017101036f, -0.046891227f, -0.017460918f, 0.022965772f, 0.020233337f, -0.024072073f, 0.017220996f, 0.009370248f, 0.0010363255f, 0.0194336f, -0.019606877f, 0.01818068f, -0.020819811f, 0.007410893f, 0.0019326969f, 0.017887443f, 0.006651143f, 0.00067394477f, -0.011889419f, -0.025058415f, -0.008543854f, 0.021579562f, 0.0047484366f, 0.014062037f, 0.0075508473f, -0.009510202f, -0.009143656f, 0.0046817916f, 0.013982063f, -0.0027990784f, 0.011782787f, 0.014541878f, -0.015701497f, -0.029350337f, 0.021979429f, 0.01332228f, -0.026244693f, -0.0123492675f, -0.003895384f, 0.0071576433f, -0.035454992f, -0.00046984528f, 0.0033522295f, 0.039347045f, 0.0005119148f, 0.00476843f, -0.012995721f, 0.0024042083f, -0.006931051f, -0.014461905f, -0.0127558f, 0.0034555288f, -0.0074842023f, -0.030256703f, -0.007057676f, -0.00807734f, 0.007804097f, -0.006957709f, 0.017181009f, -0.034575284f, -0.008603834f, -0.005008351f, -0.015834786f, 0.02943031f, 0.016861115f, -0.0050849924f, 0.014235313f, 0.0051449724f, 0.0025924798f, -0.0025741523f, 0.04289254f, -0.002104307f, 0.012969063f, -0.008310596f, 0.00423194f, 0.0074975314f, 0.0018810473f, -0.014248641f, -0.024725191f, 0.0151016945f, -0.017527562f, 0.0018727167f, 0.0002830318f, 0.015168339f, 0.0144219175f, -0.004048667f, -0.004358565f, 0.011836103f, -0.010343261f, -0.005911387f, 0.0022825818f, 0.0073175905f, 0.00403867f, 0.013188991f, 0.03334902f, 0.006111321f, 0.008597169f, 0.030123414f, -0.015474904f, 0.0017877447f, -0.024551915f, 0.013155668f, 0.023525586f, -0.0255116f, 0.017220996f, 0.004358565f, -0.00934359f, 0.0099967085f, 0.011162991f, 0.03092315f, -0.021046404f, -0.015514892f, 0.0011946067f, -0.01816735f, 0.010876419f, -0.10124666f, -0.03550831f, 0.0056348112f, 0.013942076f, 0.005951374f, 0.020419942f, -0.006857742f, -0.020873128f, -0.021259667f, 0.0137554705f, 0.0057880944f, -0.029163731f, -0.018767154f, -0.021392956f, 0.030896494f, -0.005494857f, -0.0027307675f, -0.006801094f, -0.014821786f, 0.021392956f, -0.0018110704f, -0.0018843795f, -0.012362596f, -0.0072176233f, -0.017194338f, -0.018713837f, -0.024272008f, 0.03801415f, 0.00015880188f, 0.0044951867f, -0.028630573f, -0.0014070367f, -0.00916365f, -0.026537929f, -0.009576847f, -0.013995391f, -0.0077107945f, 0.0050016865f, 0.00578143f, -0.04467862f, 0.008363913f, 0.010136662f, -0.0006268769f, -0.006591163f, 0.015341615f, -0.027377652f, -0.00093136f, 0.029243704f, -0.020886457f, -0.01041657f, -0.02424535f, 0.005291591f, -0.02980352f, -0.009190307f, 0.019460259f, -0.0041286405f, 0.004801752f, 0.0011787785f, -0.001257086f, -0.011216307f, -0.013395589f, 0.00088137644f, -0.0051616337f, 0.03876057f, -0.0033455652f, 0.00075850025f, -0.006951045f, -0.0062112883f, 0.018140694f, -0.006351242f, -0.008263946f, 0.018154023f, -0.012189319f, 0.0075508473f, -0.044358727f, -0.0040153447f, 0.0093302615f, -0.010636497f, 0.032789204f, -0.005264933f, -0.014235313f, -0.018393943f, 0.007297597f, -0.016114693f, 0.015021721f, 0.020033404f, 0.0137688f, 0.0011046362f, 0.010616505f, -0.0039453674f, 0.012109346f, 0.021099718f, -0.0072842683f, -0.019153694f, -0.003768759f, 0.039320387f, -0.006747778f, -0.0016852784f, 0.018154023f, 0.0010963057f, -0.015035049f, -0.021033075f, -0.04345236f, 0.017287642f, 0.016341286f, -0.008610498f, 0.00236922f, 0.009290274f, 0.028950468f, -0.014475234f, -0.0035654926f, 0.015434918f, -0.03372223f, 0.004501851f, -0.012929076f, -0.008483873f, -0.0044685286f, -0.0102233f, 0.01615468f, 0.0022792495f, 0.010876419f, -0.0059647025f, 0.01895376f, -0.0069976957f, -0.0042952523f, 0.017207667f, -0.00036133936f, 0.0085905045f, 0.008084005f, 0.03129636f, -0.016994404f, -0.014915089f, 0.020100048f, -0.012009379f, -0.006684466f, 0.01306903f, 0.00015765642f, -0.00530492f, 0.0005277429f, 0.015421589f, 0.015528221f, 0.032202728f, -0.003485519f, -0.0014286962f, 0.033908837f, 0.001367883f, 0.010509873f, 0.025271678f, -0.020993087f, 0.019846799f, 0.006897729f, -0.010216636f, -0.00725761f, 0.01818068f, -0.028443968f, -0.011242964f, -0.014435247f, -0.013688826f, 0.006101324f, -0.0022509254f, 0.013848773f, -0.0019077052f, 0.017181009f, 0.03422873f, 0.005324913f, -0.0035188415f, 0.014128681f, -0.004898387f, 0.005038341f, 0.0012320944f, -0.005561502f, -0.017847456f, 0.0008538855f, -0.0047884234f, 0.011849431f, 0.015421589f, -0.013942076f, 0.0029790192f, -0.013702155f, 0.0001199605f, -0.024431955f, 0.019926772f, 0.022179363f, -0.016487904f, -0.03964028f, 0.0050849924f, 0.017487574f, 0.022792496f, 0.0012504216f, 0.004048667f, -0.00997005f, 0.0076041627f, -0.014328616f, -0.020259995f, 0.0005598157f, -0.010469886f, 0.0016852784f, 0.01716768f, -0.008990373f, -0.001987679f, 0.026417969f, 0.023792166f, 0.0046917885f, -0.0071909656f, -0.00032051947f, -0.023259008f, -0.009170313f, 0.02071318f, -0.03156294f, -0.030869836f, -0.006324584f, 0.013795458f, -0.00047151142f, 0.016874444f, 0.00947688f, 0.00985009f, -0.029883493f, 0.024205362f, -0.013522214f, -0.015075036f, -0.030603256f, 0.029270362f, 0.010503208f, 0.021539574f, 0.01743426f, -0.023898797f, 0.022019416f, -0.0068777353f, 0.027857494f, -0.021259667f, 0.0025758184f, 0.006197959f, 0.006447877f, -0.00025200035f, -0.004941706f, -0.021246338f, -0.005504854f, -0.008390571f, -0.0097301295f, 0.027244363f, -0.04446536f, 0.05216949f, 0.010243294f, -0.016008062f, 0.0122493f, -0.0199401f, 0.009077012f, 0.019753495f, 0.006431216f, -0.037960835f, -0.027377652f, 0.016381273f, -0.0038620618f, 0.022512587f, -0.010996379f, -0.0015211658f, -0.0102233f, 0.007071005f, 0.008230623f, -0.009490209f, -0.010083347f, 0.024431955f, 0.002427534f, 0.02828402f, 0.0035721571f, -0.022192692f, -0.011882754f, 0.010056688f, 0.0011904413f, -0.01426197f, -0.017500903f, -0.00010985966f, 0.005591492f, -0.0077707744f, -0.012049366f, 0.011869425f, 0.00858384f, -0.024698535f, -0.030283362f, 0.020140035f, 0.011949399f, -0.013968734f, 0.042732596f, -0.011649498f, -0.011982721f, -0.016967745f, -0.0060913274f, -0.007130985f, -0.013109017f, -0.009710136f )
val queryVector: Seq[java.lang.Double] = rawVector.map(x => java.lang.Double.valueOf(x.toDouble)).toSeq
// Specify the index name for the vector embedding index
val indexName = "vector_index"
// Specify the path of the field to search on
val fieldSearchPath: FieldSearchPath = SearchPath.fieldPath("plot_embedding")
// Limit the number of matches to 1
val limit = 1L
// Create a pre-filter to only search within a subset of documents
val options: VectorSearchOptions = VectorSearchOptions.exactVectorSearchOptions()
.filter(Filters.gte("year", 2016))
// Create the vectorSearch pipeline stage
val observable: Observable[Document] = collection.aggregate(
Seq(
Aggregates.vectorSearch(
fieldSearchPath,
queryVector,
indexName,
limit,
options),
Aggregates.project(
Projections.metaVectorSearchScore("vectorSearchScore"))
)
)

Tip

Query Vector Type

The preceding example creates a Seq[Double] to serve as the query vector, but you can also use a BinaryVector. Use the BinaryVector type to improve storage efficiency.

The following example shows how you can run the aggregation and print the vector search meta-score from the result of the preceding aggregation pipeline:

observable.subscribe(
(doc: Document) => {
val score = doc.getDouble("vectorSearchScore")
println(s"vectorSearch score: $score")
},
(e: Throwable) => println(s"There was an error: $e")
)
vectorSearch score: 0.8821365833282471

To learn more about the methods and types mentioned in this guide, see the following API documentation: