debug_es.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. const { Client } = require('@elastic/elasticsearch');
  2. async function run() {
  3. const client = new Client({
  4. node: 'http://127.0.0.1:9200',
  5. });
  6. try {
  7. const indexName = 'knowledge_base';
  8. console.log(`\n--- Total Documents ---`);
  9. const count = await client.count({ index: indexName });
  10. console.log(count);
  11. console.log(`\n--- Document Distribution by tenantId ---`);
  12. const distribution = await client.search({
  13. index: indexName,
  14. size: 0,
  15. aggs: {
  16. by_tenant: {
  17. terms: { field: 'tenantId', size: 100, missing: 'N/A' }
  18. }
  19. }
  20. });
  21. console.log(JSON.stringify(distribution.aggregations.by_tenant.buckets, null, 2));
  22. console.log(`\n--- Sample Documents (last 5) ---`);
  23. const samples = await client.search({
  24. index: indexName,
  25. size: 5,
  26. sort: [{ createdAt: 'desc' }],
  27. });
  28. console.log(JSON.stringify(samples.hits.hits.map(h => ({
  29. id: h._id,
  30. tenantId: h._source.tenantId,
  31. fileName: h._source.fileName,
  32. vectorLength: h._source.vector?.length,
  33. vectorPreview: h._source.vector?.slice(0, 5),
  34. contentPreview: h._source.content?.substring(0, 50)
  35. })), null, 2));
  36. } catch (error) {
  37. console.error('Error:', error.meta?.body || error.message);
  38. }
  39. }
  40. run();