Skip to content

feat(sdk): add high-level Search helpers + ffi bindings#6394

Merged
bnjbvr merged 16 commits intomainfrom
bnjbvr/ui-search-iterators
Apr 20, 2026
Merged

feat(sdk): add high-level Search helpers + ffi bindings#6394
bnjbvr merged 16 commits intomainfrom
bnjbvr/ui-search-iterators

Conversation

@bnjbvr
Copy link
Copy Markdown
Contributor

@bnjbvr bnjbvr commented Apr 1, 2026

Starting to extract relevant helpers that can be merged ahead of having search spider all rooms in background.

Part of #5350.

  • I've documented the public API Changes in the appropriate CHANGELOG.md files.
  • This PR was made with the help of AI (autocomplete).

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 1, 2026

Codecov Report

❌ Patch coverage is 96.70659% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.86%. Comparing base (b066260) to head (7fbbdba).
⚠️ Report is 29 commits behind head on main.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
crates/matrix-sdk/src/message_search.rs 96.69% 3 Missing and 8 partials ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##             main    #6394    +/-   ##
========================================
  Coverage   89.85%   89.86%            
========================================
  Files         378      379     +1     
  Lines      104569   104893   +324     
  Branches   104569   104893   +324     
========================================
+ Hits        93964    94264   +300     
- Misses       6998     7012    +14     
- Partials     3607     3617    +10     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Apr 1, 2026

Merging this PR will not alter performance

✅ 50 untouched benchmarks


Comparing bnjbvr/ui-search-iterators (7fbbdba) with main (1bff8cb)

Open in CodSpeed

@bnjbvr bnjbvr force-pushed the bnjbvr/ui-search-iterators branch from 6ed213a to 5f0244b Compare April 2, 2026 11:22
@bnjbvr bnjbvr marked this pull request as ready for review April 2, 2026 11:22
@bnjbvr bnjbvr requested a review from a team as a code owner April 2, 2026 11:22
@bnjbvr bnjbvr requested review from stefanceriu and removed request for a team April 2, 2026 11:22
@bnjbvr
Copy link
Copy Markdown
Contributor Author

bnjbvr commented Apr 2, 2026

This PR is in a sufficiently good state that it would allow FFI embedders to try out the search feature, build a search UI and so on. Doesn't do automatic indexation of all content, that problem is currently left as an exercise to the reader/embedder.

@poljar poljar requested review from poljar and removed request for stefanceriu April 8, 2026 14:26
Copy link
Copy Markdown
Contributor

@poljar poljar left a comment

Choose a reason for hiding this comment

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

This looks mostly good, there is some missing documentation here and there though.

I'm also wondering if this would be better modeled as a stream, which is my main concern.

EventLoadError(#[from] matrix_sdk::Error),
}

/// An async iterator for a search query in a single room.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hmm, a bit worried to call this an async iterator as some folks might expect this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I wasn't too worried about that (async iterators are nightly only and have been for a while, so I wouldn't expect them to become stable any time soon?), but I like the idea of being able to add stream combinators much better 🥳

