|
20 | 20 | "```json\n",
|
21 | 21 | " {\n",
|
22 | 22 | " \"product\": \"Samsung 49-inch Curved Gaming Monitor\",\n",
|
23 |
| - " \"description\": \"is a curved gaming monitor with a high refresh rate and AMD FreeSync technology. It offers an immersive gaming experience with smooth visuals.\",\n", |
| 23 | + " \"description\": \"is a curved gaming monitor with a high refresh rate and AMD FreeSync technology.\",\n", |
24 | 24 | " \"category\": \"Monitors\"\n",
|
25 | 25 | "}\n",
|
26 | 26 | "\n",
|
|
223 | 223 | },
|
224 | 224 | {
|
225 | 225 | "cell_type": "code",
|
226 |
| - "execution_count": 56, |
| 226 | + "execution_count": 78, |
227 | 227 | "id": "6739f55b-6983-4b48-9349-6e0111b313fe",
|
228 | 228 | "metadata": {
|
229 | 229 | "id": "6739f55b-6983-4b48-9349-6e0111b313fe"
|
|
290 | 290 | },
|
291 | 291 | {
|
292 | 292 | "cell_type": "code",
|
293 |
| - "execution_count": null, |
| 293 | + "execution_count": 82, |
294 | 294 | "id": "9b53b39e-d74e-4fa8-a364-e2c3caf37418",
|
295 | 295 | "metadata": {
|
296 | 296 | "id": "9b53b39e-d74e-4fa8-a364-e2c3caf37418"
|
|
330 | 330 | " \"inference_id\": e5_endpoint,\n",
|
331 | 331 | " },\n",
|
332 | 332 | " \"elser_semantic_description_vector\": {\n",
|
333 |
| - " \"type\": \"semantic_text\",\n", |
334 |
| - " \"inference_id\": elser_endpoint,\n", |
| 333 | + " \"type\": \"semantic_text\"\n", |
| 334 | + " # \"inference_id\": elser_endpoint\n", |
335 | 335 | " },\n",
|
336 | 336 | " }\n",
|
337 | 337 | "}\n",
|
|
358 | 358 | },
|
359 | 359 | {
|
360 | 360 | "cell_type": "code",
|
361 |
| - "execution_count": 57, |
| 361 | + "execution_count": 83, |
362 | 362 | "id": "c4830b74",
|
363 | 363 | "metadata": {},
|
364 | 364 | "outputs": [
|
|
392 | 392 | },
|
393 | 393 | {
|
394 | 394 | "cell_type": "code",
|
395 |
| - "execution_count": 58, |
| 395 | + "execution_count": null, |
396 | 396 | "id": "3cfdc3b7-7e4f-4111-997b-c333ac8938ba",
|
397 | 397 | "metadata": {
|
398 | 398 | "id": "3cfdc3b7-7e4f-4111-997b-c333ac8938ba"
|
|
415 | 415 | "\n",
|
416 | 416 | "# helper function to create bulk indexing body\n",
|
417 | 417 | "def create_index_body(doc):\n",
|
| 418 | + " doc[\"elser_semantic_description_vector\"] = doc[\"description\"]\n", |
| 419 | + " doc[\"e5_semantic_description_vector\"] = doc[\"description\"]\n", |
| 420 | + "\n", |
418 | 421 | " return {\n",
|
419 | 422 | " \"_index\": \"ecommerce-search\",\n",
|
420 | 423 | " \"_source\": doc,\n",
|
|
528 | 531 | },
|
529 | 532 | {
|
530 | 533 | "cell_type": "code",
|
531 |
| - "execution_count": null, |
| 534 | + "execution_count": 64, |
532 | 535 | "id": "d44f3e2b",
|
533 | 536 | "metadata": {},
|
534 |
| - "outputs": [], |
| 537 | + "outputs": [ |
| 538 | + { |
| 539 | + "name": "stdout", |
| 540 | + "output_type": "stream", |
| 541 | + "text": [ |
| 542 | + "Custom Tokens: ['comfortable', 'furniture', 'for', 'a', 'large', 'balcony']\n" |
| 543 | + ] |
| 544 | + }, |
| 545 | + { |
| 546 | + "data": { |
| 547 | + "text/plain": [ |
| 548 | + "ObjectApiResponse({'acknowledged': True})" |
| 549 | + ] |
| 550 | + }, |
| 551 | + "execution_count": 64, |
| 552 | + "metadata": {}, |
| 553 | + "output_type": "execute_result" |
| 554 | + } |
| 555 | + ], |
535 | 556 | "source": [
|
536 | 557 | "index_settings = {\n",
|
537 | 558 | " \"settings\": {\n",
|
|
624 | 645 | },
|
625 | 646 | {
|
626 | 647 | "cell_type": "code",
|
627 |
| - "execution_count": null, |
| 648 | + "execution_count": 65, |
628 | 649 | "id": "f4984f6c-ceec-46a4-b64c-f749e6b1b04f",
|
629 | 650 | "metadata": {
|
630 | 651 | "id": "f4984f6c-ceec-46a4-b64c-f749e6b1b04f"
|
631 | 652 | },
|
632 |
| - "outputs": [], |
| 653 | + "outputs": [ |
| 654 | + { |
| 655 | + "name": "stdout", |
| 656 | + "output_type": "stream", |
| 657 | + "text": [ |
| 658 | + "\n", |
| 659 | + "Score: 12.93728\n", |
| 660 | + "Product: Barbie Dreamhouse\n", |
| 661 | + "Category: Toys\n", |
| 662 | + "Description: is a classic Barbie playset with multiple rooms, furniture, a large balcony, a pool, and accessories. It allows kids to create their dream Barbie world.\n", |
| 663 | + "\n", |
| 664 | + "\n", |
| 665 | + "Score: 7.9097595\n", |
| 666 | + "Product: Rattan Patio Conversation Set\n", |
| 667 | + "Category: Outdoor Furniture\n", |
| 668 | + "Description: is a stylish and comfortable outdoor furniture set, including a sofa, two chairs, and a coffee table, all made of durable rattan material.\n", |
| 669 | + "\n" |
| 670 | + ] |
| 671 | + } |
| 672 | + ], |
633 | 673 | "source": [
|
634 | 674 | "results_list = []\n",
|
635 | 675 | "\n",
|
|
680 | 720 | },
|
681 | 721 | {
|
682 | 722 | "cell_type": "code",
|
683 |
| - "execution_count": null, |
| 723 | + "execution_count": 66, |
684 | 724 | "id": "72187c9a-14c1-4084-a080-4e5c1e614f22",
|
685 | 725 | "metadata": {
|
686 | 726 | "id": "72187c9a-14c1-4084-a080-4e5c1e614f22"
|
687 | 727 | },
|
688 |
| - "outputs": [], |
| 728 | + "outputs": [ |
| 729 | + { |
| 730 | + "name": "stdout", |
| 731 | + "output_type": "stream", |
| 732 | + "text": [ |
| 733 | + "\n", |
| 734 | + "Score: 0.93147576\n", |
| 735 | + "Product: Metal Garden Bench with Cushion\n", |
| 736 | + "Category: Garden Furniture\n", |
| 737 | + "Description: is a stylish and comfortable metal garden bench, complete with a cushion for added support.\n", |
| 738 | + "\n", |
| 739 | + "\n", |
| 740 | + "Score: 0.9304026\n", |
| 741 | + "Product: Garden Dining Set with Swivel Chairs\n", |
| 742 | + "Category: Garden Furniture\n", |
| 743 | + "Description: is a functional and comfortable garden dining set, including a table and chairs with swivel seats for convenience.\n", |
| 744 | + "\n" |
| 745 | + ] |
| 746 | + } |
| 747 | + ], |
689 | 748 | "source": [
|
690 | 749 | "# KNN\n",
|
691 | 750 | "# TODO: Add Semantic_Text type?\n",
|
|
723 | 782 | },
|
724 | 783 | {
|
725 | 784 | "cell_type": "code",
|
726 |
| - "execution_count": null, |
| 785 | + "execution_count": 85, |
727 | 786 | "id": "c5475e21",
|
728 | 787 | "metadata": {},
|
729 |
| - "outputs": [], |
| 788 | + "outputs": [ |
| 789 | + { |
| 790 | + "name": "stdout", |
| 791 | + "output_type": "stream", |
| 792 | + "text": [ |
| 793 | + "\n", |
| 794 | + "Score: 11.354144\n", |
| 795 | + "Product: Garden Lounge Set with Side Table\n", |
| 796 | + "Category: Garden Furniture\n", |
| 797 | + "Description: is a comfortable and stylish garden lounge set, including a sofa, chairs, and a side table for outdoor relaxation.\n", |
| 798 | + "\n", |
| 799 | + "\n", |
| 800 | + "Score: 11.200024\n", |
| 801 | + "Product: Garden Lounge Set with Ottoman\n", |
| 802 | + "Category: Garden Furniture\n", |
| 803 | + "Description: is a versatile and comfortable garden lounge set, including a sofa, chairs, and ottoman for outdoor relaxation.\n", |
| 804 | + "\n" |
| 805 | + ] |
| 806 | + } |
| 807 | + ], |
730 | 808 | "source": [
|
731 | 809 | "# Elastic Learned Sparse Encoder - ELSER\n",
|
732 | 810 | "\n",
|
|
759 | 837 | },
|
760 | 838 | {
|
761 | 839 | "cell_type": "code",
|
762 |
| - "execution_count": 60, |
| 840 | + "execution_count": 93, |
| 841 | + "id": "4d2fb926", |
| 842 | + "metadata": {}, |
| 843 | + "outputs": [ |
| 844 | + { |
| 845 | + "name": "stdout", |
| 846 | + "output_type": "stream", |
| 847 | + "text": [ |
| 848 | + "\n", |
| 849 | + "Score: 11.354144\n", |
| 850 | + "Product: Garden Lounge Set with Side Table\n", |
| 851 | + "Category: Garden Furniture\n", |
| 852 | + "Description: is a comfortable and stylish garden lounge set, including a sofa, chairs, and a side table for outdoor relaxation.\n", |
| 853 | + "\n", |
| 854 | + "\n", |
| 855 | + "Score: 11.200024\n", |
| 856 | + "Product: Garden Lounge Set with Ottoman\n", |
| 857 | + "Category: Garden Furniture\n", |
| 858 | + "Description: is a versatile and comfortable garden lounge set, including a sofa, chairs, and ottoman for outdoor relaxation.\n", |
| 859 | + "\n" |
| 860 | + ] |
| 861 | + } |
| 862 | + ], |
| 863 | + "source": [ |
| 864 | + "# Elastic Learned Sparse Encoder - ELSER\n", |
| 865 | + "\n", |
| 866 | + "resp = client.search(\n", |
| 867 | + " index=\"ecommerce-search\",\n", |
| 868 | + " size=2,\n", |
| 869 | + " query={\n", |
| 870 | + " \"semantic\": {\n", |
| 871 | + " \"field\": \"elser_semantic_description_vector\",\n", |
| 872 | + " \"query\": \"Comfortable furniture for a large balcony\",\n", |
| 873 | + " }\n", |
| 874 | + " },\n", |
| 875 | + " source_excludes=[\"*_description_vector\"], # Exclude vector fields from response\n", |
| 876 | + ")\n", |
| 877 | + "\n", |
| 878 | + "elser_semantic_text_search_results = resp[\"hits\"][\"hits\"]\n", |
| 879 | + "results_list.append({\"elser_semantic_text_search\": sparse_semantic_search_results})\n", |
| 880 | + "print_search_results(elser_semantic_text_search_results)" |
| 881 | + ] |
| 882 | + }, |
| 883 | + { |
| 884 | + "cell_type": "markdown", |
| 885 | + "id": "1df079f3", |
| 886 | + "metadata": {}, |
| 887 | + "source": [ |
| 888 | + "## Semantic Search with `semantic_text` Type (e5)" |
| 889 | + ] |
| 890 | + }, |
| 891 | + { |
| 892 | + "cell_type": "code", |
| 893 | + "execution_count": 94, |
763 | 894 | "id": "2c0bf5fc-ab32-4f33-8f26-904ff10635a5",
|
764 | 895 | "metadata": {
|
765 | 896 | "id": "2c0bf5fc-ab32-4f33-8f26-904ff10635a5"
|
|
769 | 900 | "name": "stdout",
|
770 | 901 | "output_type": "stream",
|
771 | 902 | "text": [
|
772 |
| - "No matches found\n" |
| 903 | + "\n", |
| 904 | + "Score: 0.93147576\n", |
| 905 | + "Product: Metal Garden Bench with Cushion\n", |
| 906 | + "Category: Garden Furniture\n", |
| 907 | + "Description: is a stylish and comfortable metal garden bench, complete with a cushion for added support.\n", |
| 908 | + "\n", |
| 909 | + "\n", |
| 910 | + "Score: 0.9304026\n", |
| 911 | + "Product: Garden Dining Set with Swivel Chairs\n", |
| 912 | + "Category: Garden Furniture\n", |
| 913 | + "Description: is a functional and comfortable garden dining set, including a table and chairs with swivel seats for convenience.\n", |
| 914 | + "\n" |
773 | 915 | ]
|
774 | 916 | }
|
775 | 917 | ],
|
|
781 | 923 | " size=2,\n",
|
782 | 924 | " query={\n",
|
783 | 925 | " \"semantic\": {\n",
|
784 |
| - " \"field\": \"elser_semantic_description_vector\",\n", |
| 926 | + " \"field\": \"e5_semantic_description_vector\",\n", |
785 | 927 | " \"query\": \"Comfortable furniture for a large balcony\",\n",
|
786 | 928 | " }\n",
|
787 | 929 | " },\n",
|
788 | 930 | " source_excludes=[\"*_description_vector\"], # Exclude vector fields from response\n",
|
789 | 931 | ")\n",
|
790 | 932 | "\n",
|
791 |
| - "\n", |
792 | 933 | "elser_semantic_text_search_results = resp[\"hits\"][\"hits\"]\n",
|
793 | 934 | "results_list.append({\"elser_semantic_text_search\": sparse_semantic_search_results})\n",
|
794 | 935 | "print_search_results(elser_semantic_text_search_results)"
|
|
0 commit comments