You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _posts/2025-11-29-eventstore-querying-events.md
+367-1Lines changed: 367 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,6 +10,153 @@ tags: [events,query,tags]
10
10
11
11
This guide covers the various ways to query events from the EventStore, including filtering, pagination, backward queries, temporal queries, and cross-stream querying.
12
12
13
+
## EventQuery Concept
14
+
15
+
An `EventQuery` is the fundamental mechanism for selecting events from the EventStore. It defines matching criteria based on event types and tags.
16
+
17
+
**An event matches an EventQuery if:**
18
+
1. The event's type is **any** of the types allowed by the query (OR condition)
19
+
2.**All** tags specified by the query are present on the event (AND condition)
20
+
21
+
Events may have additional tags beyond those specified in the query—the query only requires that the specified tags are present.
22
+
23
+
```java
24
+
// Query for CustomerRegistered OR CustomerUpdated events with region=EU tag
**Important:** While these two approaches are functionally equivalent (they return the same events), the in-process approach suffers from significant performance issues because all events must be retrieved from the database before filtering.
Sometimes it's beneficial to retrieve a limited set of events from the database and then apply multiple fine-grained filters in Java. This allows you to reuse query results for multiple objectives without running multiple similar database queries:
- Historical events matching the upcasted target type are automatically included
348
495
- The upcasting is transparent—application code never sees historical event types
349
-
- No special handling needed in query logic for legacy events
496
+
- No special handling needed in query logic for legacy events
497
+
498
+
## Complex Event Queries
499
+
500
+
For advanced scenarios, you can combine multiple query criteria using the `combineWith()` method. This creates a UNION of queries, allowing you to retrieve events that match any of several different patterns.
501
+
502
+
### Understanding Query Matching Semantics
503
+
504
+
Event queries follow specific matching rules that combine AND and OR logic:
505
+
506
+
**Within a single query item:**
507
+
-**Event Types**: The event must match **ANY** of the specified types (OR condition)
508
+
-**Tags**: The event must contain **ALL** specified tags (AND condition)
509
+
510
+
**Across multiple query items:**
511
+
- If **ANY** item matches, the event matches the overall query (OR condition)
512
+
513
+
This gives you powerful flexibility to express complex selection criteria.
514
+
515
+
### Basic Query Combination
516
+
517
+
Combine two queries to match events that satisfy either query:
518
+
519
+
```java
520
+
// Query 1: All CustomerRegistered events
521
+
EventQuery newCustomers =EventQuery.forEvents(
522
+
EventTypesFilter.of(CustomerRegistered.class),
523
+
Tags.none()
524
+
);
525
+
526
+
// Query 2: All events for VIP customers
527
+
EventQuery vipActivity =EventQuery.forEvents(
528
+
EventTypesFilter.any(),
529
+
Tags.of("customerType", "VIP")
530
+
);
531
+
532
+
// Combined: CustomerRegistered events OR any VIP customer events
This pattern ensures that if **any** new relevant fact emerges (either about the student or the course) between reading facts and appending the new event, the append will fail with an `OptimisticLockingException`.
656
+
657
+
### Matching Examples
658
+
659
+
To clarify the matching semantics, consider these examples:
660
+
661
+
**Example 1: Simple combination**
662
+
663
+
```java
664
+
EventQuery q =EventQuery.forEvents(
665
+
EventTypesFilter.of(CustomerRegistered.class),
666
+
Tags.of("region", "EU")
667
+
).combineWith(
668
+
EventQuery.forEvents(
669
+
EventTypesFilter.of(OrderPlaced.class),
670
+
Tags.of("priority", "high")
671
+
)
672
+
);
673
+
```
674
+
675
+
This matches events where:
676
+
- Event type is `CustomerRegistered` AND has tag `region=EU`, **OR**
677
+
- Event type is `OrderPlaced` AND has tag `priority=high`
0 commit comments