spring_search_dart
spring_search_dart is full-text search inside a Spring for Dart application: the pages of a documentation site, the products of a shop, the notes of an app. You say where the documents come from (Markdown files, or any bean), and ask the SearchService bean. It runs on the Dart VM, in Flutter and on the web, in pure Dart: no server, no native code.
It is not on pub.dev yet: take it from the framework's git repository (see Setup).
Why
A search box usually means a search server (Elasticsearch, Algolia) or a hosted index, with its costs and its network round trip. For a few thousand documents that is too much: an index in memory answers as you type, offline, on every platform. This one does what users expect of a search box: every word, the word being typed as a prefix, typos, phrases, any script (Japanese without a dictionary), camel-case names, English word forms, and a snippet with the matches highlighted.
Setup
# pubspec.yaml
dependencies:
spring_search_dart:
git:
url: https://github.com/farum-azula/spring_core_dart.git
path: spring_search_dart
dev_dependencies:
build_runner: ^2.17.0
spring_core_dart_generator: ^0.9.0
It depends on spring_boot_dart only, so it works in a server, a command-line program and a Flutter app alike. The generator finds its auto-configuration, which declares the searchService bean.
import 'package:spring_search_dart/spring_search_dart.dart';
Example
# lib/resources/application.yml
spring:
search:
markdown:
- location: resources:content/**/*.md
exclude: ['**/contents*.md']
link: /docs/{path}
collapse-by: page # one hit per page: its best section
@Service()
class DocsSearch {
DocsSearch(this.search);
final SearchService search;
Future<void> show(String query) async {
final hits = await search.search(query, language: 'en');
for (final hit in hits) {
final snippet = hit.snippet('body');
print('${hit.document.title} ${hit.document.link}: ${snippet.text}');
}
}
}
show('bean factry') prints the sections of the Markdown files about the bean factory, best first, despite the typo, each with its link (/docs/core/beans#bean-factory) and a piece of its text around the match.
What it finds
- Every word of the query (
spring.search.operator: orfor one of them), ranked with BM25; a field weighs asspring.search.fieldssays (title8,heading4,body1 by default). - The last word while it is being typed, as the start of longer ones:
beafindsbean. - Words with a typo:
contxetfindscontext(spring.search.fuzzy). - Phrases:
"bean factory"finds the two words next to each other. - Words of any script. Chinese, Japanese and Korean are cut in pairs of characters, so
依存性finds依存性注入without a dictionary. - The parts of camel-case names:
contextfindsApplicationContext. - In English, the forms of a word:
bindingfindsbinds.
What is inside
- Documents: Markdown files and
SearchDocumentSourcebeans, languages, rebuilding the index. - Searching: queries, hits, scores, highlights and snippets.
- Analyzers and rankers: how text becomes terms per language, and how to change the ranking.
- Search in a Flutter app: a search box as you type, with highlights and a keyboard shortcut.
- All properties: every key of
spring.search. - Differences from Spring
In Spring (Java)
Spring has no counterpart. It plays the part of Hibernate Search, or of a search server's client, inside the application.