@@ -77,30 +77,19 @@ extension SearchableLocationDataSource {
7777 private func flattenResults( _ result: NodeResult ) -> [ NodeResult ] {
7878 let children = result. matchedChildren
7979 let totalChildren = result. node. children. count
80- let hasMatchingChildren = !children. isEmpty
81-
82- // If the current node matches:
83- // - If children also match AND a child has a better match then show children instead
84- // - Otherwise → show the parent
85- if result. matchedSelf {
86- if hasMatchingChildren, !result. bestMatchIsSelf {
87- // Collapse if ALL children matched
88- if totalChildren > 1 , children. count == totalChildren {
89- return [ result]
90- }
91- return children. flatMap ( flattenResults)
92- }
80+
81+ // Show the parent only if it matches AND it is the best match in its subtree
82+ if result. matchedSelf, result. bestMatchIsSelf {
9383 return [ result]
9484 }
9585
96- // If only one child AND parent doesn't match then skip parent
97- if result. node. children. count == 1 && !result. matchedSelf {
98- return flattenResults ( children. first!)
99- }
100-
101- // Collapse if ALL children matched
102- if !children. isEmpty && children. count == totalChildren {
103- return [ result]
86+ if !result. bestMatchIsSelf {
87+ let matchedChildren = children. filter ( { $0. score == result. score } )
88+ // Collapse if ALL children matched
89+ if matchedChildren. count > 1 && matchedChildren. count == totalChildren {
90+ return [ result]
91+ }
92+ return matchedChildren. flatMap ( flattenResults)
10493 }
10594
10695 // Repopulate node
0 commit comments