Created
June 25, 2024 19:59
-
-
Save xeraa/de8825ba3799ae7c03b8f497405504d2 to your computer and use it in GitHub Desktop.
Elasticsearch semantic_text in action
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PUT starwars | |
{ | |
"settings": { | |
"analysis": { | |
"analyzer": { | |
"my_analyzer": { | |
"char_filter": [ | |
"html_strip" | |
], | |
"tokenizer": "standard", | |
"filter": [ | |
"lowercase", | |
"stop", | |
"snowball" | |
] | |
} | |
} | |
} | |
}, | |
"mappings": { | |
"properties": { | |
"quote": { | |
"type": "text", | |
"analyzer": "my_analyzer" | |
} | |
} | |
} | |
} | |
PUT starwars/_doc/1 | |
{ | |
"quote": "These are <em>not</em> the droids you are looking for." | |
} | |
GET starwars/_search | |
{ | |
"query": { | |
"match": { | |
"quote": "Droid" | |
} | |
} | |
} | |
PUT _inference/sparse_embedding/elser-2 | |
{ | |
"service": "elser", | |
"service_settings": { | |
"num_allocations": 1, | |
"num_threads": 1 | |
} | |
} | |
PUT _inference/text_embedding/e5-small-multilingual | |
{ | |
"service": "elasticsearch", | |
"service_settings": { | |
"num_allocations": 1, | |
"num_threads": 1, | |
"model_id": ".multilingual-e5-small_linux-x86_64" | |
} | |
} | |
GET _inference | |
PUT semantic-starwars | |
{ | |
"settings": { | |
"analysis": { | |
"analyzer": { | |
"my_analyzer": { | |
"char_filter": [ | |
"html_strip" | |
], | |
"tokenizer": "standard", | |
"filter": [ | |
"lowercase", | |
"stop", | |
"snowball" | |
] | |
} | |
} | |
} | |
}, | |
"mappings": { | |
"properties": { | |
"quote": { | |
"type": "text", | |
"analyzer": "my_analyzer", | |
"copy_to": ["quote_elser", "quote_e5"] | |
}, | |
"quote_elser": { | |
"type": "semantic_text", | |
"inference_id": "elser-2" | |
}, | |
"quote_e5": { | |
"type": "semantic_text", | |
"inference_id": "e5-small-multilingual" | |
} | |
} | |
} | |
} | |
PUT semantic-starwars/_doc/1 | |
{ | |
"quote": "These are <em>not</em> the droids you are looking for." | |
} | |
GET semantic-starwars/_search | |
{ | |
"query": { | |
"semantic": { | |
"field": "quote_elser", | |
"query": "machine" | |
} | |
} | |
} | |
GET semantic-starwars/_search | |
{ | |
"query": { | |
"semantic": { | |
"field": "quote_e5", | |
"query": "search for an android" | |
} | |
} | |
} | |
GET semantic-starwars/_search | |
{ | |
"query": { | |
"bool": { | |
"should": [ | |
{ | |
"semantic": { | |
"field": "quote_elser", | |
"query": "android", | |
"_name": "elser" | |
} | |
}, | |
{ | |
"match": { | |
"quote": { | |
"query": "android", | |
"_name": "keyword" | |
} | |
} | |
} | |
] | |
} | |
} | |
} | |
GET semantic-starwars/_search | |
{ | |
"knn": { | |
"field": "quote_dense_e5", | |
"k": 10, | |
"num_candidates": 100, | |
"query_vector_builder": { | |
"text_embedding": { | |
"model_id": "e5-small-multilingual", | |
"model_text": "search for an android" | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment