What is a vector embedding? Embedding models, search, and long texts

A vector embedding is a list of numbers that captures meaning. How embedding models and vector search work, embeddings vs TF-IDF, rerankers, and long documents.

On the mic
On the mic

Key takeaways

  • An embedding is a vector of numbers where similar meanings sit close together, so search can match ideas rather than exact words.
  • Embedding models are neural networks trained so related texts get nearby vectors; use the same model for documents and queries.
  • Vector search finds the nearest stored vectors to the query vector, usually with an approximate index such as HNSW.
  • Split long texts into chunks and embed each; one vector for a 32,000-character document loses detail.
  • Embeddings retrieve candidates fast; a reranker scores the top candidates more accurately.

A vector embedding is a list of numbers that represents the meaning of a piece of data, such as a word, sentence, image or document, so that similar things have vectors close together. Embedding models produce these vectors, and vector search uses them to find the most similar items to a query, which is the retrieval step in RAG systems like RAG.NextUpgrad. This page explains embeddings and embedding models, how search works, terminology, building an embedding layer from scratch, long texts, embeddings versus TF-IDF, rerankers, and choosing a model.

What is a vector embedding?

A vector embedding is a fixed-length list of numbers, often hundreds or thousands long, that encodes the meaning or features of an input. Inputs with similar meaning map to vectors that are close in that space, measured by cosine similarity or distance.

