/** * Creates the kNN vector index in the OpenSearch Serverless collection. * * Run this ONCE after `cdk deploy` creates the AOSS collection but BEFORE * triggering a Bedrock Knowledge Base sync/ingestion job. * * Usage: * npx ts-node scripts/create-aoss-index.ts * * The collection endpoint is printed as a stack output after deploy: * npx cdk deploy --outputs-file outputs.json * cat outputs.json * * Requirements: * npm install --save-dev @opensearch-project/opensearch aws4 ts-node typescript * AWS credentials must have aoss:APIAccessAll on the collection. */ import { Client } from '@opensearch-project/opensearch'; import { AwsSigv4Signer } from '@opensearch-project/opensearch/aws'; import { defaultProvider } from '@aws-sdk/credential-provider-node'; const INDEX_NAME = 'seahaven-kb-index'; // Field names must match exactly what's configured in knowledge-base.ts const INDEX_BODY = { settings: { 'index.knn': true, }, mappings: { properties: { // Titan Embed Text v2 with 1024 dimensions 'bedrock-knowledge-base-default-vector': { type: 'knn_vector', dimension: 1024, method: { name: 'hnsw', space_type: 'l2', engine: 'faiss', parameters: { ef_construction: 512, m: 16 }, }, }, AMAZON_BEDROCK_TEXT_CHUNK: { type: 'text' }, AMAZON_BEDROCK_METADATA: { type: 'text', index: false }, }, }, }; async function main(): Promise { const endpoint = process.argv[2]; if (!endpoint) { console.error('Usage: npx ts-node scripts/create-aoss-index.ts '); console.error('Example: npx ts-node scripts/create-aoss-index.ts https://abc123.us-east-1.aoss.amazonaws.com'); process.exit(1); } const client = new Client({ ...AwsSigv4Signer({ region: 'us-east-1', service: 'aoss', getCredentials: defaultProvider(), }), node: endpoint, }); console.log(`Creating index "${INDEX_NAME}" on ${endpoint} …`); const exists = await client.indices.exists({ index: INDEX_NAME }); if (exists.statusCode === 200) { console.log(`Index "${INDEX_NAME}" already exists — nothing to do.`); return; } const res = await client.indices.create({ index: INDEX_NAME, body: INDEX_BODY, }); console.log('Index created:', JSON.stringify(res.body, null, 2)); console.log('\nNext step: trigger a sync from the Bedrock console or run:'); console.log(' aws bedrock-agent start-ingestion-job --knowledge-base-id --data-source-id '); } main().catch((err) => { console.error('Failed to create index:', err); process.exit(1); });