Complete Qdrant Tutorial: Vector Database for AI Applications
Qdrant is a high-performance vector database designed for similarity search and AI applications. It provides efficient storage and retrieval of vector embeddings, making it ideal for building recommendation systems, semantic search, and RAG applications.
Why Qdrant?
Qdrant Advantages:- High performance: Rust-based engine for speed
- Rich filtering: Combine vector search with metadata filters
- Scalable: Distributed mode for large datasets
- Easy to use: REST and gRPC APIs
- Cloud native: Docker, Kubernetes ready
- Semantic search
- Recommendation systems
- RAG (Retrieval-Augmented Generation)
- Image similarity search
- Anomaly detection
Installation
# Python client
pip install qdrant-client
Run Qdrant with Docker
docker run -p 6333:6333 -p 6334:6334 \
-v $(pwd)/qdrantstorage:/qdrant/storage:z \
qdrant/qdrant
Verify
python -c "from qdrantclient import QdrantClient; print('Qdrant client ready')"
Quick Start
1. Connect to Qdrant
from qdrantclient import QdrantClient
Local instance
client = QdrantClient("localhost", port=6333)
In-memory (for testing)
client = QdrantClient(":memory:")
Qdrant Cloud
client = QdrantClient(
url="https://xxx-xxx.us-east-1-0.aws.cloud.qdrant.io",
api
key="your-api-key"
)
Check connection
print(client.getcollections())
2. Create Collection
from qdrantclient import QdrantClient
from qdrantclient.models import Distance, VectorParams
client = QdrantClient("localhost", port=6333)
Create collection
client.createcollection(
collectionname="mycollection",
vectorsconfig=VectorParams(size=384, distance=Distance.COSINE)
)
List collections
collections = client.getcollections()
print(collections)
3. Insert Vectors
from qdrantclient.models import PointStruct
Insert points
client.upsert(
collection
name="mycollection",
points=[
PointStruct(
id=1,
vector=[0.1, 0.2, 0.3, ...], # 384-dim vector
payload={"title": "Document 1", "category": "tech"}
),
PointStruct(
id=2,
vector=[0.4, 0.5, 0.6, ...],
payload={"title": "Document 2", "category": "science"}
)
]
)
4. Search Vectors
# Search
results = client.search(
collectionname="mycollection",
queryvector=[0.1, 0.2, 0.3, ...],
limit=5
)
for result in results:
print(f"ID: {result.id}, Score: {result.score}")
print(f"Payload: {result.payload}")
Working with Embeddings
1. Using Sentence Transformers
from qdrantclient import QdrantClient
from qdrant
client.models import Distance, VectorParams, PointStruct
from sentencetransformers import SentenceTransformer
Initialize
client = QdrantClient("localhost", port=6333)
model = SentenceTransformer("all-MiniLM-L6-v2")
Create collection
client.recreatecollection(
collectionname="documents",
vectorsconfig=VectorParams(size=384, distance=Distance.COSINE)
)
Prepare documents
documents = [
{"id": 1, "text": "Machine learning is fascinating", "category": "tech"},
{"id": 2, "text": "Natural language processing", "category": "tech"},
{"id": 3, "text": "Cooking recipes for beginners", "category": "food"},
]
Generate embeddings and insert
points = []
for doc in documents:
embedding = model.encode(doc["text"]).tolist()
points.append(PointStruct(
id=doc["id"],
vector=embedding,
payload={"text": doc["text"], "category": doc["category"]}
))
client.upsert(collectionname="documents", points=points)
Search
query = "AI and deep learning"