Close Menu
geekfence.comgeekfence.com
    What's Hot

    Uber is building an autonomous vehicle empire, and here’s every company it’s using to do it

    August 1, 2026

    Building the AI backbone, waiting on the AI billions

    August 1, 2026

    The End-to-End Agentic AI Pipeline

    August 1, 2026
    Facebook X (Twitter) Instagram
    • About Us
    • Contact Us
    Facebook Instagram
    geekfence.comgeekfence.com
    • Home
    • UK Tech News
    • AI
    • Big Data
    • Cyber Security
      • Cloud Computing
      • iOS Development
    • IoT
    • Mobile
    • Software
      • Software Development
      • Software Engineering
    • Technology
      • Green Technology
      • Nanotechnology
    • Telecom
    geekfence.comgeekfence.com
    Home»Big Data»LanceDB Vector Database Guide: Features anndPython Demo
    Big Data

    LanceDB Vector Database Guide: Features anndPython Demo

    AdminBy AdminAugust 1, 2026No Comments8 Mins Read2 Views
    Facebook Twitter Pinterest LinkedIn Telegram Tumblr Email
    LanceDB Vector Database Guide: Features anndPython Demo
    Share
    Facebook Twitter LinkedIn Pinterest Email


    Large language models understand text well, but they become less effective when information is scattered across documents or mixed with images and other media. Modern AI systems rely on vector databases, which store embeddings and enable similarity search across collections.

    LanceDB is a vector database built for AI workloads, with native support for multimodal data and efficient retrieval. In this article, we will examine how vector databases work, how they retrieve similar items, how multimodal search is implemented, and what makes LanceDB useful for modern AI applications.

    What Is a Vector Database?

    In simple terms, vector databases are databases that store high-dimensional numerical vectors (embeddings) of chunks (chunks are text-pieces of document(s)). A vector database stores the embeddings in an indexed manner, which means all similar embeddings sit close to each other in the database.

    What is a vector database?

    We use a query and find the most similar items in the vector database. When we apply this approach and pass the most similar items to an LLM, then it becomes a RAG (Retrieval Augmented Generation).

    But how do we find similarities? we use the embeddings or actual documents and take help of these approaches:

    • Distance metrics: L2, cosine, dot product, hamming distance
    • Approximate Nearest Neighbor (ANN): IVF, HNSW, PQ fast even at billions of rows, trading a small amount of recall for large speedups
    • Metadata filtering: Combining other approaches with filtering

    LanceDB and Its Features

    LanceDB is an open-source, vector database. It can be used locally, or you can use the enterprise version, or self-host and use LanceDB.

    Key features:

    1. Multimodal by design: text, vectors, images, audio, and video live as columns in the same table, not as separate data. But you can opt for a mulit-table approach if that works better for you.
    2. Multiple index types: IVF / HNSW / PQ / RQ for vectors, BM25 for full text
    3. Hybrid search: combine vector similarity and keyword (BM25) search and re-rankers can be used to rank the retrieved documents.
    4. Versioning: every write creates a new version; you can checkout, restore, or tag any past version, like Git for your table.
    5. Schema changes: add, rename, retype, or drop columns without rewriting the whole dataset, thanks to Lance’s columnar storage.
    6. Object storage: the same API works against a local folder or an S3, GS or AZ path.
    7. SDKs: Python, TypeScript/JavaScript, and Rust.

    Demo

    In this section, let’s look at Python examples to use LanceDB to store embeddings, search for similar items, to chunk and index a document, and look at how to store images in the vector tables.

    Pre-requisites

    You will need an OpenAI key to create the embeddings, you can choose to use any alternatives as well.

    Installations

    uv pip install lancedb pandas pyarrow pypdf pillow numpy openai open-clip-torch torch

    Note: uv is recommended for faster installation

    Imports

    import io
    from getpass import getpass
    from pathlib import Path
    
    import lancedb
    import numpy as np
    import pandas as pd
    from pypdf import PdfReader
    
    OPENAI_API_KEY = getpass("Enter your OpenAI API key: ")

    Note: Enter the OpenAI key when prompted (If you are using OpenAI’s embedding models)

    Initialization

    # Local, embedded LanceDB
    db = lancedb.connect("./lancedb_data")
    print("Connected to local LanceDB at ./lancedb_data")
    print("Existing tables:", db.table_names())

    Intializing the database locally

    Basic vector search example

    data = [
        {
            "id": 1,
            "text": "A cat sleeping on a sofa",
            "vector": [0.1, 0.2, 0.3, 0.4],
        },
        {
            "id": 2,
            "text": "A dog playing fetch in the park",
            "vector": [0.9, 0.8, 0.1, 0.2],
        },
        {
            "id": 3,
            "text": "A kitten chasing a laser pointer",
            "vector": [0.15, 0.25, 0.35, 0.4],
        },
        {
            "id": 4,
            "text": "A puppy running through a field",
            "vector": [0.85, 0.75, 0.15, 0.25],
        },
    ]
    
    table = db.create_table("pets", data=data, mode="overwrite")
    
    table.to_pandas()

    Note: The numerical vectors here are just example embeddings used to understand vector databases here.

    Basic vector search example – illustration
    # Query vector close to the "cat" entries
    query_vector = [0.12, 0.22, 0.32, 0.4]
    
    results = (
        table.search(query_vector)
        .limit(2)
        .select(["id", "text", "_distance"])
        .to_pandas()
    )
    
    results
    Basic vector search example

    The query is first converted to a vector and then the distance from all the other vectors is calculated, more the distance the less similar the query and text are.

    Filtering the table

    # Indexed search combined with a metadata filter
    filtered_results = (
        table.search(query_vector)
        .where("id != 2")
        .limit(2)
        .select(["id", "text", "_distance"])
        .to_pandas()
    )
    
    filtered_results
    Filtering the table – illustration

    Filtering can be perfomed to exclude or select categories or IDs. You can see the “where” condition in the code.

    Creating Vector Index

    table.create_index(
        metric="cosine",
        vector_column_name="vector",
        index_type="IVF_FLAT",
    )

    This syntax can be used to create a vector index of type IVF (inverted file index) and cosine similarity to group similar items together. You can change these parameters according to your needs.

    PDF ingestion and retrieval

    from pathlib import Path
    
    pdf_path = Path("assets/exploring-ann-algorithms.pdf")
    
    assert pdf_path.exists(), f"Expected a PDF at {pdf_path}"
    
    print(
        f"Using {pdf_path} ({pdf_path.stat().st_size} bytes)"
    )

    You can use any PDF, or you can download articles (PDF) from Analytics Vidhya for ingestion.

    reader = PdfReader(str(pdf_path))
    
    chunks = []
    
    for page_num, page in enumerate(reader.pages):
        text = (page.extract_text() or "").strip()
    
        if text:
            chunks.append({
                "page": page_num,
                "text": text,
            })
    
    print(f"Extracted text from {len(chunks)} pages")

    Extracted text from 14 pages

    from openai import OpenAI
    
    
    def embed_text(text: str) -> list[float]:
        client = OpenAI(api_key=OPENAI_API_KEY)
        response = client.embeddings.create(
            model="text-embedding-3-small",
            input=text,
        )
        return response.data[0].embedding
    
    
    pdf_rows = [
        {
            "id": f"ann-pdf-p{chunk['page']}",
            "source": pdf_path.name,
            "page": chunk["page"],
            "text": chunk["text"],
            "vector": embed_text(chunk["text"]),
        }
        for chunk in chunks
    ]
    
    pdf_table = db.create_table(
        "ann_pdf_pages",
        data=pdf_rows,
        mode="overwrite",
    )
    
    pdf_table.to_pandas()[["id", "page", "source"]]
    PDF ingestion and insertion

    Let’s use an actual embedding model to create the embeddings (text-embedding-3-small from OpenAI). We have chunked the PDF into 14 parts (each page is a chunk) and then embedded them into the vector table.

    Example Query

    # Semantic search over the PDF's pages
    query = "How does the HNSW algorithm find nearest neighbors?"
    query_vec = embed_text(query)
    
    matches = (
        pdf_table.search(query_vec)
        .limit(3)
        .select(["id", "page", "text", "_distance"])
        .to_pandas()
    )
    
    matches
    Example Query
    print(matches["text"][0])

    Output:

    How HNSW Works 1. As shown in the above image, each vertex in the graph represents a data point. 2. Connect each vertex with a configurable number of nearest vertices in a greedy manner.

    Note: You can change the chunking strategy, do it on chunk size (number of chunks) instead of splitting it into varied sized chunks.

    Multimodal embedding

    from pathlib import Path
    from PIL import Image
    
    image_files = {
        "cat": "assets/cat.jpg",
        "dog": "assets/dog.jpg",
        "horse": "assets/horse.jpg",
        "peacock": "assets/peacock.jpg",
    }
    
    images_bytes = {}
    
    for label, path in image_files.items():
        p = Path(path)
        assert p.exists(), f"Expected an image at {p}"
        images_bytes[label] = p.read_bytes()
        print(f"Loaded {label}: {path} ({len(images_bytes[label])} bytes)")
    Multimodal embedding
    from lancedb.embeddings import get_registry
    from lancedb.pydantic import LanceModel, Vector
    
    # Registers LanceDB's built-in OpenCLIP embedding function
    clip = get_registry().get("open-clip").create()
    
    
    class ImageDoc(LanceModel):
        id: str
        image_bytes: bytes = clip.SourceField()
        vector: Vector(clip.ndims()) = clip.VectorField()
    
    
    images_table = db.create_table(
        "images",
        schema=ImageDoc,
        mode="overwrite",
    )
    
    # LanceDB computes vector from image_bytes
    images_table.add([
        {"id": label, "image_bytes": data}
        for label, data in images_bytes.items()
    ])
    
    images_table.to_pandas().drop(columns=["image_bytes"])
    Multimodal embedding
    query = "a colorful bird with a fanned tail"
    
    ranked = (
        images_table.search(query)
        .select(["id", "_distance"])
        .to_pandas()
    )
    
    print(ranked)
    
    top = ranked.iloc[0]
    print(f"\nTop match: {top['id']} (distance={top['_distance']:.4f})")
    
    img = Image.open(io.BytesIO(images_bytes[top["id"]]))
    Multimodal embedding

    We’re using CLIP via LanceDB to embed the image data into the table. And as you can see it works, the query returns the peacock image as the most similar item.

    Potential Applications

    Here are some of the use-cases of LanceDB:

    • Retrieval-Augmented Generation (RAG): store document chunks and their embeddings, then pass the relevant context into an LLM.
    • Semantic and hybrid search: keyword search or keyword search plus meaning-based search together.
    • Multimodal search: search images, matching similar audio clips, pulling frames from video, all alongside structured metadata.
    • Training and feature stores: Supports datasets for training and evaluation, with schema evolution when you need to add derived features later.
    • Anomaly detection: spot duplicate (or almost duplicate) records or outliers using distance-based search.

    Conclusion

    LanceDB serves well as vector database and more. It combines vectors, metadata, and media in a single versioned, embedded table, with ANN indexing, hybrid search, and object storage support built in. That cuts out a lot of work while building a RAG and search apps. The examples here are just a starting point; things get interesting once you experiment and explore things your own way.

    Frequently Asked Questions

    Q1. Do I need to run a separate server for LanceDB?

    A. No. LanceDB runs embedded inside your application for local or object storage use.

    Q2. Which distance metric should I use?

    A. Use whatever your embedding model was trained on. Cosine or dot product are the usual picks for text and image embeddings, L2 is a fine otherwise.

    Q3. Can I filter results by metadata, not just vector similarity?

    A. Yes. Chain where(“sql_expression”) onto any search query to filter and search.

    Mounish V

    Passionate about technology and innovation, a graduate of Vellore Institute of Technology. Currently working as a Data Science Trainee, focusing on Data Science. Deeply interested in Deep Learning and Generative AI, eager to explore cutting-edge techniques to solve complex problems and create impactful solutions.

    Login to continue reading and enjoy expert-curated content.



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email

    Related Posts

    Lowering AWS KMS decrypt API costs in EMR Spark jobs

    July 30, 2026

    How NorthStar Anesthesia built a scheduling app for a workforce of 3,000 clinicians in weeks

    July 29, 2026

    Compliance Training Software: 5 Enterprise Providers

    July 28, 2026

    How to Store Petabytes of Data Without Renting It From the Cloud |

    July 27, 2026

    Data Science Case Study: The SCOPE Framework Guide

    July 26, 2026

    Why Modernizing Your CCM Environment on AWS Is Simpler Than You Think

    July 25, 2026
    Top Posts

    Understanding U-Net Architecture in Deep Learning

    November 25, 202567 Views

    The Next Paradigm in Efficient Inference Scaling – The Berkeley Artificial Intelligence Research Blog

    May 16, 202636 Views

    Hard-braking events as indicators of road segment crash risk

    January 14, 202634 Views
    Don't Miss

    Uber is building an autonomous vehicle empire, and here’s every company it’s using to do it

    August 1, 2026

    Uber has partnered with — and in some cases made direct investments in — more…

    Building the AI backbone, waiting on the AI billions

    August 1, 2026

    The End-to-End Agentic AI Pipeline

    August 1, 2026

    LanceDB Vector Database Guide: Features anndPython Demo

    August 1, 2026
    Stay In Touch
    • Facebook
    • Instagram
    About Us

    At GeekFence, we are a team of tech-enthusiasts, industry watchers and content creators who believe that technology isn’t just about gadgets—it’s about how innovation transforms our lives, work and society. We’ve come together to build a place where readers, thinkers and industry insiders can converge to explore what’s next in tech.

    Our Picks

    Uber is building an autonomous vehicle empire, and here’s every company it’s using to do it

    August 1, 2026

    Building the AI backbone, waiting on the AI billions

    August 1, 2026

    Subscribe to Updates

    Please enable JavaScript in your browser to complete this form.
    Loading
    • About Us
    • Contact Us
    • Disclaimer
    • Privacy Policy
    • Terms and Conditions
    © 2026 Geekfence.All Rigt Reserved.

    Type above and press Enter to search. Press Esc to cancel.