Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(EAI-483): ingest:all command deletes pages and embeddings not in data sources #562

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export type EmbeddedContentStore = VectorStore<EmbeddedContent> & {
deleteEmbeddedContent(args: {
page?: Page;
dataSources?: string[];
inverse?: boolean;
}): Promise<void>;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ export function makeMongoDbEmbeddedContentStore({
return await embeddedContentCollection.find(pageIdentity(page)).toArray();
},

async deleteEmbeddedContent({ page, dataSources }) {
async deleteEmbeddedContent({ page, dataSources, inverse = false }) {
const deleteResult = await embeddedContentCollection.deleteMany({
...(page ? pageIdentity(page) : undefined),
...(dataSources ? { sourceName: { $in: dataSources } } : undefined),
...(dataSources ? { sourceName: { [inverse ? '$nin' : '$in']: dataSources } } : undefined),
});
if (!deleteResult.acknowledged) {
throw new Error("EmbeddedContent deletion not acknowledged!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ export function makeMongoDbPageStore({
})
);
},
async deletePages({ dataSources, permanent = false }: DeletePagesArgs) {
async deletePages({ dataSources, permanent = false, inverse = false }: DeletePagesArgs) {
const filter = {
...(dataSources ? { sourceName: { $in: dataSources } } : undefined),
...(dataSources ? { sourceName: { [inverse ? '$nin' : '$in']: dataSources } } : undefined),
};
if (permanent) {
const result = await pagesCollection.deleteMany(filter);
Expand Down
4 changes: 4 additions & 0 deletions packages/mongodb-rag-core/src/contentStore/Page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ export type DeletePagesArgs = {
rather than marking them as `"deleted"`.
*/
permanent?: boolean;
/**
* If true, delete pages that do NOT match the query.
*/
inverse?: boolean;
};

/**
Expand Down
11 changes: 11 additions & 0 deletions packages/mongodb-rag-ingest/src/commands/all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ export const doAllCommand = async (
since: lastSuccessfulRunDate ?? new Date("2023-01-01"),
});

// cleanup - delete pages and embedded content that are no longer in the data sources
await config.pageStore.deletePages({
dataSources: config.dataSources.map(({ name }) => name),
permanent: true,
inverse: true,
});
await config.embeddedContentStore.deleteEmbeddedContent({
dataSources: config.dataSources.map(({ name }) => name),
inverse: true,
});

logger.info(`Updating last successful run date`);
await ingestMetaStore.updateLastSuccessfulRunDate();
};