searchbase is a lightweight and platform agnostic library that provides scaffolding to create search experiences powered by Elasticsearch.


A simple example

The following example creates a single widget of type(search) to render the results based on the input value.

import 'dart:html';
import 'package:searchbase/searchbase.dart';

void main() {
  final index = 'gitxplore-app';
  final url = '';
  final credentials = 'a03a1cb71321:75b6603d-9456-4a5a-af6b-a487b309eb61';
  // Instantiate the [SearchController]
  final searchController = SearchController(
      // Elasticsearch index name
      // Appbase URL
      // Appbase credentials
      // Unique identifier for search widget
      // Database fields to perform the search
      dataField: ['name', 'description', '', 'fullname', 'owner', 'topics'],
      // initial value
      value: ''

  // Get the input element
  final searchElement = querySelector('#search');

  // Bind the searchController value to input value
  searchElement.value = searchController.value;

  // Update the search input value to searchController to fetch the results
  searchElement.addEventListener('input', (e) {
	// To fetch the suggestions based on the value changes
        options: Options(triggerDefaultQuery: true));

  // Build DOM when search results update
  searchController.subscribeToStateChanges((change) {
		final results = change['results'].next;
		final resultsElement = querySelector('#results');
		resultsElement.innerHTML = ''; {
			var node = document.createElement('li'); // Create a <li> node
			var resultNode = document.createTextNode(; // Create a text node
			node.append(resultNode); // Append the text to <li>

 // Fetch the default results at initial load

Add this in your HTML

<input placeholder="type to search" id="search" />

<div id="results"></div>

An example with a facet

The following example creates three widgets:

  • a search widget to perform the search,
  • a filter widget to filter the GitHub repo by languages,
  • a result widget to render the results based on the selected language filters and search query

The result widget watches for changes to the search and language filter widgets (see the react property). It reacts to the inputs and filter selection changes by triggering an Elasticsearch query to update results.

The language filter widget is also watching for changes to the search. For example, if somebody searches for angular then the language filter will show javascript as an option.

Note: This example is using the SearchBase class instead of the SearchController(that we used in the previous example) class because here we're using multiple widgets that can have dependencies on each other.

import 'dart:html';
import 'package:searchbase/searchbase.dart';

void main() {
  final index = 'gitxplore-app';
  final url = '';
  final credentials = 'a03a1cb71321:75b6603d-9456-4a5a-af6b-a487b309eb61';

  final searchbase = SearchBase(index, url, credentials,
      appbaseConfig: AppbaseSettings(recordAnalytics: true));
  // Register search widget => To render the suggestions
  final searchController = searchbase.register('search-widget', {
    'enablePopularSuggestions': true,
    'dataField': [

// Register filter widget with dependency on search widget
  final filterWidget = searchbase.register('language-filter', {
    'type': QueryType.term,
    'dataField': 'language.keyword',
    'react': {'and': 'search-widget'},
    'value': List<String>()

// Register result widget with react dependency on search and filter widget => To render the results
  final resultWidget = searchbase.register('result-widget', {
    'dataField': 'name',
    'react': {
      'and': ['search-widget', 'language-filter']

  // Render results
  querySelector('#output').innerHtml = """
    <div id="root">
      <h2 class="text-center">Searchbase Demo with Facet</h2>
      <div id="autocomplete" class="autocomplete">
        <input class="autocomplete-input" id="input" />
        <ul class="autocomplete-result-list"></ul>
      <div class="row">
        <div class="col">
          <div class="filter" id="language-filter"></div>
        <div class="col">
          <div id="results">
            <div class="loading">Loading results... </div>
  final input = querySelector('#input');
  void handleInput(e) {
    // Set the value to fetch the suggestions
        options: Options(triggerDefaultQuery: true));

  input.addEventListener('input', handleInput);

  void handleKeyPress(e) {
    // Fetch the results
    if (e.key == 'Enter') {

  input.addEventListener('keydown', handleKeyPress);

  final resultElement = querySelector('#results');

  // Fetch initial results

  // subscribe to `results` property to update re-build result list when update happens
  resultWidget.subscribeToStateChanges((change) {
    final results = change['results'].next;
    final items = {
      return """
    <div id=${i['_id']} class="result-set">
      <div class="image">
        <img src=${i['avatar']} alt=${i['name']} />
      <div class="details">
    final resultStats = """<p class="results-stats">
                          Showing ${results.numberOfResults} in ${results.time}ms

        validator: new NodeValidatorBuilder.common()
              attributes: ['src'], uriPolicy: new DefaultUriPolicy()));
  }, ['results']);

  // Fetch initial filter options

  // subscribe to updates in `aggregationData` property so filter options can change based on search
  filterWidget.subscribeToStateChanges((change) {
    final aggregations = change['aggregationData'].next;
    final container = document.getElementById('language-filter');
    container.setInnerHtml(''); {
      if (i['_key'] != null) {
        final checkbox = document.createElement('input');
        checkbox.setAttribute('type', 'checkbox');
        checkbox.setAttribute('name', i['_key']);
        checkbox.setAttribute('value', i['_key']); = i['_key'];
        checkbox.addEventListener('click', (event) {
          final List<String> values =
              filterWidget.value != null ? filterWidget.value : [];
          if (values.contains(i['_key'])) {
          } else {
          // Set filter value and trigger custom query
              options: Options(stateChanges: true, triggerCustomQuery: true));
        final label = document.createElement('label');
        label.setAttribute('htmlFor', 'i._key');
        final div = document.createElement('div');
  }, ['aggregationData']);

  searchController.subscribeToStateChanges((change) {
    print('Track State Updates');
  }, ['results']);

class DefaultUriPolicy implements UriPolicy {
  bool allowsUri(String uri) {
    return true;