Skip to content

Instantly share code, notes, and snippets.

@nathancday
Created February 28, 2023 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathancday/aa3815982b2ec6f843df804e5f1e0483 to your computer and use it in GitHub Desktop.
Save nathancday/aa3815982b2ec6f843df804e5f1e0483 to your computer and use it in GitHub Desktop.
Hybrid vigor: a winning way to use hybrid search
PUT trec
{
"mappings": {
"properties": {
"title" : {"type": "text"},
"episode": {"type": "text"},
"text": {"type": "text"},
"embed": {"type": "dense_vector", "dims": 3}
}
}
}
POST trec/_doc
{
"title": "Great Steaks",
"episode": "T-bones FTW",
"text": "When it comes down to it, in my option, the best cut of steak in the cow is the T-Bone! You can't beat the juicy tenderness",
"embed": [3.0, 2.1, 0.2]
}
POST trec/_doc
{
"title": "Beef Eater Anonymos",
"episode": "Why red meat is actually good for you",
"text": "Sure a steak tasts great, but is it also great for you? I think it is and my guess Teddy Steaks here on the show today say YES!",
"embed": [2.0, 2.9, 0.1]
}
GET trec/_search // first we gotta get the maxScore
{
"size": 1,
"query": {
"multi_match": {
"fields": [
"title",
"episode",
"text^3"
],
"query": "tasty steaks"
}
}
}
GET trec/_search
{
"query": {
"script_score": {
"query": {
"multi_match": {
"fields": [
"title",
"episode",
"text^3"
],
"query": "tasty steaks"
}
},
"script": {
"params": {
"maxScore": 2.064089,
"queryVector": [3.1, 2.5, 0.1]
},
"source": """
double myScore = _score / params.maxScore;
double sbertScore = cosineSimilarity(params.queryVector, 'embed');
return myScore + sbertScore;
"""
}
}
}
}
DELETE trec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment