Skip to content

Allow skipCreation when adding/replacing documents#751

Open
kumarUjjawal wants to merge 3 commits intomeilisearch:mainfrom
kumarUjjawal:feat/skipCreation
Open

Allow skipCreation when adding/replacing documents#751
kumarUjjawal wants to merge 3 commits intomeilisearch:mainfrom
kumarUjjawal:feat/skipCreation

Conversation

@kumarUjjawal
Copy link
Contributor

@kumarUjjawal kumarUjjawal commented Jan 3, 2026

Pull Request

Related issue

Fixes #747

What does this PR do?

  • ...

PR checklist

Please check if your PR fulfills the following requirements:

  • Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)?
  • Have you read the contributing guidelines?
  • Have you made sure that the title is accurate and descriptive of the changes?

Thank you so much for contributing to Meilisearch!

Summary by CodeRabbit

  • New Features

    • Document management methods now accept optional parameters for primary key and a "skip creation" flag when adding or updating documents.
  • Documentation

    • Examples updated to show the new optional parameter in public API calls.
  • Tests

    • Added tests validating "skip creation" behavior (existing documents updated, new documents ignored when enabled).

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 3, 2026

📝 Walkthrough

Walkthrough

Added an optional skip_creation parameter (and preserved primary_key) to document-related public APIs; centralized documents URL construction via a new private build_documents_url helper that appends optional query params. Examples and tests updated to exercise skip_creation and propagate it to HTTP requests.

Changes

Cohort / File(s) Summary
API Examples
\.code-samples\.meilisearch\.yaml
Updated sample calls to add_or_replace / add_or_update to include the new optional skip_creation parameter (examples pass None).
Index API Implementation & Tests
src/indexes.rs, tests/...
Added private build_documents_url(host, index_uid, primary_key, skip_creation) to centralize URL construction with optional primaryKey and skipCreation query params. Updated public signatures to accept primary_key: Option<&str> and skip_creation: Option<bool> for add_or_replace, add_or_update, add_documents, add_or_update_unchecked_payload, and add_or_replace_unchecked_payload. Propagated skip_creation through wrappers and NDJSON/CSV variants. Added tests test_add_or_replace_with_skip_creation and test_add_or_update_with_skip_creation.
Client Usage Example
src/client.rs
Example call sites updated to pass the additional skip_creation argument (e.g., None).
Documentation Examples
src/documents.rs, src/search.rs
Inline examples updated to pass the new skip_creation parameter in add_or_replace usage.
Examples Apps
examples/cli-app*/src/main.rs
Call sites updated for add_or_update to include the new optional skip_creation argument (e.g., None).

Sequence Diagram(s)

sequenceDiagram
    actor Client
    participant IndexModule as src/indexes.rs
    participant HTTP as HTTP Client
    participant Meili as Meilisearch Server

    Client->>IndexModule: add_or_replace(docs, primary_key?, skip_creation?)
    IndexModule->>IndexModule: build_documents_url(host, uid, primary_key, skip_creation)
    IndexModule->>HTTP: POST /indexes/{uid}/documents?primaryKey=...&skipCreation=...
    HTTP->>Meili: forward HTTP request
    Meili-->>HTTP: response (TaskInfo)
    HTTP-->>IndexModule: return response
    IndexModule-->>Client: return TaskInfo
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 I hopped through code with nimble cheer,
A skipCreation flag now nestles near.
URLs built tidy, tests in a row,
Docs and examples now ready to go—
A carrot-coded tweak, small and clear.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Allow skipCreation when adding/replacing documents' directly and accurately describes the primary change: adding support for the skipCreation parameter to document operations.
Linked Issues check ✅ Passed All requirements from issue #747 are met: skipCreation parameter added to document API methods [#747], test cases updated [#747], and examples in .code-samples.meilisearch.yaml updated [#747].
Out of Scope Changes check ✅ Passed All changes are scoped to adding skipCreation support to document operations; no unrelated modifications detected across the updated files.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

📜 Recent review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f4d3b7a and 3a5bb11.

📒 Files selected for processing (2)
  • examples/cli-app-with-awc/src/main.rs
  • examples/cli-app/src/main.rs
🧰 Additional context used
🧬 Code graph analysis (1)
examples/cli-app/src/main.rs (1)
src/client.rs (4)
  • None (1541-1541)
  • None (1584-1584)
  • None (1587-1587)
  • None (1680-1680)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: integration-tests
🔇 Additional comments (2)
examples/cli-app/src/main.rs (1)

113-113: LGTM!

The addition of the third parameter (None for skip_creation) correctly updates the example to use the new API signature while maintaining the existing behavior.

examples/cli-app-with-awc/src/main.rs (1)

189-189: LGTM!

