
# 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](#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

```yaml
# 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.

```dart
import 'package:spring_search_dart/spring_search_dart.dart';
```

## Example

```yaml
# 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
```

```dart
@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: or` for one of them), ranked with BM25; a field weighs as `spring.search.fields` says (`title` 8, `heading` 4, `body` 1 by default).
- **The last word while it is being typed**, as the start of longer ones: `bea` finds `bean`.
- **Words with a typo**: `contxet` finds `context` (`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**: `context` finds `ApplicationContext`.
- **In English, the forms of a word**: `binding` finds `binds`.

## What is inside

- [Documents](documents.md): Markdown files and `SearchDocumentSource` beans, languages, rebuilding the index.
- [Searching](searching.md): queries, hits, scores, highlights and snippets.
- [Analyzers and rankers](analyzers.md): how text becomes terms per language, and how to change the ranking.
- [Search in a Flutter app](flutter.md): a search box as you type, with highlights and a keyboard shortcut.
- [All properties](properties.md): every key of `spring.search`.
- [Differences from Spring](differences.md)

## 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

- [spring_boot_dart](../spring-boot-dart/index.md)
- [spring_boot_starter_flutter](../spring-boot-starter-flutter/index.md)
- [Resources](../spring-core-dart/resources/index.md)
