Blog

Elasticsearch: The Search Engine That Also Understands Music

05/20/2025 at 2:00pm
Elasticsearch
Fuzzy.Search
Melodic.Similarity

When text search meets melodic structure

Image relating Elasticsearch to melodic search.

Table of Contents

Introduction

Most Elasticsearch applications focus on textual data, such as log analysis or e-commerce searches. But what if the goal was to find similarity between musical melodies?

This article presents an adaptation of textual search techniques for melodic patterns, allowing you to compare sequences of notes—even with small variations—against a melody database.

The approach involves representing melodic structure symbolically, applying n-grams, and using Elasticsearch’s fuzzy search as a comparison engine.

The Problem

Imagine a user whistles or types a simple sequence like: C D E D C. The goal is to find songs that contain this pattern or something similar, even with different notes or small variations. How can you create a search engine that allows this?

Working with musical notes means dealing with structured data that has semantic and relational meaning: interval, direction, repetition, contour. A literal comparison of sequences is not enough to capture musical meaning.

The Abstraction: Melodic Contour

Instead of comparing absolute notes (like C D and E), melodies are abstracted to their melodic contours. This means representing the direction of movement between notes:

Let’s look at the song “Happy Birthday”:

Sheet music of the melody: Happy Birthday

The melody: G, G, A, G, C, B … becomes: “RUDUD” … This construction is made from every two notes:

  • G → G (same): R (Repeat)
  • G → A (higher): U (Up)
  • A → G (lower): D (Down)
  • G → C (higher): U (Up)
  • C → B (lower): D (Down)

This symbolic representation makes it easier to identify similar melodies, regardless of key, tempo, or note duration, since it considers only the melodic contour.

Applying n-grams to Capture Patterns

With contours transformed into strings, n-grams are applied to break the sequence into segments:

For “RUDUD”, with n=3, you get: “RUD”, “UDU”, “DUD”.

This is similar to what is done in NLP (Natural Language Processing) with words or characters. The goal is to index parts of the melody to allow partial matching.

Fuzzy Search with Elasticsearch

A mapping was created in Elasticsearch with type “text” and an edge n-gram analyzer, allowing melodic fragments to be indexed as “partial words” and searched with fuzziness: 0. No tolerance for differences, making the search more precise for short patterns.

Fuzzy search in Elasticsearch works by calculating the Levenshtein distance, which measures the minimum number of operations (insertions, deletions, or substitutions) needed to transform one sequence into another. This way, you can find similar patterns even if there are small variations between them.

Let’s consider as an example the section highlighted in green without the note identified in blue, taken from the song “Happy Birthday”:

Sheet music of the melody: Happy Birthday

Example of a simplified query:

With this model, it is possible to search for similar melodic patterns even when there is transposition, variations, or intermediate notes. See the search result for the query above:

Demonstration

See the full project at GitHub

Conclusion

By reinterpreting textual search as a symbolic search for structures, Elasticsearch becomes a powerful engine for comparing musical patterns. The solution is scalable, efficient, and enables applications in musicology, recommendation systems, and plagiarism detection by identifying songs with similar patterns.

It is worth noting that this is just one of many possible approaches for melodic similarity search, and there are several alternatives depending on the context.

Did you already know about this solution for searching songs with Elasticsearch? If you found it interesting, share it with colleagues who are also interested in technology and music. See you next time!

Foto de Alex Caranha

Hi, I'm Alex Caranha, thanks for reading!

If you liked this article, make sure to follow me on socials to stay updated with the latest ones: