Commit graph

8 commits

Author SHA1 Message Date
Danny Avila
33ee7dea1e
🔎 fix: Specify Explicit Primary Key for Meilisearch Document Operations (#12542)
* fix: pass explicit primaryKey to Meilisearch addDocuments/updateDocuments calls

Meilisearch v1.0+ refuses to auto-infer the primary key when a document
contains multiple fields ending with 'id'. The messages index has both
conversationId and messageId, causing addDocuments to silently fail with
index_primary_key_multiple_candidates_found, leaving message search empty.

Pass { primaryKey } to addDocumentsInBatches, addDocuments, and
updateDocuments — the variable was already in scope.

Also replace raw this.collection.updateMany with Mongoose Model.updateMany
to satisfy the no-restricted-syntax ESLint rule (tenant isolation guard).

Closes #12538

* fix: resolve additional Meilisearch plugin bugs found in review

Address review findings from PR #12542:

- Fix deleteObjectFromMeili using MongoDB _id instead of the Meilisearch
  primary key (conversationId/messageId), causing post-remove cleanup to
  silently no-op and leave orphaned documents in the index.

- Pass options.primaryKey explicitly to createMeiliMongooseModel factory
  instead of deriving it from attributesToIndex[0] (schema field order),
  eliminating a fragile implicit contract.

- Fix updateObjectToMeili skipping preprocessObjectForIndex, which meant
  updates bypassed content array-to-text conversion and conversationId
  pipe character escaping.

- Change collection.updateMany to collection.updateOne in addObjectToMeili
  since _id is unique (semantic correctness).

- Add primaryKey to validateOptions required keys.

- Strengthen test assertions to verify { primaryKey } argument is passed
  to addDocuments, addDocumentsInBatches, and updateDocuments. Add tests
  for the update path including preprocessObjectForIndex pipe escaping.

* fix: add regression tests for delete and message update paths

Address follow-up review findings:

- Add test for deleteObjectFromMeili verifying it uses messageId (not
  MongoDB _id) when calling index.deleteDocument, guarding against
  regression of the silent orphaned-document bug.

- Add test for message model update path asserting { primaryKey:
  'messageId' } is passed to updateDocuments (previously only the
  conversation model update path was tested).

- Add @param config.primaryKey to createMeiliMongooseModel JSDoc.
2026-04-03 18:01:06 -04:00
Danny Avila
d74a62ecd5
🕰️ fix: Preserve updatedAt Timestamps During Meilisearch Batch Sync (#12084)
Some checks are pending
Docker Dev Images Build / build (Dockerfile, librechat-dev, node) (push) Waiting to run
Docker Dev Images Build / build (Dockerfile.multi, librechat-dev-api, api-build) (push) Waiting to run
Sync Locize Translations & Create Translation PR / Sync Translation Keys with Locize (push) Waiting to run
Sync Locize Translations & Create Translation PR / Create Translation PR on Version Published (push) Blocked by required conditions
* refactor: Add timestamps option to updateMany in createMeiliMongooseModel

- Updated the updateMany call in createMeiliMongooseModel to include a timestamps option set to false, ensuring that the operation does not modify the document's timestamps during the indexing process. This change improves the accuracy of document state management in MongoDB.

* test: Add tests to ensure updatedAt timestamps are preserved during syncWithMeili

- Introduced new test cases for the processSyncBatch function to verify that the original updatedAt timestamps on conversations and messages remain unchanged after synchronization with Meilisearch. This enhancement ensures data integrity during the indexing process.

* docs: Update comments in createMeiliMongooseModel to clarify timestamp preservation

- Enhanced comments in the createMeiliMongooseModel function to explain the use of the { timestamps: false } option in the updateMany call, ensuring that original conversation/message timestamps are preserved during the indexing process. This change improves code clarity and maintains the integrity of document timestamps.

* test: Enhance Meilisearch sync tests to verify updatedAt timestamp preservation

- Added assertions to ensure that the updatedAt timestamps of documents remain unchanged before and after synchronization with Meilisearch. This update improves the test coverage for the syncWithMeili function, reinforcing data integrity during the indexing process.
2026-03-05 10:40:43 -05:00
Andrei Blizorukov
793ddbce9f
🔎 fix: Include Legacy Documents With Undefined _meiliIndex in Search Sync (#11745)
* fix: document with undefined _meiliIndex not synced

missing property _meiliIndex is not being synced into meilisearch

* fix: updated comments to reflect changes to fix_meiliSearch property usage
2026-02-12 18:05:53 -05:00
Andrei Blizorukov
6a49861861
🔧 refactor: Offset when deleting documents during MeiliSearch cleanup (#11488)
Some checks are pending
Docker Dev Branch Images Build / build (Dockerfile, lc-dev, node) (push) Waiting to run
Docker Dev Branch Images Build / build (Dockerfile.multi, lc-dev-api, api-build) (push) Waiting to run
* 🔧 fix: adjust offset when deleting documents during MeiliSearch cleanup

This could cause the cleanup process to skip documents in subsequent batches, potentially leaving orphaned entries in MeiliSearch that no longer exist in MongoDB

* 🔧 fix: checking results count instead of total
2026-01-24 11:11:29 -05:00
Andrei Blizorukov
02d75b24a4
🛠️ fix: improved retry logic during meili sync & improved batching (#11373)
Some checks are pending
Docker Dev Images Build / build (Dockerfile, librechat-dev, node) (push) Waiting to run
Docker Dev Images Build / build (Dockerfile.multi, librechat-dev-api, api-build) (push) Waiting to run
Sync Locize Translations & Create Translation PR / Sync Translation Keys with Locize (push) Waiting to run
Sync Locize Translations & Create Translation PR / Create Translation PR on Version Published (push) Blocked by required conditions
* 🛠️ fix: unreliable retry logic during meili sync in case of interruption

🛠️ fix: exclude temporary documents from the count on startup for meili sync

🛠️ refactor: improved meili index cleanup before sync

* fix: don't swallow the exception to prevent indefinite loop

fix: update log messages for more clarity

fix: more test coverage for exception handling
2026-01-16 10:30:00 -05:00
Andrei Blizorukov
10f591ab1c
📊 refactor: Use Estimated Document Count for Meilisearch Sync (#11329)
* 🔧 refactor: use approximate number of documents to improve performance

* 🔧 refactor: unittests for approximate document count in meilisearch sync

* refactor:  limits persentage based on approximate total count & one more test case
2026-01-13 11:49:02 -05:00
Andrei Blizorukov
bed1923990
🔧 fix: Incorrect count query in meilisearch sync (#11157)
could lead to incorrect counts during sync process and documents being out of sync
2025-12-30 11:22:39 -05:00
Daniel Lew
cd5299807b
refactor: Exclude Temporary Conversations and Messages from Meilisearch Indexing (#10872)
Temporary chat data should not show up when searching.

Now we check whether a TTL has been set on a conversation/message
before indexing it in meilisearch. If there is a TTL, we skip it.
2025-12-19 10:00:41 -05:00