{"id":9837,"date":"2025-04-01T05:00:09","date_gmt":"2025-04-01T12:00:09","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/cosmosdb\/?p=9837"},"modified":"2025-04-01T09:14:50","modified_gmt":"2025-04-01T16:14:50","slug":"exact-nearest-neighbor-enn-vector-search","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cosmosdb\/exact-nearest-neighbor-enn-vector-search\/","title":{"rendered":"Exact Nearest Neighbor (ENN) Vector Search for Precise Retrieval"},"content":{"rendered":"<p>In applications such as personalized recommendations and scientific research, accurately identifying the closest data points to a given query is crucial. Traditional methods like Approximate Nearest Neighbor (ANN) search offer faster retrieval times by approximating results but may sacrifice accuracy, leading to potential errors in scenarios where precision is essential. This trade-off between speed and accuracy poses a significant challenge when exact results are necessary.<\/p>\n<p><a href=\"https:\/\/aka.ms\/ennvectorsearch\" target=\"_blank\" rel=\"noopener\">Exact Vector Search (ENN)<\/a> is a search method that compares your input vector to every single vector in the dataset to find the closest matches. It\u2019s the most <strong>accurate<\/strong> way to find nearest neighbors because it doesn&#8217;t skip any candidates. In <strong data-start=\"462\" data-end=\"505\">vCore-based Azure Cosmos DB for MongoDB<\/strong>, ENN is supported as part of the vector search capabilities for <strong>no additional cost<\/strong>. It allows you to run high-precision searches across your vector-indexed collections without needing to rebuild existing indexes.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2025\/03\/enn-2.png\"><img decoding=\"async\" class=\"aligncenter wp-image-9859\" src=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2025\/03\/enn-2.png\" alt=\"enn in vCore-based Azure Cosmos DB for MongoDB\" width=\"450\" height=\"450\" srcset=\"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2025\/03\/enn-2.png 1024w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2025\/03\/enn-2-300x300.png 300w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2025\/03\/enn-2-150x150.png 150w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2025\/03\/enn-2-768x768.png 768w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2025\/03\/enn-2-24x24.png 24w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2025\/03\/enn-2-48x48.png 48w, https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-content\/uploads\/sites\/52\/2025\/03\/enn-2-96x96.png 96w\" sizes=\"(max-width: 450px) 100vw, 450px\" \/><\/a><\/p>\n<h2>ANN vs ENN: What\u2019s the Difference?<\/h2>\n<table style=\"width: 89.0772%; height: 120px;\">\n<thead>\n<tr style=\"height: 24px;\">\n<th style=\"height: 24px; width: 27.6065%;\">Feature<\/th>\n<th style=\"height: 24px; width: 36.3683%;\">ANN (Approximate)<\/th>\n<th style=\"height: 24px; width: 40.871%;\">ENN (Exact)<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr style=\"height: 24px;\">\n<td style=\"height: 24px; width: 27.6065%;\">Speed (on large dataset)<\/td>\n<td style=\"height: 24px; width: 36.3683%;\">Fast<\/td>\n<td style=\"height: 24px; width: 40.871%;\">Slower<\/td>\n<\/tr>\n<tr style=\"height: 24px;\">\n<td style=\"height: 24px; width: 27.6065%;\">Accuracy<\/td>\n<td style=\"height: 24px; width: 36.3683%;\">Close (not always 100%)<\/td>\n<td style=\"height: 24px; width: 40.871%;\">Always correct<\/td>\n<\/tr>\n<tr style=\"height: 24px;\">\n<td style=\"height: 24px; width: 27.6065%;\">Scales well?<\/td>\n<td style=\"height: 24px; width: 36.3683%;\">Yes (great for millions of vectors)<\/td>\n<td style=\"height: 24px; width: 40.871%;\">Only if data is small or filtered<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>When to Use <strong>ENN<\/strong><\/h2>\n<p>You should use <strong>Exact Vector Search<\/strong> when:<\/p>\n<ol>\n<li><strong>You need perfect accuracy<\/strong> \u2014 e.g., in recommendations, scientific work, legal\/financial scenarios.<\/li>\n<li><strong>You have a small dataset<\/strong> \u2014 or performance isn&#8217;t a concern.<\/li>\n<li><strong>You\u2019re using filters that return a small subset of data<\/strong> \u2014 even in big datasets!<\/li>\n<\/ol>\n<h3>Practical Example: Why ENN Can Be the Better Choice?<\/h3>\n<p class=\"\" data-start=\"160\" data-end=\"383\">A customer was working with ~300K documents across thousands of tenants (using <code data-start=\"239\" data-end=\"250\">tenant_id<\/code>). They originally used ANN vector search with a <code data-start=\"239\" data-end=\"250\">tenant_id<\/code> filter but ran into slow query performance and inconsistent accuracy.<\/p>\n<p class=\"\" data-start=\"385\" data-end=\"475\">After switching to <strong data-start=\"404\" data-end=\"436\">Exact Nearest Neighbor (ENN)<\/strong> search with the same filter, they saw:<\/p>\n<ul data-start=\"477\" data-end=\"561\">\n<li class=\"\" data-start=\"477\" data-end=\"513\">\n<p class=\"\" data-start=\"479\" data-end=\"513\"><strong data-start=\"479\" data-end=\"493\">50\u00d7 faster<\/strong> query performance<\/p>\n<\/li>\n<li class=\"\" data-start=\"514\" data-end=\"561\">\n<p class=\"\" data-start=\"516\" data-end=\"561\"><strong data-start=\"516\" data-end=\"533\">100% accuracy<\/strong> in nearest neighbor results<\/p>\n<\/li>\n<\/ul>\n<p class=\"\" data-start=\"563\" data-end=\"834\">The reason? Each tenant only had a few thousand documents, and ANN indexing isn&#8217;t as efficient on small-filtered subsets. ENN, by scanning all candidates in the filtered set, delivered better results without any index changes\u2014just by setting <code data-start=\"805\" data-end=\"820\">\"exact\": true<\/code> in the query.<\/p>\n<h2>How to Use It<\/h2>\n<p>\u200bBefore using ENN vector search in vCore-based Azure Cosmos DB for MongoDB, ensure that a <a href=\"https:\/\/learn.microsoft.com\/azure\/cosmos-db\/mongodb\/vcore\/vector-search?tabs=diskann\" target=\"_blank\" rel=\"noopener\">vector index<\/a> is created for the target field, similar to the setup for ANN searches. If you already have an existing vector index, you don\u2019t need to rebuild your vector index \u2014 just flip a switch in your query:<\/p>\n<pre><code class=\"language-json\">{\r\n  \"$search\": {\r\n    \"cosmosSearch\": {\r\n      \"path\": \"myVectorField\",\r\n      \"exact\": true,               \/\/ Enables ENN\r\n      \"query\": [0.2, 0.4, 0.9],    \/\/ Query vector\r\n      \"k\": 10,                     \/\/ Number of results to return\r\n      \"filter\": {\r\n        \"tenant_id\": { \"$eq\": \"tenant123\" }\r\n      }\r\n    }\r\n  }\r\n}<\/code><\/pre>\n<h2>Conclusion<\/h2>\n<ul>\n<li>Use <a href=\"https:\/\/aka.ms\/ennvectorsearch\" target=\"_blank\" rel=\"noopener\"><strong>ENN<\/strong><\/a>\u00a0when you need perfect accuracy, or when you&#8217;re searching in a small or filtered set of vectors.<\/li>\n<li>Use <a href=\"https:\/\/learn.microsoft.com\/azure\/cosmos-db\/mongodb\/vcore\/vector-search?tabs=diskann#perform-a-vector-search-with-diskann\" target=\"_blank\" rel=\"noopener\"><strong>ANN<\/strong><\/a>\u00a0for large-scale, fast searches where approximate matches are good enough.<\/li>\n<li>You enable ENN by setting <code>\"exact\": true<\/code> in your query. No index rebuild needed.<\/li>\n<\/ul>\n<p style=\"text-align: center;\"><div  class=\"d-flex justify-content-left\"><a class=\"cta_button_link btn-primary mb-24\" href=\"https:\/\/aka.ms\/ennvectorsearch\" target=\"_blank\">Get Started now!<\/a><\/div>\u200b<\/p>\n<h2 id=\"leave-a-review\">Leave a review<\/h2>\n<p>Tell us about your Azure Cosmos DB experience! Leave a review on PeerSpot and we\u2019ll gift you $50.\u00a0<a href=\"https:\/\/peerspotdotcom.my.site.com\/proReviews\/?SalesOpportunityProduct=00kPy000004TKXJIA4&amp;productPeerspotNumber=30881&amp;CalendlyAccount=peerspot&amp;CalendlyFormLink=peerspot-product-reviews-ps-gc-vi-sf-50&amp;giftCard=50\" target=\"_blank\" rel=\"noopener\">Get started here<\/a>.<\/p>\n<h2 id=\"about-azure-cosmos-db\">About Azure Cosmos DB<\/h2>\n<p>Azure Cosmos DB is a fully managed and serverless NoSQL and vector database for modern app development, including AI applications. With its SLA-backed speed and availability as well as instant dynamic scalability, it is ideal for real-time NoSQL and MongoDB applications that require high performance and distributed computing over massive volumes of NoSQL and vector data.<\/p>\n<p><a href=\"https:\/\/cosmos.azure.com\/try\/\" target=\"_blank\" rel=\"noopener\">Try Azure Cosmos DB for free here.<\/a>\u00a0To stay in the loop on Azure Cosmos DB updates, follow us on\u00a0<a href=\"https:\/\/twitter.com\/AzureCosmosDB\" target=\"_blank\" rel=\"noopener\">X<\/a>,\u00a0<a href=\"https:\/\/aka.ms\/AzureCosmosDBYouTube\" target=\"_blank\" rel=\"noopener\">YouTube<\/a>, and\u00a0<a href=\"https:\/\/www.linkedin.com\/company\/azure-cosmos-db\/\" target=\"_blank\" rel=\"noopener\">LinkedIn<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In applications such as personalized recommendations and scientific research, accurately identifying the closest data points to a given query is crucial. Traditional methods like Approximate Nearest Neighbor (ANN) search offer faster retrieval times by approximating results but may sacrifice accuracy, leading to potential errors in scenarios where precision is essential. This trade-off between speed and [&hellip;]<\/p>\n","protected":false},"author":125132,"featured_media":9846,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[15],"tags":[1938,1919,1868],"class_list":["post-9837","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mongodb-api","tag-enn","tag-mongodbvcore","tag-vector-search"],"acf":[],"blog_post_summary":"<p>In applications such as personalized recommendations and scientific research, accurately identifying the closest data points to a given query is crucial. Traditional methods like Approximate Nearest Neighbor (ANN) search offer faster retrieval times by approximating results but may sacrifice accuracy, leading to potential errors in scenarios where precision is essential. This trade-off between speed and [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts\/9837","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/users\/125132"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/comments?post=9837"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/posts\/9837\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/media\/9846"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/media?parent=9837"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/categories?post=9837"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cosmosdb\/wp-json\/wp\/v2\/tags?post=9837"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}