documentsService.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { config } from "../config/index.js";
  2. import { qdrant } from "./qdrantClient.js";
  3. export async function listDocuments({ limit, offset }) {
  4. const collectionName = config.qdrant.collection;
  5. let points;
  6. try {
  7. points = await qdrant.scroll(collectionName, {
  8. limit,
  9. offset: offset || undefined,
  10. with_payload: true,
  11. with_vector: false
  12. });
  13. } catch (err) {
  14. const status = err?.status ?? err?.statusCode ?? err?.response?.status ?? null;
  15. if (Number(status) === 404) return { items: [], nextOffset: null };
  16. throw err;
  17. }
  18. const items = (points?.points ?? []).map((p) => ({
  19. id: p.id,
  20. source: p.payload?.source ?? null,
  21. title: p.payload?.title ?? null,
  22. text: p.payload?.text ?? null,
  23. chunkIndex: p.payload?.chunkIndex ?? null,
  24. metadata: p.payload?.metadata ?? null,
  25. ingestedAt: p.payload?.ingestedAt ?? null
  26. }));
  27. const nextOffset = points?.next_page_offset ?? points?.nextPageOffset ?? null;
  28. return { items, nextOffset };
  29. }
  30. export async function deleteDocumentsBySource(source) {
  31. const collectionName = config.qdrant.collection;
  32. await qdrant.delete(collectionName, {
  33. wait: true,
  34. filter: {
  35. must: [{ key: "source", match: { value: source } }]
  36. }
  37. });
  38. }