const { Client } = require('@elastic/elasticsearch');

async function run() {
    const client = new Client({
        node: 'http://127.0.0.1:9200',
    });

    try {
        const indexName = 'knowledge_base';

        console.log(`\n--- Total Documents ---`);
        const count = await client.count({ index: indexName });
        console.log(count);

        console.log(`\n--- Document Distribution by tenantId ---`);
        const distribution = await client.search({
            index: indexName,
            size: 0,
            aggs: {
                by_tenant: {
                    terms: { field: 'tenantId', size: 100, missing: 'N/A' }
                }
            }
        });
        console.log(JSON.stringify(distribution.aggregations.by_tenant.buckets, null, 2));

        console.log(`\n--- Sample Documents (last 5) ---`);
        const samples = await client.search({
            index: indexName,
            size: 5,
            sort: [{ createdAt: 'desc' }],
        });
        console.log(JSON.stringify(samples.hits.hits.map(h => ({
            id: h._id,
            tenantId: h._source.tenantId,
            fileName: h._source.fileName,
            vectorLength: h._source.vector?.length,
            vectorPreview: h._source.vector?.slice(0, 5),
            contentPreview: h._source.content?.substring(0, 50)
        })), null, 2));

    } catch (error) {
        console.error('Error:', error.meta?.body || error.message);
    }
}

run();