The addition of the third parameter (None for skip_creation) correctly updates the example to use the new API signature while preserving the existing behavior.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
.code-samples.meilisearch.yaml (1)

163-163: Consider adding examples that demonstrate skip_creation usage.

The documentation currently shows the default behavior (None, None). Consider adding additional examples demonstrating the skip_creation parameter in action to help users understand when and how to use it.

Suggested additional example
add_or_replace_documents_skip_creation_example: |-
  // Only update existing documents, skip creating new ones
  let task: TaskInfo = client
    .index("movies")
    .add_or_replace(&[
      Movie {
        id: 287947,
        title: "Shazam ⚡️ - Updated".to_string(),
        // ... other fields
      }
    ], None, Some(true))  // primary_key, skip_creation=true
    .await
    .unwrap();

Also applies to: 183-183

📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1a4fbb3 and b68ebc4.

📒 Files selected for processing (2)
  • .code-samples.meilisearch.yaml
  • src/indexes.rs
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: integration-tests
🔇 Additional comments (5)
src/indexes.rs (5)

645-663: LGTM: Breaking change properly implemented.

The addition of the skip_creation parameter is a breaking change for users calling this method with positional arguments. However, this is the idiomatic Rust approach for adding optional parameters. Ensure this breaking change is clearly documented in the release notes and that the SDK version is appropriately bumped (likely a minor or major version increment per semantic versioning).


969-987: LGTM: Consistent implementation.

The add_or_update method follows the same pattern as add_or_replace. The implementation is consistent and correct.


727-733: LGTM: Wrapper methods maintain backward compatibility.

All wrapper methods correctly pass None for the skip_creation parameter, ensuring existing code paths continue to work as before.

Also applies to: 772-779, 818-825, 863-870, 908-915, 1722-1725


2293-2366: LGTM: Comprehensive test coverage for skip_creation.

Both test cases validate the expected behavior:

  • test_add_or_replace_with_skip_creation confirms existing documents are updated while new documents are skipped
  • test_add_or_update_with_skip_creation confirms partial updates work correctly with skip_creation, preserving fields not in the update payload

2270-2270: LGTM: Test updated for new signature.

The existing test has been correctly updated to work with the new method signature.

Comment on lines +16 to +38
/// Builds the URL for document addition/update endpoints with optional query parameters.
fn build_documents_url(
host: &str,
index_uid: &str,
primary_key: Option<&str>,
skip_creation: Option<bool>,
) -> String {
let base_url = format!("{}/indexes/{}/documents", host, index_uid);
let mut query_params = Vec::new();

if let Some(pk) = primary_key {
query_params.push(format!("primaryKey={}", pk));
}
if skip_creation == Some(true) {
query_params.push("skipCreation=true".to_string());
}

if query_params.is_empty() {
base_url
} else {
format!("{}?{}", base_url, query_params.join("&"))
}
}
Copy link
Contributor

@coderabbitai coderabbitai bot Jan 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

URL-encode the primary_key parameter.

The primary_key value is inserted directly into the query string without URL encoding. If a primary key contains special characters (spaces, &, =, etc.), this will produce a malformed URL.

🔎 Proposed fix using URL encoding
 fn build_documents_url(
     host: &str,
     index_uid: &str,
     primary_key: Option<&str>,
     skip_creation: Option<bool>,
 ) -> String {
     let base_url = format!("{}/indexes/{}/documents", host, index_uid);
     let mut query_params = Vec::new();
 
     if let Some(pk) = primary_key {
-        query_params.push(format!("primaryKey={}", pk));
+        query_params.push(format!("primaryKey={}", urlencoding::encode(pk)));
     }
     if skip_creation == Some(true) {
         query_params.push("skipCreation=true".to_string());
     }
 
     if query_params.is_empty() {
         base_url
     } else {
         format!("{}?{}", base_url, query_params.join("&"))
     }
 }

Note: You may need to add the urlencoding crate as a dependency.

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In src/indexes.rs around lines 16 to 38, the primary_key is being interpolated
directly into the query string which can break URLs when it contains reserved
characters; update the function to percent-encode the primary_key before
appending it as a query parameter (e.g., use a URL-encoding helper such as
urlencoding::encode or the percent-encoding utilities from the url crate), add
the chosen crate to Cargo.toml, and replace the direct insertion with the
encoded value so the generated URL is always valid.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not required

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kumarUjjawal, understood. I've noted your decision.

@curquiza
Copy link
Member

curquiza commented Jan 8, 2026

@kumarUjjawal can you fix the tests in the CI?

@kumarUjjawal
Copy link
Contributor Author

@kumarUjjawal can you fix the tests in the CI?

Fixed it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Meilisearch v1.31] Allow skipCreation when adding/replacing documents

2 participants