Also asked as: what is vector embedding · how do people usually handle creating an embedding vector of longer texts (32000 characters

For example, "How do I reset my password?" and "I forgot my login" have different words but similar embeddings, so a search using embeddings matches them. Embeddings exist for text, images, audio and code, and multimodal models can place images and text in the same space. They power semantic search, recommendations, clustering, duplicate detection and classification, and they are the backbone of retrieval-augmented generation.

What is an embedding model?

An embedding model is a neural network that converts input, such as text, into an embedding vector. Text embedding models are trained so that related sentences or passages produce nearby vectors and unrelated ones produce distant vectors.

Also asked as: what is embedding model · embedding model vs reranking model

You can use hosted embedding models through APIs from providers such as OpenAI, Google and others, or open models from Hugging Face, such as sentence-transformers and newer open embedding families, run locally or through Ollama. Important properties are the vector dimension, maximum input length, supported languages and quality on retrieval benchmarks. The rule that matters most in practice: use the same embedding model for indexing documents and for embedding queries, or the vectors will not be comparable.

How does an embedding model work?

Modern text embedding models are transformer encoders. They tokenise the text, process tokens with attention so each token's representation reflects its context, and pool the token vectors, for example by averaging, into one vector for the whole text.

Also asked as: how does embedding model work

They learn useful vectors through contrastive training: pairs of related texts, such as a question and its answer passage, are pulled together, while unrelated texts are pushed apart. Training on millions of such pairs teaches the model which meanings are similar. Sentence-BERT popularised this approach for sentence embeddings. Some models also accept instructions, such as "represent this query for retrieval", which improves search quality, so follow each model's usage notes on its model card.

How does vector embedding search work?

You embed every document chunk once and store the vectors in an index. At query time, you embed the query with the same model and find the stored vectors nearest to it, returning their chunks as results.

Comparing a query with every vector works for small collections but becomes slow at scale, so vector databases use approximate nearest-neighbour indexes, such as HNSW graphs or inverted-file indexes, that return nearly the best matches in milliseconds. Normalise vectors and use cosine similarity or inner product as the model recommends. In production, combine vector search with keyword search, merge the lists with reciprocal-rank fusion, and add metadata filters, because embeddings alone often miss exact terms such as IDs and error codes.

Are "vector embedding" and "embedding vector" interchangeable?

Yes, in practice they mean the same thing: the vector produced by an embedding model. "Embedding" alone is also common. Strictly, "embedding" can mean the mapping itself, while "embedding vector" is one output of it.

Also asked as: are the terms "vector embedding" and "embedding vector" interchangeable

You will also see "dense vector" or "dense representation", in contrast to sparse vectors from keyword methods such as BM25 or TF-IDF, where most values are zero. In vector database documentation, "vectors" usually refers to embeddings. When writing documentation or reports, pick one term and use it consistently, and say which model produced the vectors, because embeddings from different models are not interchangeable.

How can I create an embedding layer from scratch?

An embedding layer is a lookup table: a matrix with one row per word in your vocabulary, initialised randomly and learned during training. In PyTorch, nn.Embedding provides it; training the network on a task updates the rows so useful word relationships emerge.

Also asked as: how can i create an embedding layer to convert words to a vector space from scratch

import torch, torch.nn as nn
vocab = {"<pad>": 0, "ai": 1, "model": 2, "data": 3}
emb = nn.Embedding(num_embeddings=len(vocab), embedding_dim=8, padding_idx=0)
ids = torch.tensor([[1, 2, 3]])
vectors = emb(ids)          # shape (1, 3, 8)

To learn word vectors without a downstream task, train word2vec-style: predict surrounding words from a word, or the reverse, which makes words in similar contexts get similar vectors. For real projects, pre-trained sentence embedding models are far better than training from scratch.

How do you create embeddings for long texts?

Split long texts into chunks, embed each chunk, and store them separately with a reference to the source document. A single vector for a 32,000-character document averages away detail, and many embedding models cannot accept that much text anyway.

Chunking guidelines:

  • Split by structure, such as headings and paragraphs, to keep ideas together.
  • Use chunks of a few hundred tokens, with small overlap where meaning spans boundaries.
  • Include the document title and section heading in each chunk's text.
  • Keep page numbers and IDs in metadata for citations.

If you need one vector per document, for example for clustering, average the chunk embeddings or use a long-context embedding model, but retrieve at chunk level for question answering.

Vector embeddings vs TF-IDF: which is better for document similarity?

Embeddings usually perform better when meaning matters and wording differs, such as paraphrases and questions versus answers. TF-IDF and BM25 perform better for exact terms, rare names, codes and specialised jargon. Combining both usually beats either alone.

Also asked as: document similarity: vector embedding versus tf-idf performance

TF-IDF and bag-of-words count words, so "car" and "automobile" do not match, but "error 0x80070005" matches exactly. Embeddings capture semantics but can blur specific identifiers. TF-IDF is also cheap, transparent and needs no model. A sound approach is to evaluate both on your own data with labelled similar pairs or search queries, then use hybrid retrieval: run both, and merge results with reciprocal-rank fusion, as RAG.NextUpgrad does with dense retrieval and BM25.

What is the difference between an embedding model and a reranking model?

An embedding model encodes the query and each document separately into vectors, which makes search over millions of documents fast. A reranking model, usually a cross-encoder, reads the query and one document together and outputs a relevance score, which is more accurate but too slow to run over everything.

So they work in two stages: embeddings, often with keyword search, retrieve the top 50 to 100 candidates quickly, and the reranker reorders those candidates by true relevance. Adding a reranker is one of the most effective ways to improve RAG answers, because the right passage is often retrieved but ranked too low to reach the prompt. Measure retrieval recall at 5 before and after adding one.

There is no universal best. Check the MTEB leaderboard for retrieval scores, then test two or three candidates on your own queries, considering language support, maximum input length, vector size, speed and cost.

Also asked as: best semantic search embedding model

Practical guidance:

  • Hosted and easy: embedding APIs from major providers.
  • Open and local: strong sentence-transformers and newer open embedding models, runnable with Ollama or Hugging Face.
  • Indian languages or mixed Hindi-English: choose a multilingual model and test on your own data.
  • Large scale: smaller dimensions reduce storage and speed up search.

Build a test set of 50 real queries with known relevant passages, and pick the model with the best recall at 5 for your data, not the best leaderboard score.

Sources

  1. Mikolov et al., Efficient Estimation of Word Representations in Vector Space (2013)arxiv.org
  2. Reimers & Gurevych, Sentence-BERT (2019)arxiv.org
  3. Muennighoff et al., MTEB: Massive Text Embedding Benchmark (2022)arxiv.org
  4. PyTorch, torch.nn.Embeddingpytorch.org
  5. Nogueira & Cho, Passage Re-ranking with BERT (2019)arxiv.org
Pranjul Rathour
Pranjul Rathour
GenAI Engineer · India

GenAI engineer and AI product builder with 2+ years shipping production-grade AI systems: RAG pipelines, fine-tuned LLMs, hybrid retrieval and multi-modal apps across vision, speech and OCR, architected end to end from ingestion to deployment. Leads engineering for SCULT INDIA's 14-member team, founded the 500+ member TechVerse Enclave community and has mentored 200+ students. Three hackathon first prizes: Changethon 2025 (IIT Roorkee), Product Genesis at Vividhotsava 2025 (CSJMU) and BYTEBATTLE (MeetKats).

Open to GenAI roles, hackathon judging, mentorship sessions and guest talks at colleges. For campus sessions the inviting institution covers the visit end to end: travel (a First AC train ticket for journeys up to five hours, a flight for anything longer), accommodation, meals, and pickup and drop; the honorarium and workshop fees are negotiable on WhatsApp or on the call, and requests are welcome from institutions that can cover the visit this way. Email pranjulrathour41@gmail.com.

Keep reading

More on RAG & retrieval

All RAG & retrieval guides →
Campus talks · hackathon judging · mentoring

Want this as a live session at your college?

Open to GenAI roles, hackathon judging, mentorship sessions and guest talks at colleges. For campus sessions the inviting institution covers the visit end to end: travel (a First AC train ticket for journeys up to five hours, a flight for anything longer), accommodation, meals, and pickup and drop; the honorarium and workshop fees are negotiable on WhatsApp or on the call, and requests are welcome from institutions that can cover the visit this way.

Pranjul Rathour, GenAI engineer in India