documentsService.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. }));
  26. const nextOffset = points?.next_page_offset ?? points?.nextPageOffset ?? null;
  27. return { items, nextOffset };
  28. }
  29. export async function deleteDocumentsBySource(source) {
  30. const collectionName = config.qdrant.collection;
  31. await qdrant.delete(collectionName, {
  32. wait: true,
  33. filter: {
  34. must: [{ key: "source", match: { value: source } }]
  35. }
  36. });
  37. }