{"id":136157,"date":"2024-09-18T12:04:11","date_gmt":"2024-09-18T06:34:11","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?page_id=136157"},"modified":"2024-09-18T12:04:11","modified_gmt":"2024-09-18T06:34:11","slug":"looping-through-results-and-displaying-similarity-search","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/looping-through-results-and-displaying-similarity-search\/","title":{"rendered":"Looping Through Results and Displaying Similarity Search"},"content":{"rendered":"\n<p>Once you&#8217;ve performed a similarity search in your vector database, you&#8217;ll typically want to iterate through the results and display the most relevant items. In this guide, we&#8217;ll demonstrate how to loop through the results and display the similarity score for each item.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Example Using Chroma<\/strong><\/h2>\n\n\n\n<p>Python<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import chromadb\nfrom sentence_transformers import SentenceTransformer\n\n# Create a Chroma client and collection (as shown in the previous guide)\n\n# Perform a query\nquery_text = \"What is the capital of France?\"\nquery_embedding = embedding_model.encode(&#91;query_text])\nresults = collection.query(\n    query_embeddings=query_embedding,\n    n_results=5\n)\n\n# Loop through the results and display similarity scores\nfor result in results&#91;\"matches\"]:\n    document = result&#91;\"document\"]\n    similarity_score = result&#91;\"score\"]\n    print(f\"Document: {document}\")\n    print(f\"Similarity Score: {similarity_score}\")\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Iterate through Results:<\/strong> The <code class=\"\">results[\"matches\"]<\/code> list contains the top results from the query. We iterate through each result using a <code class=\"\">for<\/code> loop.<\/li>\n\n\n\n<li><strong>Access Document:<\/strong> The <code class=\"\">document<\/code> key in each result contains the actual document text.<\/li>\n\n\n\n<li><strong>Access Similarity Score:<\/strong> The <code class=\"\">score<\/code> key provides the similarity score between the query and the document.<\/li>\n\n\n\n<li><strong>Display Results:<\/strong> We print the document and its corresponding similarity score to the console.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Customizing Output<\/strong><\/h2>\n\n\n\n<p>You can customize the output to suit your specific needs. For example, you might want to sort the results by similarity score, format the output in a table, or highlight the most relevant keywords within the documents.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Additional Considerations<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Thresholding:<\/strong> If you want to filter out results below a certain similarity threshold, you can check the <code class=\"\">score<\/code> value and only display results that meet the criteria.<\/li>\n\n\n\n<li><strong>Pagination:<\/strong> For large result sets, you might want to implement pagination to display results in batches.<\/li>\n\n\n\n<li><strong>Visualization:<\/strong> Consider using visualization techniques to represent the similarity scores graphically, such as a bar chart or scatter plot.<\/li>\n<\/ul>\n\n\n\n<p>By following these steps and customizing the output, you can effectively loop through the results of your similarity search and present the information in a meaningful way.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Once you&#8217;ve performed a similarity search in your vector database, you&#8217;ll typically want to iterate through the results and display the most relevant items. In this guide, we&#8217;ll demonstrate how to loop through the results and display the similarity score for each item. Example Using Chroma Python Explanation Customizing Output You can customize the output&#8230;<\/p>\n","protected":false},"author":16,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-136157","page","type-page","status-publish","hentry"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Looping Through Results and Displaying Similarity Search - Tutorial<\/title>\n<meta name=\"description\" content=\"Discover how to loop through results and display similarity searches in vector databases, optimizing the retrieval and presentation of data.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vskills.in\/certification\/tutorial\/looping-through-results-and-displaying-similarity-search\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Looping Through Results and Displaying Similarity Search - Tutorial\" \/>\n<meta property=\"og:description\" content=\"Discover how to loop through results and display similarity searches in vector databases, optimizing the retrieval and presentation of data.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/looping-through-results-and-displaying-similarity-search\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vskills.in\/\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/looping-through-results-and-displaying-similarity-search\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/looping-through-results-and-displaying-similarity-search\/\",\"name\":\"Looping Through Results and Displaying Similarity Search - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"datePublished\":\"2024-09-18T06:34:11+00:00\",\"description\":\"Discover how to loop through results and display similarity searches in vector databases, optimizing the retrieval and presentation of data.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/looping-through-results-and-displaying-similarity-search\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/looping-through-results-and-displaying-similarity-search\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/looping-through-results-and-displaying-similarity-search\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Looping Through Results and Displaying Similarity Search\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"name\":\"Tutorial\",\"description\":\"Vskills - A initiative in elearning and certification\",\"publisher\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\",\"name\":\"Vskills\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"width\":73,\"height\":55,\"caption\":\"Vskills\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/vskills.in\/\",\"https:\/\/x.com\/vskills_in\",\"https:\/\/www.linkedin.com\/company-beta\/1371554\/\",\"https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Looping Through Results and Displaying Similarity Search - Tutorial","description":"Discover how to loop through results and display similarity searches in vector databases, optimizing the retrieval and presentation of data.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.vskills.in\/certification\/tutorial\/looping-through-results-and-displaying-similarity-search\/","og_locale":"en_US","og_type":"article","og_title":"Looping Through Results and Displaying Similarity Search - Tutorial","og_description":"Discover how to loop through results and display similarity searches in vector databases, optimizing the retrieval and presentation of data.","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/looping-through-results-and-displaying-similarity-search\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/looping-through-results-and-displaying-similarity-search\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/looping-through-results-and-displaying-similarity-search\/","name":"Looping Through Results and Displaying Similarity Search - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"datePublished":"2024-09-18T06:34:11+00:00","description":"Discover how to loop through results and display similarity searches in vector databases, optimizing the retrieval and presentation of data.","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/looping-through-results-and-displaying-similarity-search\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/looping-through-results-and-displaying-similarity-search\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/looping-through-results-and-displaying-similarity-search\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Looping Through Results and Displaying Similarity Search"}]},{"@type":"WebSite","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","name":"Tutorial","description":"Vskills - A initiative in elearning and certification","publisher":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization","name":"Vskills","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","contentUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","width":73,"height":55,"caption":"Vskills"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/vskills.in\/","https:\/\/x.com\/vskills_in","https:\/\/www.linkedin.com\/company-beta\/1371554\/","https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw"]}]}},"_links":{"self":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/136157","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/comments?post=136157"}],"version-history":[{"count":1,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/136157\/revisions"}],"predecessor-version":[{"id":136167,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/136157\/revisions\/136167"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=136157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=136157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=136157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}