Vector Search in Elastic

Introduction

Explanation of what vector search is

Vector search is a technique that uses vector-based representation of data to search for similar items or patterns. It is a machine learning approach that transforms unstructured data, such as text, images or audio, into a numerical representation in a high-dimensional space. This numerical representation of the data is then used to perform similarity calculations using algorithms such as cosine similarity or k-nearest neighbors. Vector search is commonly used in natural language processing, recommendation systems, image and video search, and other areas where similarity matching is important. Compared to traditional keyword search, vector search produces more accurate and relevant results and operates at a faster pace.

Importance of vector search in various fields

Vector search can be used in various applications to help users find relevant information efficiently. Some examples of its application are:

  • Recommendations: Online shopping websites may use vector search to recommend products based on a user’s search history, purchase history, and preferences.

  • Image similarities: Vector search can be used to retrieve similar images

  • Content-based image retrieval: Vector search can be used to search for images based on their visual features like colors, shapes, and textures.

  • Music recommendations: Music streaming services use vector search to recommend songs based on a user’s listening history, mood, and preferences.

  • Natural language processing: Vector search is used in language processing tasks like text classification, sentiment analysis, and machine translation.

How does a Vector Search Engine work?

Vector search engine identifies the closest neighbors to a provided query that has been vectorized. Rather than relying on keyword mentions, lexical similarity, or word frequency, vector search engine uses distances in the embedding space to represent similarity. Therefore, searching for related data in vector search becomes equivalent to searching for the nearest neighbors of the query.

Vector Search

There are three main components of an engine:

  • Convert document (text, image, audio) to numbers with vector embeddings.

    Machine learning models are created to transform document content, keywords, sentences, images or audios into numerical data, also called vector embeddings that can be compared.

  • Fast search algorithm.

    Vector search uses powerful algorithm to quickly find and show results that are relevant, even when dealing with billions of documents. That algorithm called approximate nearest neighbor (ANN). The core of an algorithm is a graph, which allows easily move through all the data points and quickly find the best matching results.

  • Discover similar results with distance metrics.

    Using vectors, we can give each data point in search term a unique set of coordinates that can be utilized to determine how similar a query is to a document by measuring the distance between related search terms. When the distance between vectors is shorter, it indicates that the content is more alike. Techniques like cosine similarity or k-nearest are applied to evaluate the similarity between two sequences of numbers or vectors, which enables us to find the most comparable results.

Vector Search is built in Elastic products

Elastic provides an easy, integrated end-to-end solution for vector search, which covers an ability to import pre-trained ML models from public repositories, apply them during data ingestion, and leverage dedicated search API to enhance your search experience. Most solutions require an external process to generate vector embeddings, the advantage of Elastic’s vector search is how easy it is to create support for vector embeddings. This is done through the dense vector field type, which stores dense vectors of float values and can be used to quickly find similar results with Elastics kNN search API.

Image Similarities with ElasticSearch

Using vector search, a built-in feature of Elastic, organizations can enable similarity image search. This facilitates a more intuitive search experience for customers, allowing them to search for their desired items by simply uploading an image. This diagram shows high level proposed architecture for finding similar images using vector search in Elastic.

image similarity architecture

  • Embedding models

    To enable similarity search for image data, machine learning models are required to convert the data into numeric vector embeddings. Example of such “transformer” model is the OpenAI CLIP model. CLIP model is imported from public repository and uploaded into Machine Learning instance in Elastic Cloud

  • Generate image embeddings

    CLIP model is used to generate embedding, which occurs at ingest time through external processes, including loading the model, evaluating images, saving embeddings into a document, and storing the document into Elasticsearch. Image embeddings should be stored separately in a dedicated index, which contains a document per image and fields for the context and image embedding. The process of image embeddings can be described as: image-embeddings

    The document inside an index after embedding looks like:

    {
     "_index": "image-embeddings",
     "_id": "ecknklcnik92dwk",
     "_score": 6.703597,
     "_source": {
       "image_id": "car_1111",
       "image_name": "car_1111.jpeg",
       "image_embedding": [
         -0.3415695130825043,
         0.1906963288784027,
         .....
         -0.10289803147315979,
         -0.15871885418891907
         ],
       "relative_path": "vehicles/car_1111.jpeg"
     }
    }
    
  • The Application Logic

    The application logic consists of:

    • The query issued by an user, based on a given image, which is vectorized by the embedding model.
    • It converts an input image into its numeric representation and store the result into a dense vector type in Elasticsearch ([number, number, number…]).
    • The vector representation is then used in a kNN search API to find similar vectors (images).

    That flow can be shown as:

    application-logic

Conclusion:

Vector search in Elastic platform enables users to discover more relevant results, improve search experiences, and enhance various applications such as e-commerce, content-based image retrieval, music recommendations, and natural language processing.