Skip to content

Commit bd7ff5a

Browse files
Daniel-Robbinsauxten
authored andcommitted
Add query similar movies to "Shawshank Redemption, The (1994)"
1 parent f347dad commit bd7ff5a

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

examples/chDB_vector_search.ipynb

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,80 @@
670670
"print(\"Movie Id, Title, Embeddings\")\n",
671671
"print(chs.query('SELECT * FROM embeddings_with_title LIMIT 5'))\n"
672672
]
673+
},
674+
{
675+
"cell_type": "code",
676+
"source": [
677+
"target_movieId = 318\n",
678+
"topN = chs.query(f\"\"\"\n",
679+
" WITH\n",
680+
" {target_movieId} AS theMovieId,\n",
681+
" (SELECT embedding FROM embeddings_with_title WHERE movieId = theMovieId LIMIT 1) AS targetEmbedding\n",
682+
" SELECT\n",
683+
" movieId,\n",
684+
" title,\n",
685+
" cosineDistance(embedding, targetEmbedding) AS distance\n",
686+
" FROM embeddings_with_title\n",
687+
" WHERE movieId != theMovieId -- Not self\n",
688+
" ORDER BY distance ASC\n",
689+
" LIMIT 10\n",
690+
" \"\"\", \"Pretty\")\n",
691+
"print(f\"Scaned {topN.rows_read()} rows, \"\n",
692+
" f\"Top 10 similar movies to movieId {target_movieId} in {topN.elapsed()}\")\n",
693+
"print(\"Target Movie:\")\n",
694+
"print(chs.query(f\"SELECT * FROM movielens.movies WHERE movieId={target_movieId}\", \"Pretty\"))\n",
695+
"print(\"Top10 Similar:\")\n",
696+
"print(topN)\n"
697+
],
698+
"metadata": {
699+
"colab": {
700+
"base_uri": "https://localhost:8080/"
701+
},
702+
"id": "wvBnbe4Vn7O4",
703+
"outputId": "825530f5-012f-438a-bb33-13ecec4ac542"
704+
},
705+
"execution_count": 37,
706+
"outputs": [
707+
{
708+
"output_type": "stream",
709+
"name": "stdout",
710+
"text": [
711+
"Scaned 10 rows, Top 10 similar movies to movieId 318 in 0.037762344\n",
712+
"Target Movie:\n",
713+
"┏━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┓\n",
714+
"\u001b[1mmovieId\u001b[0m ┃ \u001b[1mtitle \u001b[0m ┃ \u001b[1mgenres \u001b[0m ┃\n",
715+
"┡━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━┩\n",
716+
"│ 318 │ Shawshank Redemption, The (1994) │ Crime|Drama │\n",
717+
"└─────────┴──────────────────────────────────┴─────────────┘\n",
718+
"\n",
719+
"Top10 Similar:\n",
720+
"┏━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┓\n",
721+
"\u001b[1mmovieId\u001b[0m ┃ \u001b[1mtitle \u001b[0m ┃ \u001b[1m distance\u001b[0m ┃\n",
722+
"┡━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━┩\n",
723+
"│ 527 │ Schindler's List (1993) │ 0.045725048 │\n",
724+
"├─────────┼──────────────────────────────────┼─────────────┤\n",
725+
"│ 593 │ Silence of the Lambs, The (1991) │ 0.069214344 │\n",
726+
"├─────────┼──────────────────────────────────┼─────────────┤\n",
727+
"│ 50 │ Usual Suspects, The (1995) │ 0.10683352 │\n",
728+
"├─────────┼──────────────────────────────────┼─────────────┤\n",
729+
"│ 296 │ Pulp Fiction (1994) │ 0.11023432 │\n",
730+
"├─────────┼──────────────────────────────────┼─────────────┤\n",
731+
"│ 356 │ Forrest Gump (1994) │ 0.15409416 │\n",
732+
"├─────────┼──────────────────────────────────┼─────────────┤\n",
733+
"│ 858 │ Godfather, The (1972) │ 0.16235054 │\n",
734+
"├─────────┼──────────────────────────────────┼─────────────┤\n",
735+
"│ 110 │ Braveheart (1995) │ 0.17049736 │\n",
736+
"├─────────┼──────────────────────────────────┼─────────────┤\n",
737+
"│ 2028 │ Saving Private Ryan (1998) │ 0.1742953 │\n",
738+
"├─────────┼──────────────────────────────────┼─────────────┤\n",
739+
"│ 2858 │ American Beauty (1999) │ 0.19775331 │\n",
740+
"├─────────┼──────────────────────────────────┼─────────────┤\n",
741+
"│ 2959 │ Fight Club (1999) │ 0.19789273 │\n",
742+
"└─────────┴──────────────────────────────────┴─────────────┘\n",
743+
"\n"
744+
]
745+
}
746+
]
673747
}
674748
],
675749
"metadata": {

0 commit comments

Comments
 (0)