🎹🎻🎸 Searching for similar music tracks 🎼🎶

A presentation at OpenConf in November 2024 in Athens, Greece by David Pilato

Slide 1

Slide 1

🎹 Searching for similar musics 🎻🎸 David Pilato | @dadoonet

Slide 2

Slide 2

Elasticsearch You Know, for Search

Slide 3

Slide 3

Slide 4

Slide 4

Slide 5

Slide 5

These are not the droids you are looking for.

Slide 6

Slide 6

GET /_analyze { “char_filter”: [ “html_strip” ], “tokenizer”: “standard”, “filter”: [ “lowercase”, “stop”, “snowball” ], “text”: “These are <em>not</em> the droids you are looking for.” }

Slide 7

Slide 7

These are <em>not</em> the droids you are looking for. { “tokens”: [{ “token”: “droid”, “start_offset”: 27, “end_offset”: 33, “type”: “<ALPHANUM>”, “position”: 4 },{ “token”: “you”, “start_offset”: 34, “end_offset”: 37, “type”: “<ALPHANUM>”, “position”: 5 }, { “token”: “look”, “start_offset”: 42, “end_offset”: 49, “type”: “<ALPHANUM>”, “position”: 7 }]}

Slide 8

Slide 8

Semantic search ≠ Literal matches

Slide 9

Slide 9

Elasticsearch You Know, for Search

Slide 10

Slide 10

Elasticsearch You Know, for Vector Search

Slide 11

Slide 11

Example: 1-dimensional vector Character Vector [ 1 ] ] Realistic

[ Embeddings represent your data Cartoon 1

Slide 12

Slide 12

represent different data aspects Human Character Vector [ 1, 1 Realistic Cartoon ] ] Machine

[ Multiple dimensions 1, 0

Slide 13

Slide 13

is grouped together Human Character Vector [ 1.0, 1.0 1.0, 0.0 Realistic Cartoon [ 1.0, 0.8 1.0, 1.0 [ 1.0, 1.0 ] ] ] ] ]

Machine

[ [ Similar data

Slide 14

Slide 14

Vector search ranks objects by similarity (~relevance) to the query Human Rank Query 1 Realistic Cartoon 2 3 4 5 Machine Result

Slide 15

Slide 15

How do you index vectors ?

Slide 16

Slide 16

Architecture of Vector Search

Slide 17

Slide 17

dense_vector field type PUT ecommerce { “mappings”: { “properties”: { “description”: { “type”: “text” } “desc_embedding”: { “type”: “dense_vector” } } } }

Slide 18

Slide 18

Data Ingestion and Embedding Generation POST ecommerce/_doc { “_id”:”product-1234”, “product_name”:”Summer Dress”, “description”:”Our best-selling…”, “Price”: 118, “color”:”blue”, “fabric”:”cotton”, “fabric”:”cotton” “desc_embedding”:[0.452,0.3242,…], } “desc_embedding”:[0.452,0.3242,…] } “img_embedding”:[0.012,0.0,…] } Source data POST /ecommerce/_doc

Slide 19

Slide 19

How do you search vectors ?

Slide 20

Slide 20

Architecture of Vector Search

Slide 21

Slide 21

knn query GET ecommerce/_search { “query” : { “bool”: { “must”: [{ “knn”: { “field”: “desc_embbeding”, “num_candidates”: 50, “query_vector”: [0.123, 0.244,…] } }], “filter”: { “term”: { “department”: “women” } } } } }, “size”: 10

Slide 22

Slide 22

Architecture of Vector Search

Slide 23

Slide 23

But how does it really work?

Slide 24

Slide 24

Similarity Human q cos(θ) = d1 d2 Realistic θ q⃗ × d ⃗ | q⃗ | × | d |⃗ _score = 1 + cos(θ) 2

Slide 25

Slide 25

Similarity θ Similar vectors θ close to 0 cos(θ) close to 1 1+1 _score = =1 2 θ Orthogonal vectors θ close to 90° cos(θ) close to 0 1+0 _score = = 0.5 2 θ Opposite vectors θ close to 180° cos(θ) close to -1 1−1 _score = =0 2

Slide 26

Slide 26

https://djdadoo.pilato.fr/

Slide 27

Slide 27

https://github.com/dadoonet/music-search/

Slide 28

Slide 28

🎹 Searching for similar musics 🎻🎸 David Pilato | @dadoonet