Jump to content

Introducing flutter tantivy: Bringing Rust-Powered Full-Text Search to Flutter

From JOHNWICK

Search is everywhere. From finding the right email in your inbox to discovering products in an e-commerce app, full-text search has become an essential feature in modern applications. But implementing high-performance search on mobile devices? That’s a different challenge altogether. Today, I’m excited to share flutter_tantivy — a Flutter plugin that brings the blazing-fast Tantivy search engine, written in Rust, directly to your Flutter applications.

The Problem with Mobile Search Mobile developers face a unique set of challenges when implementing search functionality:

  • Performance constraints: Mobile devices have limited resources compared to servers
  • Offline-first requirements: Users expect search to work without an internet connection
  • Battery efficiency: Search operations shouldn’t drain the battery
  • Cross-platform consistency: The same search experience should work across iOS, Android, and desktop platforms

Most existing solutions either rely on cloud-based search (requiring constant connectivity) or use less efficient native implementations that compromise on speed and features. Enter Tantivy: The Rust-Powered Search Engine Tantivy is a full-text search engine library written in Rust, inspired by Apache Lucene. It’s designed to be:

  • Fast: Rust’s zero-cost abstractions and memory safety guarantees mean excellent performance
  • Efficient: Optimized indexing and search algorithms
  • Feature-rich: Supports complex queries, relevance scoring, and more
  • Battle-tested: Used in production by companies running serious workloads

But Tantivy is a Rust library. How do we use it in Flutter? That’s where flutter_rust_bridge comes in. Bridging Two Worlds: Flutter ❤️ Rust The flutter_tantivy plugin leverages flutter_rust_bridge to create seamless interoperability between Dart and Rust. This means you get:

  • Native performance: Direct FFI calls with minimal overhead
  • Type safety: Strongly typed interfaces between Dart and Rust
  • Async support: Full support for asynchronous operations
  • Cross-platform: Works on Android, iOS, macOS, Linux, and Windows

What Can You Build? The possibilities are endless:

  • Note-taking apps: Search through thousands of notes instantly
  • E-commerce apps: Product search with relevance ranking
  • Documentation viewers: Quick lookup in technical docs
  • Chat applications: Message history search
  • Contact managers: Fast contact lookups
  • Local-first apps: Any app that needs powerful offline search

Getting Started in Minutes Here’s how simple it is to add full-text search to your Flutter app: 1. Install the Plugin dependencies:

 flutter_tantivy: ^0.1.0

2. Initialize import 'package:flutter_tantivy/flutter_tantivy.dart'; import 'package:path_provider/path_provider.dart'; Future<void> main() async {

 WidgetsFlutterBinding.ensureInitialized();
 
 await RustLib.init();
 
 final directory = await getApplicationDocumentsDirectory();
 final indexPath = '${directory.path}/tantivy_index';
 initTantivy(dirPath: indexPath);
 
 runApp(MyApp());

} 3. Index Your Data final docs = [

 Document(id: '1', text: 'Flutter is a UI toolkit'),
 Document(id: '2', text: 'Rust is a systems programming language'),
 Document(id: '3', text: 'Tantivy is a search engine library'),

]; await addDocumentsBatch(docs: docs); 4. Search! final results = await searchDocuments(

 query: 'Flutter OR Rust',
 topK: BigInt.from(10),

); for (final result in results) {

 print('Score: ${result.score}');
 print('Text: ${result.doc.text}');

} That’s it! You now have a fully functional, high-performance search engine running locally in your Flutter app. Key Features 🚀 High Performance Native Rust implementation means blazing-fast indexing and search operations, even on mobile devices. 🔍 Advanced Query Syntax Support for boolean operators (AND, OR, NOT), phrase searches, wildcards, and more: // Boolean search searchDocuments(query: 'flutter AND dart') // Phrase search searchDocuments(query: '"mobile development"') // Wildcard search searchDocuments(query: 'flut*') 💾 Persistent Storage Your index persists across app sessions. No need to rebuild it every time the app starts. 📦 Batch Operations Efficiently add or delete thousands of documents at once: await addDocumentsBatch(docs: largeListOfDocuments); 🎯 Relevance Scoring Results are automatically ranked by relevance, putting the most relevant documents at the top. 🔒 Thread-Safe Safe concurrent access means you can search while adding documents without conflicts. Real-World Performance In our tests, flutter_tantivy can:

  • Index 10,000 documents in under 2 seconds
  • Search through 100,000 documents in milliseconds
  • Handle complex boolean queries with multiple terms efficiently

All while maintaining a small memory footprint and respecting battery life. Advanced Usage: Fine-Grained Control For power users, flutter_tantivy offers manual transaction control: // Add multiple documents atomically await addDocumentNoCommit(doc: doc1); await addDocumentNoCommit(doc: doc2); await addDocumentNoCommit(doc: doc3); // Commit all changes at once commit(); This is perfect for bulk operations where you want to ensure consistency and maximize performance. Cross-Platform from Day One Unlike some search solutions that work well on one platform but struggle on others, flutter_tantivy is built with cross-platform support as a core requirement. The same code runs efficiently on:

  • Android (NDK 25.1.8937393+)
  • iOS (11.0+)
  • macOS (10.11+)
  • Linux
  • Windows

The Future of Mobile Search As apps become more sophisticated and users demand better offline experiences, the need for high-performance local search will only grow. By combining Flutter’s excellent UI framework with Rust’s performance and safety guarantees, flutter_tantivy represents the future of mobile search. Try It Today The plugin is open-source and available on pub.dev. Check out the example app for a complete working demo, or dive right in and add it to your project. Whether you’re building a note-taking app, an e-commerce platform, or anything in between, flutter_tantivy makes it easy to add professional-grade search functionality that your users will love. Links