如何在单条EPL查询中匹配多个match_recognize模式
Hey there! I see you've got two working match_recognize patterns in Esper and want to combine them into a single query that returns results when either pattern is matched. Let's go through two solid approaches to do this:
Method 1: Combine Patterns in a Single match_recognize
You can use the | (OR) operator in the pattern clause to include both sequences, and use distinct variable names for each pattern's events to avoid confusion. Then, map the results to consistent output fields using conditional logic:
select * from YourEventStream match_recognize( measures -- Map the appropriate A event to the 'a' field case when A1 is not null then A1 else A2 end as a, -- Map the appropriate B event to the 'b' field case when B1 is not null then B1 else B2 end as b, -- 'c' will be null when the first pattern matches C2 as c pattern ((A1 B1) | (A2 B2 C2)) define -- First pattern's A definition A1 as A1.scene = 'stock' and A1.activity = 'assembly' and A1.task = 'picking' and A1.mod2 = 'FT' and A1.mod3 = 'Scn', -- First pattern's B definition B1 as B1.scene = 'stock' and B1.activity = 'assembly' and B1.task = 'picking' and B1.mod2 = 'PG' and B1.mod3 = 'GzS', -- Second pattern's A definition A2 as A2.scene = 'assembly' and A2.activity = 'assembly' and A2.task = 'moving' and A2.mod2 = 'GrS' and A2.mod3 = 'GzS', -- Second pattern's B definition B2 as B2.scene = 'assembly' and B2.activity = 'assembly' and B2.task = 'moving' and B2.mod2 = 'GrT' and B2.mod3 = 'Follow', -- Second pattern's C definition C2 as C2.scene = 'assembly' and C2.activity = 'assembly' and C2.task = 'moving' and C2.mod2 = 'GrT' and C2.mod3 = 'GzS' )
This approach processes the event stream once, which can be more performant. The conditional case statements ensure you get a consistent output structure regardless of which pattern matches.
Method 2: Use UNION ALL to Merge Separate Queries
If you prefer to keep your original pattern logic intact (easier to maintain if patterns get complex), you can run each match_recognize query separately and merge their results with UNION ALL. Just make sure the output fields align by adding null for missing fields:
-- First pattern query, add null for the 'c' field to match the second query's structure select a, b, null as c from YourEventStream match_recognize( measures A as a, B as b pattern (A B) define A as A.scene = 'stock' and A.activity = 'assembly' and A.task = 'picking' and A.mod2 = 'FT' and A.mod3 = 'Scn', B as B.scene = 'stock' and B.activity = 'assembly' and B.task = 'picking' and B.mod2 = 'PG' and B.mod3 = 'GzS' ) union all -- Second pattern query, uses all three fields select a, b, c from YourEventStream match_recognize( measures A as a, B as b, C as c pattern (A B C) define A as A.scene = 'assembly' and A.activity = 'assembly' and A.task = 'moving' and A.mod2 = 'GrS' and A.mod3 = 'GzS', B as B.scene = 'assembly' and B.activity = 'assembly' and B.task = 'moving' and B.mod2 = 'GrT' and B.mod3 = 'Follow', C as C.scene = 'assembly' and C.activity = 'assembly' and C.task = 'moving' and C.mod2 = 'GrT' and C.mod3 = 'GzS' )
This method keeps your original patterns untouched, making it simpler to debug or modify each pattern independently. Just note that Esper will process the event stream twice (once per query), though the engine may optimize this in practice.
Either approach will work—pick the one that best fits your maintainability and performance needs!
内容的提问来源于stack exchange,提问作者user3615089