Comment on lines +23 to +24
#[derive(thiserror::Error, Debug)]
pub enum SearchError {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Missing docs on the type and enum variants.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Since both error variants are transparent, I don't think it's really worth documenting them, since the interesting documentation should rather be on the wrapped inner error type. And ditto for the overall error, not sure that adding "an error that happened during search" makes it clearer, but I'll add a tiny one though.

// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The module is public but there's no module-level documentation.

It would be nice to have some sort of introductory example on how this should be used in the module level docs.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Still no example? Could we please add one as it makes things for newcommers much easier.

Comment thread bindings/matrix-sdk-ffi/src/client_builder.rs
Comment thread bindings/matrix-sdk-ffi/src/error.rs Outdated
Comment thread bindings/matrix-sdk-ffi/src/search.rs Outdated

#[matrix_sdk_ffi_macros::export]
impl Room {
pub fn search(&self, query: String) -> RoomSearchIterator {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Missing docs.

In fact, a lot of the new types/functions here don't have docs.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I don't think adding docs must be a religion, when the doc only repeats the function name or states something clearly obvious 🤷

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'll add some for your pleasure, where I think it makes the most sense.

Comment thread crates/matrix-sdk-ui/src/search/mod.rs Outdated
return Ok(None);
}

let result = self.room.search(&self.query, num_results, self.offset).await?;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This function only uses the local index, which is fine for a first prototype, but it should use the homeserver for rooms that aren't encrypted.

A TODO item to record this might make sense.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

For what it's worth: I do NOT think we should use the homeserver search for rooms that aren't encrypted, otherwise we need to make sure that the behavior of local search is exactly (and not more) than that of the homeserver search. Otherwise, it would be observable that we're using one or the other implementation, according to the search results, and the room encryption state.

This extra requirement may restrict what we're able to do (after all, we could do better search locally than what the search CS APIs ought to offer, because tantivy has fancy keywords, we could index more), and it might be hard to follow the implementation-specific details, especially as they evolve (which could be super slow as adding features to the homeserver is usually a slow process; or involve implementation-specific details, and show preference to a HS implementation rather than another).

As a result, I would strongly advocate for using a local index even in the case of unencrypted rooms, otherwise the semantics of our local search API would likely be harder to define.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I agree with all the arguments why it would be better to use local search for both types of rooms.

Sadly none of them matter since you can't expect every user, and potentially every device, to locally index something like Matrix HQ. It's just not possible for storage, bandwidth, and CPU usage on the device as well as on the homeserver, reasons.

You could argue that you don't need to index the whole lot of Matrix HQ, but then you have incomplete search results. This would be strictly worse than a complete but less feature-full search implementation.

It's just not feasible to implement full-text search in this manner.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That's a fair point (although I've managed to index the whole of the "new" 1 MatrixHQ in around 30 minutes!). Will add a TODO 👍

Footnotes

  1. the v11 room created around december 25

Comment thread crates/matrix-sdk-ui/src/search/mod.rs Outdated
Comment thread crates/matrix-sdk-ui/src/search/mod.rs Outdated
/// if there are no more results.
pub async fn next(
&mut self,
num_results: usize,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I wonder if we should put this argument into the builder.

Then we could convert this into a proper Stream which would allow users to use many of the available adapters to post-process the results.

This would also then require a rename of the type, but I think that's fine as well as we wouldn't confuse people by calling something an iterator which doesn't implement Iterator or AsyncIterator.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

(I still need to address this, first.)

Comment thread crates/matrix-sdk/src/message_search.rs
@bnjbvr bnjbvr force-pushed the bnjbvr/ui-search-iterators branch 3 times, most recently from 232b5bc to 89c2da8 Compare April 14, 2026 14:03
@bnjbvr bnjbvr marked this pull request as draft April 14, 2026 14:03
@bnjbvr bnjbvr force-pushed the bnjbvr/ui-search-iterators branch from 3e7e41f to a37fdc2 Compare April 17, 2026 10:18
@bnjbvr bnjbvr marked this pull request as ready for review April 17, 2026 10:18
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Still no example? Could we please add one as it makes things for newcommers much easier.

@bnjbvr bnjbvr force-pushed the bnjbvr/ui-search-iterators branch from 0cdf60b to 7fbbdba Compare April 20, 2026 10:03
@bnjbvr bnjbvr enabled auto-merge (rebase) April 20, 2026 10:16
@bnjbvr bnjbvr merged commit 8b14a89 into main Apr 20, 2026
54 checks passed
@bnjbvr bnjbvr deleted the bnjbvr/ui-search-iterators branch April 20, 2026 10:18
@bnjbvr bnjbvr mentioned this pull request Apr 20, 2026
24 tasks
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.

2 participants