context-index
v0.1.2
Published
Client library for Cloudflare BM25 search API
Readme
Client
A TypeScript client library for interacting with the Cloudflare BM25 search API.
Installation
npm install cf-bm25-client
# or
yarn add cf-bm25-client
# or
pnpm add cf-bm25-clientUsage
import { ContextIndex } from 'cf-bm25-client';
const client = new ContextIndex({
baseUrl: 'https://your-cf-bm25-api.example.com',
headers: {
'Authorization': 'Bearer your-token',
},
});
// List all indexes
async function listIndexes() {
const response = await client.listIndexes();
console.log(response.data.indexes);
}
// Create a new index
async function createIndex() {
const response = await client.createIndex({ name: 'my-index' });
console.log(`Created index: ${response.data.name} with ID: ${response.data.id}`);
}
// Add a document
async function addDocument() {
const response = await client.addDocument('my-index', {
id: 'doc-1',
content: 'This is a sample document',
metadata: {
author: 'John Doe',
tags: ['sample', 'document'],
},
});
console.log('Document added:', response.data.success);
}
// Search documents
async function searchDocuments() {
const response = await client.search('my-index', 'sample document', {
topK: 10,
returnMetadata: true,
});
console.log(`Found ${response.data.count} documents`);
console.log('Matches:', response.data.matches);
}API
Index Management
listIndexes()- List all indexescreateIndex(options: CreateIndexOptions)- Create a new indexdeleteIndex(indexName: string)- Delete an indexdescribeIndex(indexName: string)- Get information about an index
Document Management
addDocument(indexName: string, document: SearchDocument)- Add a document to an indexinsertDocuments(indexName: string, documents: SearchDocument[])- Insert multiple documentsupsertDocuments(indexName: string, documents: SearchDocument[])- Upsert multiple documentsdeleteDocument(indexName: string, documentId: string)- Delete a documentdeleteDocumentsByIds(indexName: string, documentIds: string[])- Delete multiple documentsgetDocumentsByIds(indexName: string, documentIds: string[])- Get multiple documents
Search
search(indexName: string, query: string, options?: SearchOptions)- Search for documents
Metadata
addMetadata(indexName: string, documentId: string, metadata: Metadata)- Add metadata to a documentgetMetadata(indexName: string, documentId: string)- Get metadata for a document
Analysis
analyzeDocument(indexName: string, documentId: string)- Analyze a document
Types
The library exports all the necessary TypeScript types and Zod schemas:
import {
SearchDocument,
SearchResult,
SearchOptions,
Metadata,
// ... other types
} from 'cf-bm25-client';License
MIT
