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

What is inside

In Spring (Java)

Spring has no counterpart. It plays the part of Hibernate Search, or of a search server's client, inside the application.

See also