Bases de datos vectoriales
4.13.2kata 2 de 4

Metadata Filtering & Hybrid Search

Combinar vector search con filtros estructurados

Metadata filtering: cada vector en el store puede llevar datos escalares (strings, números, booleans, timestamps) adjuntos. Al buscar, puedes filtrar por metadata ANTES de la búsqueda vectorial (pre-filtering) o DESPUÉS (post-filtering). Pre-filtering es más rápido porque reduce el espacio de búsqueda. Post-filtering es más preciso porque no rechaza documentos relevantes que no pasan el filtro.

Qdrant soporta filtros expresivos: must (AND), should (OR), must_not (NOT). Ejemplo: `must=[category=='legal', year >= 2024], must_not=[author=='unknown']` retorna vectores que cumplen TODAS las condiciones. El payload filtering es lo que hace productivo un vector store — sin él, buscar sobre millones de vectores es lento.

Hybrid search: algunos stores (Weaviate, Qdrant) soportan combinar búsqueda vectorial con búsqueda keyword (BM25). Retornas los top-K por vector score + top-M por keyword score, fusionas los rankings. Es poderoso cuando tienes queries con términos únicos que no aparecen en embeddings (IDs, códigos).

structure.txt
from qdrant_client.models import Filter, FieldCondition, MatchValue, Range

# Simple metadata filter: exact match
hits = qdrant.search(
    collection_name='contracts',
    query_vector=query_vec,
    query_filter=Filter(
        must=[
            FieldCondition(
                key='category',
                match=MatchValue(value='legal')
            )
        ]
    ),
    limit=5
)

# Complex filter: multiple conditions
hits = qdrant.search(
    collection_name='contracts',
    query_vector=query_vec,
    query_filter=Filter(
        must=[
            FieldCondition(key='category', match=MatchValue(value='legal')),
            FieldCondition(key='year', range=Range(gte=2023, lte=2024)),
        ],
        must_not=[
            FieldCondition(key='status', match=MatchValue(value='archived'))
        ]
    ),
    limit=5,
    score_threshold=0.72
)

Debugging lab

Detecta y corrige el error en el código.

0/3 tests passing0%
  1. 01

    # Search entire corpus without filtering results = qdrant.search( collection_name='docs', query_vector=query_vec, limit=10 )

  2. 02

    # Metadata not stored in payload qdrant.upsert( collection_name='docs', points=[PointStruct(id=i, vector=vec) for i, vec in ...] )

  3. 03

    # OR logic needed but not using should Filter( must=[ FieldCondition(key='category', match=MatchValue(value='legal')), FieldCondition(key='category', match=MatchValue(value='finance')) ] ) # impossible: category can't be both