1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
|
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
LOAD 'age';
SET search_path=ag_catalog;
SELECT create_graph('graph');
NOTICE: graph "graph" has been created
create_graph
--------------
(1 row)
-- Should error out
SELECT * FROM cypher('graph', $$ RETURN cosine_distance("[1,2,3]", "[1,2,3]") $$) AS (n agtype);
ERROR: function cosine_distance does not exist
LINE 1: SELECT * FROM cypher('graph', $$ RETURN cosine_distance("[1,...
^
HINT: If the function is from an external extension, make sure the extension is installed and the function is in the search path.
-- Create the extension in the public schema
CREATE EXTENSION vector SCHEMA public;
-- Should error out
SELECT * FROM cypher('graph', $$ RETURN cosine_distance("[1,2,3]", "[1,2,3]") $$) AS (n agtype);
ERROR: function cosine_distance does not exist
LINE 1: SELECT * FROM cypher('graph', $$ RETURN cosine_distance("[1,...
^
HINT: If the function is from an external extension, make sure the extension is installed and the function is in the search path.
-- Should work
SET search_path=ag_catalog, public;
SELECT create_graph('graph');
ERROR: graph "graph" already exists
SELECT * FROM cypher('graph', $$ RETURN "[1.22,2.22,3.33]"::vector $$) AS (n vector);
n
------------------
[1.22,2.22,3.33]
(1 row)
SELECT * FROM cypher('graph', $$ RETURN "[1.22,2.22,3.33]"::vector $$) AS (n halfvec);
n
---------------------------------
[1.2197266,2.2207031,3.3300781]
(1 row)
SELECT * FROM cypher('graph', $$ RETURN "[1.22,2.22,3.33]"::vector $$) AS (n sparsevec);
n
--------------------------
{1:1.22,2:2.22,3:3.33}/3
(1 row)
SELECT * FROM cypher('graph', $$ RETURN [1.22,2.22,3.33]::vector $$) AS (n vector);
n
------------------
[1.22,2.22,3.33]
(1 row)
SELECT * FROM cypher('graph', $$ RETURN [1.22,2.22,3.33]::vector $$) AS (n halfvec);
n
---------------------------------
[1.2197266,2.2207031,3.3300781]
(1 row)
SELECT * FROM cypher('graph', $$ RETURN [1.22,2.22,3.33]::vector $$) AS (n sparsevec);
n
--------------------------
{1:1.22,2:2.22,3:3.33}/3
(1 row)
SELECT * FROM cypher('graph', $$ RETURN [1.22,2.22,3.33]::vector $$) AS (n vector(3));
n
------------------
[1.22,2.22,3.33]
(1 row)
SELECT * FROM cypher('graph', $$ RETURN [1.22,2.22,3.33]::vector $$) AS (n halfvec(3));
n
---------------------------------
[1.2197266,2.2207031,3.3300781]
(1 row)
SELECT * FROM cypher('graph', $$ RETURN [1.22,2.22,3.33]::vector $$) AS (n sparsevec(3));
n
--------------------------
{1:1.22,2:2.22,3:3.33}/3
(1 row)
-- Should error out
SELECT * FROM cypher('graph', $$ RETURN [1.22,2.22,3.33]::vector $$) AS (n vector(2));
ERROR: expected 2 dimensions, not 3
SELECT * FROM cypher('graph', $$ RETURN [1.22,2.22,3.33]::vector $$) AS (n halfvec(2));
ERROR: expected 2 dimensions, not 3
SELECT * FROM cypher('graph', $$ RETURN [1.22,2.22,3.33]::vector $$) AS (n sparsevec(2));
ERROR: expected 2 dimensions, not 3
SELECT * FROM cypher('graph', $$ RETURN [1.22,2.22,3.33]::vector(3) $$) AS (n vector(4));
ERROR: expected 4 dimensions, not 3
SELECT * FROM cypher('graph', $$ RETURN [1.22,2.22,3.33]::vector(3) $$) AS (n halfvec(4));
ERROR: expected 4 dimensions, not 3
SELECT * FROM cypher('graph', $$ RETURN [1.22,2.22,3.33]::vector(3) $$) AS (n sparsevec(4));
ERROR: expected 4 dimensions, not 3
--
-- Test functions
--
SELECT * FROM cypher('graph', $$ RETURN l2_distance("[1,2,3]", "[1,2,4]") $$) AS (n agtype);
n
-----
1.0
(1 row)
SELECT * FROM cypher('graph', $$ RETURN inner_product("[1,2,3]", "[1,2,4]") $$) AS (n agtype);
n
------
17.0
(1 row)
SELECT * FROM cypher('graph', $$ RETURN cosine_distance("[1,2,3]", "[1,2,4]") $$) AS (n agtype);
n
---------------------
0.00853986601633272
(1 row)
SELECT * FROM cypher('graph', $$ RETURN l1_distance("[1,2,3]", "[1,2,4]") $$) AS (n agtype);
n
-----
1.0
(1 row)
SELECT * FROM cypher('graph', $$ RETURN vector_dims("[1,2,3]") $$) AS (n agtype);
n
---
3
(1 row)
SELECT * FROM cypher('graph', $$ RETURN vector_norm("[1,2,3]") $$) AS (n agtype);
n
--------------------
3.7416573867739413
(1 row)
SELECT * FROM cypher('graph', $$ RETURN l2_normalize("[1,2,3]") $$) AS (n vector);
n
-----------------------------------
[0.26726124,0.5345225,0.80178374]
(1 row)
SELECT * FROM cypher('graph', $$ RETURN l2_normalize("[1,2,3]")::text $$) AS (n agtype);
n
-------------------------------------
[0.26726124, 0.5345225, 0.80178374]
(1 row)
SELECT * FROM cypher('graph', $$ RETURN subvector("[1,2,3,4,5,6]", 2, 4) $$) AS (n vector);
n
-----------
[2,3,4,5]
(1 row)
SELECT * FROM cypher('graph', $$ RETURN subvector("[1,2,3,4,5,6]", 2, 4)::text $$) AS (n agtype);
n
--------------
[2, 3, 4, 5]
(1 row)
SELECT * FROM cypher('graph', $$ RETURN binary_quantize("[1,2,4]") $$) AS (n bit(3));
n
-----
111
(1 row)
--
-- Test operators
--
SELECT * FROM cypher('graph', $$ RETURN [1,2,3]::vector + [1,2,4]::vector $$) AS (n vector);
n
---------
[2,4,7]
(1 row)
SELECT * FROM cypher('graph', $$ RETURN [1,2,3]::vector - [1,2,4]::vector $$) AS (n vector);
n
----------
[0,0,-1]
(1 row)
SELECT * FROM cypher('graph', $$ RETURN [1,2,3]::vector * [1,2,4]::vector $$) AS (n vector);
n
----------
[1,4,12]
(1 row)
SELECT * FROM cypher('graph', $$ RETURN [1,2,3]::vector || [1,2,4]::vector $$) AS (n vector);
n
---------------
[1,2,3,1,2,4]
(1 row)
SELECT * FROM cypher('graph', $$ RETURN [1,2,3]::vector <#> [1,2,4]::vector $$) AS (n agtype);
n
-------
-17.0
(1 row)
SELECT * FROM cypher('graph', $$ RETURN [1,2,3]::vector <=> [1,2,4]::vector $$) AS (n agtype);
n
---------------------
0.00853986601633272
(1 row)
SELECT * FROM cypher('graph', $$ RETURN [1,2,3]::vector <+> [1,2,4]::vector $$) AS (n agtype);
n
-----
1.0
(1 row)
--
-- Due to issues with pattern matching syntax, '-' is not allowed
-- as an operator character, so we have to use the OPERATOR syntax.
--
SELECT * FROM cypher('graph', $$ RETURN [1,2,3]::vector OPERATOR (`<->`) [1,2,4]::vector $$) AS (n agtype);
n
-----
1.0
(1 row)
-- Using OPERATOR () syntax
SELECT * FROM cypher('graph', $$ RETURN [1,2,3]::vector OPERATOR (+) [1,2,4]::vector $$) AS (n vector);
n
---------
[2,4,7]
(1 row)
SELECT * FROM cypher('graph', $$ RETURN [1,2,3]::vector OPERATOR (-) [1,2,4]::vector $$) AS (n vector);
n
----------
[0,0,-1]
(1 row)
SELECT * FROM cypher('graph', $$ RETURN [1,2,3]::vector OPERATOR (*) [1,2,4]::vector $$) AS (n vector);
n
----------
[1,4,12]
(1 row)
SELECT * FROM cypher('graph', $$ RETURN [1,2,3]::vector OPERATOR (||) [1,2,4]::vector $$) AS (n vector);
n
---------------
[1,2,3,1,2,4]
(1 row)
SELECT * FROM cypher('graph', $$ RETURN [1,2,3]::vector OPERATOR (`<->`) [1,2,4]::vector $$) AS (n agtype);
n
-----
1.0
(1 row)
SELECT * FROM cypher('graph', $$ RETURN [1,2,3]::vector OPERATOR (<#>) [1,2,4]::vector $$) AS (n agtype);
n
-------
-17.0
(1 row)
SELECT * FROM cypher('graph', $$ RETURN [1,2,3]::vector OPERATOR (<=>) [1,2,4]::vector $$) AS (n agtype);
n
---------------------
0.00853986601633272
(1 row)
SELECT * FROM cypher('graph', $$ RETURN [1,2,3]::vector OPERATOR (<+>) [1,2,4]::vector $$) AS (n agtype);
n
-----
1.0
(1 row)
SELECT * FROM cypher('graph', $$ RETURN [1,2,3]::vector OPERATOR (public.+) [1,2,4]::vector $$) AS (n vector);
n
---------
[2,4,7]
(1 row)
SELECT * FROM cypher('graph', $$ RETURN [1,2,3]::vector OPERATOR (public.-) [1,2,4]::vector $$) AS (n vector);
n
----------
[0,0,-1]
(1 row)
SELECT * FROM cypher('graph', $$ RETURN [1,2,3]::vector OPERATOR (public.*) [1,2,4]::vector $$) AS (n vector);
n
----------
[1,4,12]
(1 row)
SELECT * FROM cypher('graph', $$ RETURN [1,2,3]::vector OPERATOR (public.||) [1,2,4]::vector $$) AS (n vector);
n
---------------
[1,2,3,1,2,4]
(1 row)
SELECT * FROM cypher('graph', $$ RETURN [1,2,3]::vector OPERATOR (public.`<->`) [1,2,4]::vector $$) AS (n agtype);
n
-----
1.0
(1 row)
SELECT * FROM cypher('graph', $$ RETURN [1,2,3]::vector OPERATOR (public.<#>) [1,2,4]::vector $$) AS (n agtype);
n
-------
-17.0
(1 row)
SELECT * FROM cypher('graph', $$ RETURN [1,2,3]::vector OPERATOR (public.<=>) [1,2,4]::vector $$) AS (n agtype);
n
---------------------
0.00853986601633272
(1 row)
SELECT * FROM cypher('graph', $$ RETURN [1,2,3]::vector OPERATOR (public.<+>) [1,2,4]::vector $$) AS (n agtype);
n
-----
1.0
(1 row)
--
-- An example usage
--
SELECT * FROM cypher('graph', $$
CREATE (:Movie {title: "The Matrix", year: 1999, genre: "Action", plot: "A computer hacker learns about the true nature of reality and joins a rebellion to free humanity from a simulated world controlled by machines.", embedding: [-0.07594558, 0.04081754, 0.29592122, -0.11921061]}),
(:Movie {title: "The Matrix Reloaded", year: 2003, genre: "Action", plot: "The rebels continue their fight against the machines, uncovering deeper truths about the Matrix and the nature of their mission.", embedding: [0.30228977, -0.22839354, 0.35070436, 0.01262819]}),
(:Movie {title: "The Matrix Revolutions", year: 2003, genre: "Action", plot: "The final battle between humans and machines reaches its climax as the fate of both worlds hangs in the balance.", embedding: [ 0.12240622, -0.29752459, 0.22620453, 0.24454723]}),
(:Movie {title: "The Matrix Resurrections", year: 2021, genre: "Action", plot: "Neo returns to a new version of the Matrix and must once again fight to save the people from the control of the machines.", embedding: [ 0.34717246, -0.13820869, 0.29214213, 0.08090488]}),
(:Movie {title: "Inception", year: 2010, genre: "Sci-Fi", plot: "A skilled thief is given a chance at redemption if he can successfully perform an inception: planting an idea into someone’s subconscious.", embedding: [ 0.03923657, 0.39284106, -0.20927092, -0.17770818]}),
(:Movie {title: "Interstellar", year: 2014, genre: "Sci-Fi", plot: "A group of explorers travel through a wormhole in space in an attempt to ensure humanity’s survival.", embedding: [-0.29302418, -0.39615033, -0.23393948, -0.09601383]}),
(:Movie {title: "Avatar", year: 2009, genre: "Sci-Fi", plot: "A paraplegic Marine is sent to the moon Pandora, where he becomes torn between following orders and protecting the world he feels is his home.", embedding: [-0.13663386, 0.00635589, -0.03038832, -0.08252723]}),
(:Movie {title: "Blade Runner", year: 1982, genre: "Sci-Fi", plot: "A blade runner must pursue and terminate four replicants who have stolen a ship in space and returned to Earth.", embedding: [ 0.27215557, -0.1479577, -0.09972772, -0.08234394]}),
(:Movie {title: "Blade Runner 2049", year: 2017, genre: "Sci-Fi", plot: "A new blade runner unearths a long-buried secret that has the potential to plunge what’s left of society into chaos.", embedding: [ 0.21560573, -0.07505179, -0.01331814, 0.13403069]}),
(:Movie {title: "Minority Report", year: 2002, genre: "Sci-Fi", plot: "In a future where a special police unit can arrest murderers before they commit their crimes, a top officer is accused of a future murder.", embedding: [ 0.24008012, 0.44954908, -0.30905488, 0.15195407]}),
(:Movie {title: "Total Recall", year: 1990, genre: "Sci-Fi", plot: "A construction worker discovers that his memories have been implanted and becomes embroiled in a conspiracy on Mars.", embedding: [-0.17471036, 0.14695261, -0.06272433, -0.21795064]}),
(:Movie {title: "Elysium", year: 2013, genre: "Sci-Fi", plot: "In a future where the rich live on a luxurious space station while the rest of humanity lives in squalor, a man fights to bring equality.", embedding: [-0.33280967, 0.07733926, 0.11015328, 0.53382836]}),
(:Movie {title: "Gattaca", year: 1997, genre: "Sci-Fi", plot: "In a future where genetic engineering determines social class, a man defies his fate to achieve his dreams.", embedding: [-0.21629286, 0.31114665, 0.08303899, 0.46199759]}),
(:Movie {title: "The Fifth Element", year: 1997, genre: "Sci-Fi", plot: "In a futuristic world, a cab driver becomes the key to saving humanity from an impending cosmic threat.", embedding: [-0.11528205, -0.0208782, -0.0735215, 0.14327449]}),
(:Movie {title: "The Terminator", year: 1984, genre: "Action", plot: "A cyborg assassin is sent back in time to kill the mother of the future resistance leader.", embedding: [ 0.33666933, 0.18040994, -0.01075103, -0.11117851]}),
(:Movie {title: "Terminator 2: Judgment Day", year: 1991, genre: "Action", plot: "A reprogrammed Terminator is sent to protect the future leader of the human resistance from a more advanced Terminator.", embedding: [ 0.34698868, 0.06439331, 0.06232323, -0.19534876]}),
(:Movie {title: "Jurassic Park", year: 1993, genre: "Adventure", plot: "Scientists clone dinosaurs to create a theme park, but things go awry when the creatures escape.", embedding: [ 0.01794725, -0.11434246, -0.46831815, -0.01049593]}),
(:Movie {title: "The Avengers", year: 2012, genre: "Action", plot: "Superheroes assemble to face a global threat from an alien invasion led by Loki.", embedding: [ 0.00546514, -0.37005171, -0.42612838, 0.07968612]})
$$) AS (result agtype);
result
--------
(0 rows)
SELECT * FROM cypher('graph', $$ MATCH (m:Movie) RETURN m.title, (m.embedding)::vector $$) AS (title agtype, embedding vector);
title | embedding
------------------------------+---------------------------------------------------
"The Matrix" | [-0.07594558,0.04081754,0.2959212,-0.11921061]
"The Matrix Reloaded" | [0.30228978,-0.22839354,0.35070437,0.01262819]
"The Matrix Revolutions" | [0.12240622,-0.2975246,0.22620453,0.24454723]
"The Matrix Resurrections" | [0.34717247,-0.13820869,0.29214212,0.08090488]
"Inception" | [0.03923657,0.39284107,-0.20927092,-0.17770818]
"Interstellar" | [-0.29302418,-0.39615032,-0.23393948,-0.09601383]
"Avatar" | [-0.13663386,0.00635589,-0.03038832,-0.08252723]
"Blade Runner" | [0.27215558,-0.1479577,-0.09972772,-0.08234394]
"Blade Runner 2049" | [0.21560574,-0.07505179,-0.01331814,0.13403068]
"Minority Report" | [0.24008012,0.44954908,-0.30905488,0.15195407]
"Total Recall" | [-0.17471036,0.14695261,-0.06272433,-0.21795064]
"Elysium" | [-0.33280966,0.07733926,0.11015328,0.5338284]
"Gattaca" | [-0.21629286,0.31114665,0.08303899,0.4619976]
"The Fifth Element" | [-0.11528205,-0.0208782,-0.0735215,0.14327449]
"The Terminator" | [0.33666933,0.18040994,-0.01075103,-0.11117851]
"Terminator 2: Judgment Day" | [0.34698868,0.06439331,0.06232323,-0.19534875]
"Jurassic Park" | [0.01794725,-0.11434246,-0.46831816,-0.01049593]
"The Avengers" | [0.00546514,-0.3700517,-0.4261284,0.07968612]
(18 rows)
-- Check the dimension of the embedding
SELECT * FROM cypher('graph', $$ MATCH (m:Movie) RETURN m.title, vector_dims(m.embedding) $$) AS (title agtype, dimension int);
title | dimension
------------------------------+-----------
"The Matrix" | 4
"The Matrix Reloaded" | 4
"The Matrix Revolutions" | 4
"The Matrix Resurrections" | 4
"Inception" | 4
"Interstellar" | 4
"Avatar" | 4
"Blade Runner" | 4
"Blade Runner 2049" | 4
"Minority Report" | 4
"Total Recall" | 4
"Elysium" | 4
"Gattaca" | 4
"The Fifth Element" | 4
"The Terminator" | 4
"Terminator 2: Judgment Day" | 4
"Jurassic Park" | 4
"The Avengers" | 4
(18 rows)
-- Get top 4 most similar movies to The Terminator using cosine distance
SELECT * FROM cypher('graph', $$ MATCH (m:Movie), (search:Movie {title: "The Terminator"})
RETURN m.title ORDER BY cosine_distance(m.embedding, search.embedding)
ASC LIMIT 4
$$) AS (title agtype);
title
------------------------------
"The Terminator"
"Terminator 2: Judgment Day"
"Minority Report"
"Blade Runner"
(4 rows)
SELECT * FROM cypher('graph', $$ MATCH (m:Movie), (search:Movie {title: "The Terminator"})
RETURN m.title ORDER BY m.embedding::vector <=> search.embedding::vector
ASC LIMIT 4
$$) AS (title agtype);
title
------------------------------
"The Terminator"
"Terminator 2: Judgment Day"
"Minority Report"
"Blade Runner"
(4 rows)
-- Get top 4 most similar movies to The Matrix using cosine distance
SELECT * FROM cypher('graph', $$ MATCH (m:Movie), (search:Movie {title: "The Matrix"})
RETURN m.title ORDER BY cosine_distance(m.embedding, search.embedding)
ASC LIMIT 4
$$) AS (title agtype);
title
----------------------------
"The Matrix"
"The Matrix Reloaded"
"The Matrix Resurrections"
"Total Recall"
(4 rows)
SELECT * FROM cypher('graph', $$ MATCH (m:Movie), (search:Movie {title: "The Matrix"})
RETURN m.title ORDER BY m.embedding::vector <=> search.embedding::vector
ASC LIMIT 4
$$) AS (title agtype);
title
----------------------------
"The Matrix"
"The Matrix Reloaded"
"The Matrix Resurrections"
"Total Recall"
(4 rows)
-- l2 norm of the embedding
SELECT * FROM cypher('graph', $$ MATCH (m:Movie) set m.embedding=l2_normalize(m.embedding)::agtype return m.title, m.embedding $$) AS (title agtype, embedding agtype);
title | embedding
------------------------------+-----------------------------------------------------
"The Matrix" | [-0.22980669, 0.12351139, 0.89543957, -0.36072403]
"The Matrix Reloaded" | [0.58534974, -0.44225806, 0.6790991, 0.024453051]
"The Matrix Revolutions" | [0.26431033, -0.6424414, 0.4884408, 0.528048]
"The Matrix Resurrections" | [0.72151977, -0.28723562, 0.60715157, 0.16814256]
"Inception" | [0.08159459, 0.81693435, -0.43519026, -0.3695538]
"Interstellar" | [-0.5290723, -0.71527255, -0.4223914, -0.17335857]
"Avatar" | [-0.84023285, 0.039085682, -0.18687363, -0.507503]
"Blade Runner" | [0.81074023, -0.44075987, -0.29708475, -0.2452992]
"Blade Runner 2049" | [0.8134027, -0.28314334, -0.05024454, 0.50564945]
"Minority Report" | [0.39031598, 0.7308651, -0.5024533, 0.24704295]
"Total Recall" | [-0.54291505, 0.4566574, -0.19491677, -0.67728484]
"Elysium" | [-0.517338, 0.12022049, 0.17122844, 0.82981277]
"Gattaca" | [-0.35853538, 0.51576865, 0.13764863, 0.765825]
"The Fifth Element" | [-0.5788842, -0.10483904, -0.36918527, 0.7194471]
"The Terminator" | [0.84599304, 0.45333964, -0.02701552, -0.27937278]
"Terminator 2: Judgment Day" | [0.8501332, 0.15776564, 0.15269388, -0.4786106]
"Jurassic Park" | [0.037194606, -0.23696794, -0.9705615, -0.02175219]
"The Avengers" | [0.009587915, -0.6492101, -0.7475897, 0.13979948]
(18 rows)
-- Get top 4 most similar movies to The Terminator using l2 distance
SELECT * FROM cypher('graph', $$ MATCH (m:Movie), (search:Movie {title: "The Terminator"})
RETURN m.title ORDER BY l2_distance(m.embedding, search.embedding) ASC LIMIT 4
$$) AS (title agtype);
title
------------------------------
"The Terminator"
"Terminator 2: Judgment Day"
"Minority Report"
"Blade Runner"
(4 rows)
SELECT * FROM cypher('graph', $$ MATCH (m:Movie), (search:Movie {title: "The Terminator"})
RETURN m.title ORDER BY m.embedding::vector OPERATOR (`<->`) search.embedding::vector
ASC LIMIT 4
$$) AS (title agtype);
title
------------------------------
"The Terminator"
"Terminator 2: Judgment Day"
"Minority Report"
"Blade Runner"
(4 rows)
-- Get top 4 most similar movies to The Matrix using l2 distance
SELECT * FROM cypher('graph', $$ MATCH (m:Movie), (search:Movie {title: "The Matrix"})
RETURN m.title ORDER BY l2_distance(m.embedding, search.embedding) ASC LIMIT 4
$$) AS (title agtype);
title
----------------------------
"The Matrix"
"The Matrix Reloaded"
"The Matrix Resurrections"
"Total Recall"
(4 rows)
SELECT * FROM cypher('graph', $$ MATCH (m:Movie), (search:Movie {title: "The Matrix"})
RETURN m.title ORDER BY m.embedding::vector OPERATOR (`<->`) search.embedding::vector
ASC LIMIT 4
$$) AS (title agtype);
title
----------------------------
"The Matrix"
"The Matrix Reloaded"
"The Matrix Resurrections"
"Total Recall"
(4 rows)
--
-- Test vector index
--
-- This function will be used to check if index scan
-- is used successfully. We cannot simply have EXPLAIN
-- in the upcoming queries because it produces some
-- hardcoded oids in sort node, which may change in
-- future and break the tests.
CREATE OR REPLACE FUNCTION plan_has_index_scan(sql text)
RETURNS boolean
LANGUAGE plpgsql AS
$$
DECLARE
plan_lines text[];
plan_text text;
BEGIN
EXECUTE format('EXPLAIN (FORMAT JSON, COSTS OFF) %s', sql) INTO plan_text;
-- Return true if 'Index Scan' appears anywhere
RETURN position('"Index Scan"' in plan_text) > 0;
END;
$$;
SELECT * FROM cypher('graph', $$ MATCH (m:Movie), (search:Movie {title: "The Matrix"})
RETURN m.title ORDER BY m.embedding::vector(4) <=> search.embedding::vector(4)
ASC LIMIT 4
$$) AS (title agtype);
title
----------------------------
"The Matrix"
"The Matrix Reloaded"
"The Matrix Resurrections"
"Total Recall"
(4 rows)
-- The index expression below matches the expression
-- seen in the EXPLAIN plan of above query
DO $$
DECLARE
graph_oid oid;
BEGIN
SELECT graphid INTO graph_oid
FROM ag_catalog.ag_graph
WHERE name = 'graph';
EXECUTE format($f$
CREATE INDEX movie_vector_idx ON graph."Movie"
USING hnsw (((
agtype_access_operator(
VARIADIC ARRAY[
_agtype_build_vertex(id, _label_name(%L::oid, id), properties),
'"embedding"'::agtype
]
)::text
)::vector(4)) vector_cosine_ops);
$f$, graph_oid);
END;
$$;
-- Disable seqscan just to test the index
SET enable_seqscan = off;
SELECT plan_has_index_scan($f$
SELECT * FROM cypher('graph', $$
MATCH (m:Movie)
RETURN m.title
ORDER BY m.embedding::vector(4) <=> [-0.07594558, 0.04081754, 0.29592122, -0.11921061]::vector(4)
ASC LIMIT 4
$$) AS (title agtype);
$f$);
plan_has_index_scan
---------------------
t
(1 row)
SELECT * FROM cypher('graph', $$ MATCH (m:Movie)
RETURN m.title
ORDER BY m.embedding::vector(4) <=> [-0.07594558, 0.04081754, 0.29592122, -0.11921061]::vector(4)
ASC LIMIT 4
$$) AS (title agtype);
title
----------------------------
"The Matrix"
"The Matrix Reloaded"
"The Matrix Resurrections"
"Total Recall"
(4 rows)
DROP INDEX graph.movie_vector_idx;
SET enable_seqscan = on;
-- Test a direct implicit cast
CREATE CAST (agtype AS vector)
WITH INOUT AS implicit;
SELECT * FROM cypher('graph', $$ MATCH (m:Movie), (search:Movie {title: "The Matrix"})
RETURN m.title ORDER BY m.embedding <=> search.embedding
ASC LIMIT 4
$$) AS (title agtype);
title
----------------------------
"The Matrix"
"The Matrix Reloaded"
"The Matrix Resurrections"
"Total Recall"
(4 rows)
SELECT * FROM cypher('graph', $$ MATCH (m:Movie), (search:Movie {title: "The Matrix"})
RETURN m.title ORDER BY m.embedding OPERATOR (`<->`) search.embedding
ASC LIMIT 4
$$) AS (title agtype);
title
----------------------------
"The Matrix"
"The Matrix Reloaded"
"The Matrix Resurrections"
"Total Recall"
(4 rows)
DO $$
DECLARE
graph_oid oid;
BEGIN
SELECT graphid INTO graph_oid
FROM ag_catalog.ag_graph
WHERE name = 'graph';
EXECUTE format($f$
CREATE INDEX movie_vector_idx ON graph."Movie"
USING hnsw ((
agtype_access_operator(
VARIADIC ARRAY[
_agtype_build_vertex(id, _label_name(%L::oid, id), properties),
'"embedding"'::agtype
]
)::vector(4)) vector_cosine_ops);
$f$, graph_oid);
END;
$$;
-- Disable seqscan just to test the index
SET enable_seqscan = off;
SELECT plan_has_index_scan($f$
SELECT * FROM cypher('graph', $$
MATCH (m:Movie)
RETURN m.title
ORDER BY m.embedding::vector(4) <=> [-0.07594558, 0.04081754, 0.29592122, -0.11921061]::vector(4)
ASC LIMIT 4
$$) AS (title agtype);
$f$);
plan_has_index_scan
---------------------
t
(1 row)
SELECT * FROM cypher('graph', $$ MATCH (m:Movie)
RETURN m.title
ORDER BY m.embedding::vector(4) <=> [-0.07594558, 0.04081754, 0.29592122, -0.11921061]::vector(4)
ASC LIMIT 4
$$) AS (title agtype);
title
----------------------------
"The Matrix"
"The Matrix Reloaded"
"The Matrix Resurrections"
"Total Recall"
(4 rows)
SET enable_seqscan = on;
--
-- Clean up
--
DROP FUNCTION plan_has_index_scan(text);
DROP CAST (agtype AS vector);
SELECT drop_graph('graph', true);
NOTICE: drop cascades to 3 other objects
DETAIL: drop cascades to table graph._ag_label_vertex
drop cascades to table graph._ag_label_edge
drop cascades to table graph."Movie"
NOTICE: graph "graph" has been dropped
drop_graph
------------
(1 row)
DROP EXTENSION vector CASCADE;
|