Skip to content

[Spring Data Cosmos] Implement Query by Example support (#45919)#48785

Draft
jeet1995 wants to merge 1 commit intoAzure:mainfrom
jeet1995:squad/45919-spring-query-by-example
Draft

[Spring Data Cosmos] Implement Query by Example support (#45919)#48785
jeet1995 wants to merge 1 commit intoAzure:mainfrom
jeet1995:squad/45919-spring-query-by-example

Conversation

@jeet1995
Copy link
Copy Markdown
Member

Issue

Fixes #45919

Feature

Implemented Query by Example (QBE) for Spring Data Cosmos, enabling dynamic queries via probe entities and \ExampleMatcher\ — a standard Spring Data feature that dramatically reduces code needed for dynamic searches.

Implementation

  1. *\CosmosExampleCriteriaBuilder* — converts \Example\ → \Criteria\ using \CosmosPersistentEntity\ metadata (not raw reflection)
    • Respects \ExampleMatcher\ settings: null handling, string matching modes, ignored paths
    • Maps non-string types to \IS_EQUAL, strings to configurable matching (contains, starts with, etc.)
    • Handles case-insensitive matching, nested objects, @Transient/@Version\ field skipping
  2. *\CosmosRepository* now extends \QueryByExampleExecutor\
  3. *\ReactiveCosmosRepository* now extends \ReactiveQueryByExampleExecutor\
  4. All methods implemented in both \SimpleCosmosRepository\ and \SimpleReactiveCosmosRepository\

Supported operations

  • \ indOne(Example)\ / \ indAll(Example)\ / \ indAll(Example, Sort)\
  • \count(Example)\ / \�xists(Example)\
  • Reactive variants returning \Mono, \Flux\

Testing

  • 16 new unit tests covering string matching modes, case insensitivity, null handling, ignored paths, version/transient field skipping, OR/AND combining
  • Build compiles clean

Community

Built upon the direction from community PR #45918 by @javiersvg, with fixes for critical bugs (wrong CriteriaType for non-strings, NPE risk, raw reflection).

…e#45919)

Add QueryByExampleExecutor<T> to CosmosRepository and
ReactiveQueryByExampleExecutor<T> to ReactiveCosmosRepository, enabling
dynamic queries via probe entities and ExampleMatcher.

New components:
- CosmosExampleCriteriaBuilder: converts Example<T> to Criteria tree
  using CosmosPersistentEntity metadata (not raw reflection). Supports
  string matching modes (exact, containing, starts/ends with),
  case-insensitive matching, null handling (INCLUDE/IGNORE), ignored
  paths, and nested object dot-notation. Skips @transient, null @id,
  and @Version fields. Falls back to CriteriaType.ALL for empty probes.

- SimpleCosmosRepository QBE methods: findOne, findAll, findAll(Sort),
  findAll(Pageable), count, exists via existing template operations.

- SimpleReactiveCosmosRepository QBE methods: reactive equivalents
  returning Mono/Flux via ReactiveCosmosOperations.

- 16 unit tests covering all ExampleMatcher configurations.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions github-actions bot added the azure-spring All azure-spring related issues label Apr 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

azure-spring All azure-spring related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE REQ] Implement query by example in spring-data-cosmos

1 participant