Skip to content

Commit ca99d4f

Browse files
author
Justin Castilla
committed
sample object edit
1 parent 3cd8fdb commit ca99d4f

File tree

1 file changed

+160
-19
lines changed

1 file changed

+160
-19
lines changed

supporting-blog-content/lexical-and-semantic-search-with-elasticsearch/updated-ecommerce_dense_sparse_project.ipynb

Lines changed: 160 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"```json\n",
2121
" {\n",
2222
" \"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",
2424
" \"category\": \"Monitors\"\n",
2525
"}\n",
2626
"\n",
@@ -223,7 +223,7 @@
223223
},
224224
{
225225
"cell_type": "code",
226-
"execution_count": 56,
226+
"execution_count": 78,
227227
"id": "6739f55b-6983-4b48-9349-6e0111b313fe",
228228
"metadata": {
229229
"id": "6739f55b-6983-4b48-9349-6e0111b313fe"
@@ -290,7 +290,7 @@
290290
},
291291
{
292292
"cell_type": "code",
293-
"execution_count": null,
293+
"execution_count": 82,
294294
"id": "9b53b39e-d74e-4fa8-a364-e2c3caf37418",
295295
"metadata": {
296296
"id": "9b53b39e-d74e-4fa8-a364-e2c3caf37418"
@@ -330,8 +330,8 @@
330330
" \"inference_id\": e5_endpoint,\n",
331331
" },\n",
332332
" \"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",
335335
" },\n",
336336
" }\n",
337337
"}\n",
@@ -358,7 +358,7 @@
358358
},
359359
{
360360
"cell_type": "code",
361-
"execution_count": 57,
361+
"execution_count": 83,
362362
"id": "c4830b74",
363363
"metadata": {},
364364
"outputs": [
@@ -392,7 +392,7 @@
392392
},
393393
{
394394
"cell_type": "code",
395-
"execution_count": 58,
395+
"execution_count": null,
396396
"id": "3cfdc3b7-7e4f-4111-997b-c333ac8938ba",
397397
"metadata": {
398398
"id": "3cfdc3b7-7e4f-4111-997b-c333ac8938ba"
@@ -415,6 +415,9 @@
415415
"\n",
416416
"# helper function to create bulk indexing body\n",
417417
"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",
418421
" return {\n",
419422
" \"_index\": \"ecommerce-search\",\n",
420423
" \"_source\": doc,\n",
@@ -528,10 +531,28 @@
528531
},
529532
{
530533
"cell_type": "code",
531-
"execution_count": null,
534+
"execution_count": 64,
532535
"id": "d44f3e2b",
533536
"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+
],
535556
"source": [
536557
"index_settings = {\n",
537558
" \"settings\": {\n",
@@ -624,12 +645,31 @@
624645
},
625646
{
626647
"cell_type": "code",
627-
"execution_count": null,
648+
"execution_count": 65,
628649
"id": "f4984f6c-ceec-46a4-b64c-f749e6b1b04f",
629650
"metadata": {
630651
"id": "f4984f6c-ceec-46a4-b64c-f749e6b1b04f"
631652
},
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+
],
633673
"source": [
634674
"results_list = []\n",
635675
"\n",
@@ -680,12 +720,31 @@
680720
},
681721
{
682722
"cell_type": "code",
683-
"execution_count": null,
723+
"execution_count": 66,
684724
"id": "72187c9a-14c1-4084-a080-4e5c1e614f22",
685725
"metadata": {
686726
"id": "72187c9a-14c1-4084-a080-4e5c1e614f22"
687727
},
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+
],
689748
"source": [
690749
"# KNN\n",
691750
"# TODO: Add Semantic_Text type?\n",
@@ -723,10 +782,29 @@
723782
},
724783
{
725784
"cell_type": "code",
726-
"execution_count": null,
785+
"execution_count": 85,
727786
"id": "c5475e21",
728787
"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+
],
730808
"source": [
731809
"# Elastic Learned Sparse Encoder - ELSER\n",
732810
"\n",
@@ -759,7 +837,60 @@
759837
},
760838
{
761839
"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,
763894
"id": "2c0bf5fc-ab32-4f33-8f26-904ff10635a5",
764895
"metadata": {
765896
"id": "2c0bf5fc-ab32-4f33-8f26-904ff10635a5"
@@ -769,7 +900,18 @@
769900
"name": "stdout",
770901
"output_type": "stream",
771902
"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"
773915
]
774916
}
775917
],
@@ -781,14 +923,13 @@
781923
" size=2,\n",
782924
" query={\n",
783925
" \"semantic\": {\n",
784-
" \"field\": \"elser_semantic_description_vector\",\n",
926+
" \"field\": \"e5_semantic_description_vector\",\n",
785927
" \"query\": \"Comfortable furniture for a large balcony\",\n",
786928
" }\n",
787929
" },\n",
788930
" source_excludes=[\"*_description_vector\"], # Exclude vector fields from response\n",
789931
")\n",
790932
"\n",
791-
"\n",
792933
"elser_semantic_text_search_results = resp[\"hits\"][\"hits\"]\n",
793934
"results_list.append({\"elser_semantic_text_search\": sparse_semantic_search_results})\n",
794935
"print_search_results(elser_semantic_text_search_results)"

0 commit comments

Comments
 (0)