Last active
February 2, 2025 01:42
-
-
Save xeraa/de8825ba3799ae7c03b8f497405504d2 to your computer and use it in GitHub Desktop.
Elasticsearch semantic_text in action
This file contains hidden or 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
GET / | |
DELETE starwars | |
DELETE semantic-starwars | |
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" | |
} | |
} | |
} | |
} | |
GET starwars/_analyze | |
{ | |
"analyzer" : "my_analyzer", | |
"text": "These are <em>not</em> the droids you are looking for." | |
} | |
PUT starwars/_doc/1 | |
{ | |
"quote": "These are <em>not</em> the droids you are looking for." | |
} | |
PUT starwars/_doc/2 | |
{ | |
"quote": "<b>No.</b> I am your father." | |
} | |
GET starwars/_search | |
{ | |
"query": { | |
"match": { | |
"quote": "Droid" | |
} | |
} | |
} | |
PUT _inference/sparse_embedding/elser | |
{ | |
"service": "elser", | |
"service_settings": { | |
"num_allocations": 1, | |
"num_threads": 1 | |
} | |
} | |
PUT _inference/text_embedding/e5 | |
{ | |
"service": "elasticsearch", | |
"service_settings": { | |
"model_id": ".multilingual-e5-small", | |
"num_allocations": 1, | |
"num_threads": 1 | |
} | |
} | |
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" | |
}, | |
"quote_e5": { | |
"type": "semantic_text", | |
"inference_id": "e5" | |
} | |
} | |
} | |
} | |
POST _reindex | |
{ | |
"source": { | |
"index": "starwars" | |
}, | |
"dest": { | |
"index": "semantic-starwars" | |
} | |
} | |
GET semantic-starwars/_search | |
{ | |
"query": { | |
"semantic": { | |
"field": "quote_elser", | |
"query": "robot" | |
} | |
} | |
} | |
GET semantic-starwars/_search | |
{ | |
"query": { | |
"semantic": { | |
"field": "quote_e5", | |
"query": "dad" | |
} | |
}, | |
"_source": "quote" | |
} | |
GET semantic-starwars/_search | |
{ | |
"query": { | |
"bool": { | |
"should": [ | |
{ | |
"semantic": { | |
"field": "quote_elser", | |
"query": "android", | |
"boost": 1, | |
"_name": "elser" | |
} | |
}, | |
{ | |
"match": { | |
"quote": { | |
"query": "android", | |
"boost": 3, | |
"_name": "keyword" | |
} | |
} | |
} | |
] | |
} | |
}, | |
"_source": "quote" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment