Experience hybrid search with the SDK
This guide uses a Linux environment to show how to try hybrid search quickly with embedded seekdb using pyseekdb (OceanBase's Python client) or seekdb-js (OceanBase's Node client).
Besides Linux, pyseekdb also runs on macOS and Windows. On Windows, only server-mode seekdb is supported today. For macOS and Windows setup, see pyseekdb quick start and seekdb-js quick start.
In this example, you will:
- Install pyseekdb or seekdb-js and deploy embedded seekdb.
- Connect to seekdb and create a database.
- Connect to the database and create a collection with embedding functions.
- Add data with
documents(vectors are generated automatically). - Run hybrid search (query vectors are generated automatically) and print results.
- Clean up the environment.
Background information
pyseekdb
pyseekdb is OceanBase's Python client for seekdb. It exposes one API surface and supports three connection modes: embedded seekdb, server-mode seekdb, and OceanBase Database.
Installing the client also installs embedded seekdb so you can connect directly to embedded seekdb and create databases, or connect remotely to an existing server-mode seekdb or OceanBase deployment.
seekdb-js
seekdb-js is OceanBase's Node client for seekdb. It exposes one API surface and supports three connection modes: embedded seekdb, server-mode seekdb, and OceanBase Database.
Installing the client also installs embedded seekdb so you can connect directly to embedded seekdb and create databases, or connect remotely to an existing server-mode seekdb or OceanBase deployment.
seekdb deployment modes
seekdb supports flexible deployment modes, from quick prototypes to workloads that serve large user bases.
-
Embedded mode
seekdb runs as a lightweight library inside your application and installs in one step. It suits learning, rapid prototyping, and efficient runs on many edge-class devices.
-
Server mode
Recommended for test and production. Lightweight and easy to operate for stable, efficient service.
For server mode, see Experience hybrid search with SQL.
- pyseekdb
- seekdb-js
Step 1: Install pyseekdb and deploy embedded seekdb
Prerequisites
Make sure your environment meets the following requirements:
- OS: Linux (glibc >= 2.28)
- Python: 3.11 or later
- Architecture: x86_64 or aarch64
Install
Install with pip. It uses your default Python version and platform.
pip install -U pyseekdb
If pip is outdated, upgrade it first:
pip install --upgrade pip
Installing pyseekdb also installs embedded seekdb so you can connect directly to embedded seekdb and create databases.
Step 2: Connect to seekdb and create a database
Use the Admin Client to connect to seekdb and create a database named hybrid_search_test.
-
For more on the Admin Client, see Admin Client.
-
For creating a database, see create_database - Create a database.
import pyseekdb
# Create embedded admin client
admin = pyseekdb.AdminClient(path="./seekdb.db")
# Create database
admin.create_database("hybrid_search_test")
Step 3: Connect to the database and create a collection with embedding functions
Use Client to connect to the hybrid_search_test database and create a collection.
- For the Client API, see Client.
- For creating a collection, see create_collection - Create a collection.
import pyseekdb
# Create embedded client
client = pyseekdb.Client(path="./seekdb.db", database="hybrid_search_test")
# Create collection
collection = client.create_collection(
name="hybrid_search_demo"
)
Step 4: Insert data
Use add to insert data into the collection.
For the add API, see add - Insert data.
import pyseekdb
# Create embedded client
client = pyseekdb.Client(path="./seekdb.db", database="hybrid_search_test")
# get collection
collection = client.get_collection("hybrid_search_demo")
# Define documents
documents = [
"Machine learning is revolutionizing artificial intelligence and data science",
"Python programming language is essential for machine learning developers",
"Deep learning neural networks enable advanced AI applications",
"Data science combines statistics, programming, and domain expertise",
"Natural language processing uses machine learning to understand text",
"Computer vision algorithms process images using deep learning techniques",
"Reinforcement learning trains agents through reward-based feedback",
"Python libraries like TensorFlow and PyTorch simplify machine learning",
"Artificial intelligence systems can learn from large datasets",
"Neural networks mimic the structure of biological brain connections"
]
# Define metadatas
metadatas = [
{"category": "AI", "topic": "machine learning", "year": 2023, "popularity": 95},
{"category": "Programming", "topic": "python", "year": 2023, "popularity": 88},
{"category": "AI", "topic": "deep learning", "year": 2024, "popularity": 92},
{"category": "Data Science", "topic": "data analysis", "year": 2023, "popularity": 85},
{"category": "AI", "topic": "nlp", "year": 2024, "popularity": 90},
{"category": "AI", "topic": "computer vision", "year": 2023, "popularity": 87},
{"category": "AI", "topic": "reinforcement learning", "year": 2024, "popularity": 89},
{"category": "Programming", "topic": "python", "year": 2023, "popularity": 91},
{"category": "AI", "topic": "general ai", "year": 2023, "popularity": 93},
{"category": "AI", "topic": "neural networks", "year": 2024, "popularity": 94}
]
ids = [f"doc_{i+1}" for i in range(len(documents))]
# Insert data
collection.add(ids=ids, documents=documents, metadatas=metadatas)
Step 5: Run hybrid search and print results
Use hybrid_search to run hybrid search and print the results.
For hybrid search, see hybrid_search - Hybrid search.
import pyseekdb
# Create embedded client
client = pyseekdb.Client(path="./seekdb.db", database="hybrid_search_test")
# get collection
collection = client.get_collection("hybrid_search_demo")
# Perform hybrid search
hybrid_result = collection.hybrid_search(
query={"where_document": {"$contains": "machine learning"}, "n_results": 10},
knn={"query_texts": ["AI research"], "n_results": 10},
rank={"rrf": {}},
n_results=5
)
# Print results
print("\nhybrid_search() Results:")
print(f" ids: {hybrid_result ['ids'][0]}")
print(f" Document: {hybrid_result ['documents'][0]}")
Step 6: Clean up
When you no longer need the sample database and collection, use delete_collection to remove the collection and delete_database to remove the database.
-
For deleting a collection, see delete_collection - Delete a collection.
-
For deleting a database, see delete_database - Delete a database.
import pyseekdb
# Create embedded client
admin = pyseekdb.AdminClient(path="./seekdb.db")
client = pyseekdb.Client(path="./seekdb.db", database="hybrid_search_test")
# Delete collection
client.delete_collection("hybrid_search_demo")
print(f"\nDeleted collection")
# Delete database
admin.delete_database("hybrid_search_test")
print(f"\nDeleted database")
Complete example
import pyseekdb
#==================== Create Database ====================
# Create embedded admin client
admin = pyseekdb.AdminClient(path="./seekdb.db")
# Create database
admin.create_database("hybrid_search_test")
# ==================== Create Collection ====================
# Create embedded client
client = pyseekdb.Client(path="./seekdb.db", database="hybrid_search_test")
# Create collection
collection = client.create_collection(
name="hybrid_search_demo"
)
# ==================== Add Data to Collection ====================
# Define documents
documents = [
"Machine learning is revolutionizing artificial intelligence and data science",
"Python programming language is essential for machine learning developers",
"Deep learning neural networks enable advanced AI applications",
"Data science combines statistics, programming, and domain expertise",
"Natural language processing uses machine learning to understand text",
"Computer vision algorithms process images using deep learning techniques",
"Reinforcement learning trains agents through reward-based feedback",
"Python libraries like TensorFlow and PyTorch simplify machine learning",
"Artificial intelligence systems can learn from large datasets",
"Neural networks mimic the structure of biological brain connections"
]
# Define metadatas
metadatas = [
{"category": "AI", "topic": "machine learning", "year": 2023, "popularity": 95},
{"category": "Programming", "topic": "python", "year": 2023, "popularity": 88},
{"category": "AI", "topic": "deep learning", "year": 2024, "popularity": 92},
{"category": "Data Science", "topic": "data analysis", "year": 2023, "popularity": 85},
{"category": "AI", "topic": "nlp", "year": 2024, "popularity": 90},
{"category": "AI", "topic": "computer vision", "year": 2023, "popularity": 87},
{"category": "AI", "topic": "reinforcement learning", "year": 2024, "popularity": 89},
{"category": "Programming", "topic": "python", "year": 2023, "popularity": 91},
{"category": "AI", "topic": "general ai", "year": 2023, "popularity": 93},
{"category": "AI", "topic": "neural networks", "year": 2024, "popularity": 94}
]
ids = [f"doc_{i+1}" for i in range(len(documents))]
# Insert data
collection.add(ids=ids, documents=documents, metadatas=metadatas)
# ==================== Perform Hybrid Search ====================
# Perform hybrid search
hybrid_result = collection.hybrid_search(
query={"where_document": {"$contains": "machine learning"}, "n_results": 10},
knn={"query_texts": ["AI research"], "n_results": 10},
rank={"rrf": {}},
n_results=5
)
# ==================== Print Query Results ====================
# Print results
print("\nhybrid_search() Results:")
print(f" ids: {hybrid_result ['ids'][0]}")
print(f" Document: {hybrid_result ['documents'][0]}")
# ==================== Cleanup ====================
# Delete collection
client.delete_collection("hybrid_search_demo")
print(f"\nDeleted collection")
# Delete database
admin.delete_database("hybrid_search_test")
print(f"\nDeleted database")
Step 1: Install seekdb-js and deploy embedded seekdb
Prerequisites
Make sure your environment meets the following requirements:
- OS: Linux, macOS (Apple Silicon)
- Node.js: 20 or later
- Architecture: x86_64 or aarch64
Install
Install with npm.
npm install seekdb @seekdb/default-embed
Installing seekdb-js also installs embedded seekdb so you can connect directly to embedded seekdb and create databases.
Step 2: Connect to seekdb and create a database
Use SeekdbAdminClient to connect to seekdb and create a database named hybrid_search_test.
-
For more on
SeekdbAdminClient, see SeekdbAdminClient. -
For creating a database, see createDatabase - Create a database.
import { SeekdbAdminClient } from "seekdb";
// Create embedded admin client
const admin = new SeekdbAdminClient({ path: "./seekdb.db" });
// Create database
await admin.createDatabase("hybrid_search_test");
Step 3: Connect to the database and create a collection with embedding functions
Use SeekdbClient to connect to the hybrid_search_test database and create a collection.
- For more on
SeekdbClient, see SeekdbClient. - For creating a collection, see createCollection - Create a collection.
import { SeekdbClient } from "seekdb";
// Create embedded client
const client = new SeekdbClient({
path: "./seekdb.db",
database: "hybrid_search_test",
});
// Create collection
const collection = await client.createCollection({ name: "hybrid_search_demo" });
Step 4: Insert data
Use add to insert data into the collection.
For the add API, see add - Insert data.
import { SeekdbClient } from "seekdb";
// Create embedded client
const client = new SeekdbClient({
path: "./seekdb.db",
database: "hybrid_search_test",
});
// get collection
const collection = await client.getCollection({
name: 'hybrid_search_demo'
});
// Define documents
const documents = [
"Machine learning is revolutionizing artificial intelligence and data science",
"Python programming language is essential for machine learning developers",
"Deep learning neural networks enable advanced AI applications",
"Data science combines statistics, programming, and domain expertise",
"Natural language processing uses machine learning to understand text",
"Computer vision algorithms process images using deep learning techniques",
"Reinforcement learning trains agents through reward-based feedback",
"Python libraries like TensorFlow and PyTorch simplify machine learning",
"Artificial intelligence systems can learn from large datasets",
"Neural networks mimic the structure of biological brain connections",
];
// Define metadatas
const metadatas = [
{ category: "AI", topic: "machine learning", year: 2023, popularity: 95 },
{ category: "Programming", topic: "python", year: 2023, popularity: 88 },
{ category: "AI", topic: "deep learning", year: 2024, popularity: 92 },
{
category: "Data Science",
topic: "data analysis",
year: 2023,
popularity: 85,
},
{ category: "AI", topic: "nlp", year: 2024, popularity: 90 },
{ category: "AI", topic: "computer vision", year: 2023, popularity: 87 },
{
category: "AI",
topic: "reinforcement learning",
year: 2024,
popularity: 89,
},
{ category: "Programming", topic: "python", year: 2023, popularity: 91 },
{ category: "AI", topic: "general ai", year: 2023, popularity: 93 },
{ category: "AI", topic: "neural networks", year: 2024, popularity: 94 },
];
const ids = documents.map((_, i) => `doc_${i + 1}`);
// Insert data
await collection.add({ ids, documents, metadatas });
Step 5: Run hybrid search and print results
Use hybridSearch to run hybrid search and print the results.
For hybrid search, see hybridSearch - Hybrid search.
import { SeekdbClient } from "seekdb";
// Create embedded client
const client = new SeekdbClient({
path: "./seekdb.db",
database: "hybrid_search_test",
});
// get collection
const collection = await client.getCollection({
name: 'hybrid_search_demo'
});
// Perform hybrid search
const hybridResult1 = await collection.hybridSearch({
query: { whereDocument: { $contains: "machine learning" }, nResults: 10 },
knn: { queryTexts: ["AI research"], nResults: 10 },
rank: { rrf: {} },
nResults: 5,
});
// Print results
console.log("\nhybridSearch() Results:");
for (let i = 0; i < hybridResult1.ids[0].length; i++) {
const docId = hybridResult1.ids[0][i];
const idx = ids.indexOf(docId);
console.log(` ${i + 1}. ${documents[idx]}`);
}
Step 6: Clean up
When you no longer need the sample database and collection, use deleteCollection to remove the collection and deleteDatabase to remove the database.
-
For deleting a collection, see deleteCollection - Delete a collection.
-
For deleting a database, see deleteDatabase - Delete a database.
import { SeekdbAdminClient } from "seekdb";
import { SeekdbClient } from "seekdb";
// Create embedded client
const admin = new SeekdbAdminClient({ path: "./seekdb.db" });
const client = new SeekdbClient({
path: "./seekdb.db",
database: "hybrid_search_test",
});
// Delete collection
await client.deleteCollection('hybrid_search_demo');
console.log('\nDeleted collection')
// Delete database
await admin.deleteDatabase("hybrid_search_test");
console.log('\nDeleted database')
Complete example
import { SeekdbAdminClient } from "seekdb";
import { SeekdbClient } from "seekdb";
//==================== Create Database ====================
// Create embedded admin client
const admin = new SeekdbAdminClient({ path: "./seekdb.db" });
// Create database
await admin.createDatabase("hybrid_search_test");
// ==================== Create Collection ====================
// Create embedded client
const client = new SeekdbClient({
path: "./seekdb.db",
database: "hybrid_search_test",
});
// Create collection
const collection = await client.createCollection({ name: "hybrid_search_demo" });
// ==================== Add Data to Collection ====================
// Define documents
const documents = [
"Machine learning is revolutionizing artificial intelligence and data science",
"Python programming language is essential for machine learning developers",
"Deep learning neural networks enable advanced AI applications",
"Data science combines statistics, programming, and domain expertise",
"Natural language processing uses machine learning to understand text",
"Computer vision algorithms process images using deep learning techniques",
"Reinforcement learning trains agents through reward-based feedback",
"Python libraries like TensorFlow and PyTorch simplify machine learning",
"Artificial intelligence systems can learn from large datasets",
"Neural networks mimic the structure of biological brain connections",
];
// Define metadatas
const metadatas = [
{ category: "AI", topic: "machine learning", year: 2023, popularity: 95 },
{ category: "Programming", topic: "python", year: 2023, popularity: 88 },
{ category: "AI", topic: "deep learning", year: 2024, popularity: 92 },
{
category: "Data Science",
topic: "data analysis",
year: 2023,
popularity: 85,
},
{ category: "AI", topic: "nlp", year: 2024, popularity: 90 },
{ category: "AI", topic: "computer vision", year: 2023, popularity: 87 },
{
category: "AI",
topic: "reinforcement learning",
year: 2024,
popularity: 89,
},
{ category: "Programming", topic: "python", year: 2023, popularity: 91 },
{ category: "AI", topic: "general ai", year: 2023, popularity: 93 },
{ category: "AI", topic: "neural networks", year: 2024, popularity: 94 },
];
const ids = documents.map((_, i) => `doc_${i + 1}`);
// Insert data
await collection.add({ ids, documents, metadatas });
// ==================== Perform Hybrid Search ====================
// Perform hybrid search
const hybridResult1 = await collection.hybridSearch({
query: { whereDocument: { $contains: "machine learning" }, nResults: 10 },
knn: { queryTexts: ["AI research"], nResults: 10 },
rank: { rrf: {} },
nResults: 5,
});
// ==================== Print Query Results ====================
// Print results
console.log("\nhybridSearch() Results:");
for (let i = 0; i < hybridResult1.ids[0].length; i++) {
const docId = hybridResult1.ids[0][i];
const idx = ids.indexOf(docId);
console.log(` ${i + 1}. ${documents[idx]}`);
}
// ==================== Cleanup ====================
// Delete collection
await client.deleteCollection('hybrid_search_demo');
console.log('\nDeleted collection')
// Delete database
await admin.deleteDatabase("hybrid_search_test");
console.log('\nDeleted database')
More information
-
For a deeper introduction to pyseekdb, see pyseekdb quick start.
-
For more pyseekdb examples, see:
-
Simple sample: demonstrates vector search with pyseekdb.
-
Complete sample: demonstrates the full set of capabilities supported by pyseekdb today.
-
Hybrid search sample: demonstrates seekdb hybrid search and compares it with vector search.
-
For more seekdb-js examples, see:
-
Simple sample: demonstrates vector search with seekdb-js.
-
Complete sample: demonstrates the full set of capabilities supported by seekdb-js today.
-
Hybrid search sample: demonstrates seekdb hybrid search and compares it with vector search.
-
Beyond the Python SDK and seekdb-js, seekdb also supports SQL. For SQL-based workflows, see Experience vector search with SQL and Experience hybrid search with SQL.