Search

Tenary Trie word look-ahead

by Read Write Tools
Abstract
The rwt-search web component is the standard user interface for the Site Search feature of the RWSERVE HTTP/2 server. This search dialog box has look-ahead word autofill to guide users in their search for locally hosted documents.

Motivation

The SEMWORDS and SITEWORDS tools (see Site Search) produce an index of all the words used in a given website. That index is used by this web component to provide full text searching on a local basis, without relying on AJAX or direct server interaction.

This web component handles the initial fetch of the site index, caching it to the user's local-storage for ready use across all of the website's documents.

Internally, the web component uses a ternary search trie to provide partial word lookups as the user types, guiding the user towards better results.

Prerequisites

The rwt-search web component works in any browser that supports modern W3C standards. Templates are written using BLUEPHRASE notation, which can be compiled into HTML using the free Read Write View desktop app. It has no other prerequisites. Distribution and installation are done with either NPM or via Github.

Installation using NPM

If you are familiar with Node.js and the package.json file, you'll be comfortable installing the component just using this command:

npm install rwt-search

If you are a front-end Web developer with no prior experience with NPM, follow these general steps:

  • Install Node.js/NPM on your development computer.
  • Create a package.json file in the root of your web project using the command:
  • npm init
  • Download and install the web component using the command:
  • npm install rwt-search

Important note: This web component uses Node.js and NPM and package.json as a convenient distribution and installation mechanism. The web component itself does not need them.

Installation using Github

If you are more comfortable using Github for installation, follow these steps:

  • Create a directory node_modules in the root of your web project.
  • Clone the rwt-search web component into it using the command:
  • git clone https://github.com/readwritetools/rwt-search.git

Using the web component

After installation, you need to add four things to your HTML page to make use of it.

  • Add a script tag to load the component's rwt-search.js file:
  • <script src='/node_modules/rwt-search/rwt-search.js' type=module></script>             
  • Add the component tag somewhere on the page.
    • For scripting purposes, apply an id attribute.
    • Apply a sourceref attribute with a reference to the full-text word index file created by the SITEWORDS utility.
    • Optionally, apply a shortcut attribute with something like F2, F4, etc. for hotkey access.
    • And for WAI-ARIA accessibility apply a role=search attribute.
    <rwt-search id=search sourceref='/data/sitewords' shortcut=F4 role=search></rwt-search>             
  • Add a button for the visitor to click to show the dialog:
  • <a id=search-button title='Search (F4)'>🔎</a>
  • Add a listener to respond to the click event:
  • <script type=module>
    document.getElementById('search-button').addEventListener('click', (e) => {
    document.getElementById('search').toggleDialog(e);
    });
    </script>

Customization

Dialog size and position

The dialog is absolutely positioned towards the bottom right of the viewport. Its position and size may be overridden using CSS by defining new values for the variables:

rwt-search {
--width: 70vw;
--height: 75vh;
--bottom: 1rem;
--right: 1rem;
}

Dialog color scheme

The default color palette for the dialog uses a dark mode theme. You can use CSS to override the variables' defaults:

rwt-search {
--color: var(--white);
--accent-color1: var(--pure-white);
--accent-color2: var(--yellow);
--accent-color3: var(--js-blue);
--background: var(--black);
--accent-background1: var(--medium-black);
--accent-background2: var(--pure-black);
--accent-background3: var(--nav-black);
--accent-background4: var(--black);
}

Internals

The browser's local-storage area is used to cache the sitewords file and the user's most recent search terms. These keys are set by the web component:

sitewords-data The contents of the SITEWORDS index fetched from the server.
sitewords-expires An expiration date checked before considering whether or not to refetch the SITEWORDS index. This is obtained from the server's cache-control response header.
sitewords-etag The etag response header captured in the most recent SITEWORDS fetch. This is used to send a conditional request to the server when the SITEWORDS index has passed its expiration date.
rwsearch-request The most recent search terms used by the visitor.

License

The rwt-search web component is licensed under the MIT License.

MIT License

Copyright © 2020 Read Write Tools.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Availability

Source code github
Package installation NPM
Documentation Read Write Hub

Search — Tenary Trie word look-ahead

🔎