Adding Search to an Existing Website

Introduction

When you want to add search to an intranet portal or a product site, it is not realistic to ask users to remember the URL of Fess. What is expected is a search box in the header of the pages they always open, and a results page that looks like part of that site.

In this article, we use Fess 15.9 to try, in order, the following three methods, all of which are completed on the browser side only.

  • Place an HTML search form on an existing page and show the results on the Fess search page

  • Use Fess Site Search (FSS) to show search results inside a page of the existing site

  • Change the look of the Fess search page (the theme) to match the existing site

We also configure “path mapping”, which changes the link targets of search results to URLs that users can access.

Building your own screens by calling the search API from the server of a business system is covered in Integrating Business Systems with the Search API.

Prerequisites

  • Following the steps in Running and Searching Fess with Docker, Fess 15.9 is running with Docker Compose at http://localhost:8080/, and at least one thing has been crawled

  • A local HTML file to stand in for the existing site

An existing site is normally on a different origin (the combination of scheme, host, and port) from Fess. To reproduce that condition in this article, run the following command in the directory that holds the HTML file, and open it at http://localhost:8000/.

$ python3 -m http.server 8000

Choosing a Method

Method Where search results are shown What Fess needs
Search form Moves to the Fess search page (/search) and shows them there Nothing
Fess Site Search Inside a page of the existing site The fess-webapp-v1-api plugin
Changing the theme The Fess search page (with a different look) A theme ZIP (or the [General] settings)

The search form and a theme change can be combined. If the page users land on after leaving the existing site should look the same, configure both.

Sending Queries from a Search Form to the Fess Search Page

Placing the Form

Write the following form where you want the search box on the existing page.

<form method="get" action="http://localhost:8080/search">
  <input type="text" name="q" maxlength="1000" autocomplete="off">
  <button type="submit">Search</button>
</form>

The search terms are sent to /search of Fess as the q parameter, and the results are shown on the Fess search page. maxlength matches the upper limit for the length of search terms on the Fess side, query.max.length (default 1000). In production, change the action URL to the Fess URL that users access.

To have users search only documents with a particular label, send the label value as fields.label as well.

<form method="get" action="http://localhost:8080/search">
  <input type="text" name="q" maxlength="1000" autocomplete="off">
  <input type="hidden" name="fields.label" value="intranet">
  <button type="submit">Search</button>
</form>

intranet is the value of a label created in [Crawler] > [Label]. If you send a value that does not exist, there will be 0 results. For how to create labels, see Searching Websites and File Servers Together.

Checking the Result

  1. Open the HTML file from http://localhost:8000/ and search for a word contained in a crawled document

  2. If the browser moves to http://localhost:8080/search?q=... and the results are shown on the Fess search page, you are done

If there are 0 results, search for the same word in the search box at http://localhost:8080/ and compare. If that also returns 0 results, the problem is not the form but crawling or permissions.

About Suggestions

The Search Form Integration page includes an example that adds suggestions to the form. However, /js/suggestor.js and /js/jquery-3.7.1.min.js, which that example loads, are not included in Fess 15.9, so the example does not work as is in 15.9. The form part (sending q to /search) can be used as it is.

To show suggestions in the search box of an existing site, use one of the following.

Once users have moved to the Fess search page, the theme bundled with Fess shows suggestions.

Changing the Look of the Fess Search Page

In 15.9, the search page is rendered by a “theme”, which bundles HTML, CSS, and JavaScript. The [Page Design] screen and JSP editing of earlier versions no longer exist. If nothing is configured, the bundled bootstrap theme is used.

What You Can Change Without Building a ZIP

What you write in “Search Top Page” under “Notice” in [System] > [General] is shown on the search top page and the search results page of the bundled theme. You can also write HTML, but elements such as <script> and <style> are removed. If all you want is a one-line notice for internal users, there is no need to build a theme.

Using a Published Theme

The Fess project publishes themes such as docsearch, which is designed for document search.

  1. Open [System] > [Theme]

  2. Under “Install Theme”, choose a theme in “Name” and click [Select]

  3. In “Version”, choose the version for 15.9 (15.9.x) and click [Install]

  4. In “Default Theme” above the list, choose the installed theme and click [Set]

When you reopen http://localhost:8080/, the page is shown with the new theme. Choosing “(no default)” and clicking [Set] returns to the bundled bootstrap theme.

Copying the Bundled Theme and Changing the Logo and Colors

The bundled theme cannot be edited directly, and a theme named bootstrap cannot be replaced by uploading one either. Copy it under a different name and change the copy. Here we use mytheme.

  1. Copy the bundled theme out of the container

    $ docker compose -f compose.yaml -f compose-opensearch3.yaml cp fess01:/usr/share/fess/app/themes/bootstrap ./mytheme
    
  2. In mytheme/theme.yml, change name to mytheme and displayName to something like My Theme

  3. Change the following files

    • Colors and layout: assets/styles.css

    • Logo: assets/logo-head.png (header) and assets/logo.png (search top page)

    • Organization name in the footer: footer.copyright_org in i18n/messages.en.json (messages.ja.json for the Japanese UI)

  4. Package the files so that theme.yml is at the root of the ZIP

    $ cd mytheme && zip -r ../mytheme.zip . && cd ..
    
  5. Upload mytheme.zip with [Upload] in [System] > [Theme]

  6. In “Default Theme”, choose mytheme and click [Set]

Do not change index.html. The bundled index.html refers to the files in the theme in the form {{themePath}}/assets/.... Because Fess replaces {{themePath}} with themes/mytheme when serving the page, renaming the theme is enough for the files of the copied theme to be loaded.

Checking the Result

Reopen http://localhost:8080/ and check that the logo and colors have changed. If they have not, check in the Network tab of the developer tools whether the stylesheet is loaded from /themes/mytheme/assets/styles.css. If it is loaded from /themes/bootstrap/, the default theme setting has not taken effect.

Things to Watch Out for with Themes

  • The search page carries a Content-Security-Policy header that limits the sources of scripts, styles, and images to Fess itself. Fonts and scripts from external CDNs are not loaded, so include them in the theme ZIP

  • The search page is not shown inside an iframe of another page (because the default of theme.index.frame.ancestors is 'none'). To show search results inside a portal, use FSS

  • fess01 in Docker has no volumes. Uploaded themes are placed inside the container and the default theme setting is saved in system.properties, so both disappear when the container is recreated with down. Normally use stop/start, and after recreating the container, upload and configure the theme again

  • The bundled theme is built for /api/v2 of the same version of Fess. After upgrading Fess, copy the new bundled theme again and reapply your changes

For the structure of themes and the items in theme.yml, see the Theme Development Guide.

References