> For the complete documentation index, see [llms.txt](https://alhena.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://alhena.gitbook.io/docs/features/ai-search/installation.md).

# Installation

Add AI Search to your website in three steps: install the Alhena SDK, add the search container, and open AI Search from your search bar or a button.

AI Search uses the same code as [Conversational Search](https://alhena.gitbook.io/docs/features/conversational-search/installation.md). Once Alhena turns AI Search on for your store, that code opens AI Search instead.

{% hint style="success" %}
**Already have Conversational Search on your site?** There is nothing to install. Your existing search buttons open AI Search from the next page load after it is turned on for your store.
{% endhint %}

## Prerequisites

* An active Alhena AI account
* AI Search turned on for your store by the Alhena team (see [Getting Started](https://alhena.gitbook.io/docs/features/ai-search.md#getting-started))
* Access to your website's HTML or theme editor
* Your company key (found in your dashboard URL. For example, if your dashboard is `https://app.alhena.ai/dashboard/acme/`, your company key is `acme`)

## Step 1: Install the Alhena SDK

If you already have the Alhena Chat Widget, Product FAQs or Conversational Search installed, skip this step. AI Search uses the same SDK.

Add the following code just before the closing `</body>` tag of your website:

```html
<script>
document.gleenConfig = {
    company: "YOUR_COMPANY_KEY",
    apiBaseUrl: "https://app.alhena.ai"
};
</script>
<script src="https://app.alhena.ai/sdk/gleenWidget.js"></script>
```

{% hint style="info" %}
Replace `YOUR_COMPANY_KEY` with your actual company key. For EU region accounts, use `https://eu.alhena.ai` instead of `https://app.alhena.ai`.
{% endhint %}

**For Shopify stores:** The SDK is automatically installed when you add the Alhena Shopify app. No manual setup is needed.

You can also find your pre-configured snippet in the Alhena dashboard under **Integrations > AI Search > Installation**.

## Step 2: Add the Search Container

Add this element to every page where shoppers can search. Placing it just before the closing `</body>` tag works well:

```html
<div id="alhena-conversational-search"></div>
```

AI Search opens as a full page on top of your site, so the container doesn't need a particular spot or size. It tells the SDK that the page uses search. Without it, `gleenWidget.openSearch()` does nothing.

{% hint style="info" %}
The container keeps its `alhena-conversational-search` name for AI Search, so the same code works for both experiences.
{% endhint %}

For Shopify stores, add the container to your theme's `theme.liquid` file, after the main content:

```html
<body>
  ...
  <main id="MainContent" class="content-for-layout focus-none" role="main" tabindex="-1">
    {{ content_for_layout }}
  </main>

  <!-- Add the AI Search container after main content -->
  <div id="alhena-conversational-search"></div>
  ...
</body>
```

## Step 3: Open AI Search

### Option A: Send Searches from Your Search Bar (Recommended)

The most natural setup sends what the shopper types in your existing search bar to AI Search. When the shopper presses Enter, AI Search opens with their search already running:

<figure><img src="https://2864633804-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKZ3bWFAIdLAqpW2uafZ4%2Fuploads%2Fgit-blob-bafa085ff8468b5d4f495ecfc6445e627ea6de25%2Fai-search-option-a-search-bar.png?alt=media" alt=""><figcaption><p>Your search bar stays as it is. Pressing Enter opens AI Search.</p></figcaption></figure>

```html
<form id="store-search" action="/search" method="get">
    <input id="search-input" name="q" type="search" placeholder="Search products">
    <button type="submit">Search</button>
</form>

<script>
document.getElementById('store-search').addEventListener('submit', function (event) {
    var query = document.getElementById('search-input').value.trim();
    if (!query || !window.gleenWidget) return;
    event.preventDefault();
    window.gleenWidget.openSearch(query);
});
</script>
```

If the box is empty, the form runs your store's regular search.

### Option B: Keep Your Search and Add an AI Search Button

To keep your regular search and offer AI Search next to it, add a button that passes the search bar's text to AI Search. Style the button like the other buttons on your site:

<figure><img src="https://2864633804-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKZ3bWFAIdLAqpW2uafZ4%2Fuploads%2Fgit-blob-76e66a7eb194afaf223beecc545aad27f901af88%2Fai-search-option-b-button.png?alt=media" alt=""><figcaption><p>Enter runs your regular search. The AI Search button sends the same text to AI Search.</p></figcaption></figure>

```html
<input id="search-input" type="text" placeholder="Search...">
<button onclick="openAISearch()">
    AI Search
</button>
```

```javascript
function openAISearch() {
    const searchInput = document.querySelector('#search-input');
    gleenWidget.openSearch(searchInput.value);
}
```

### Option C: Open from Any Button

If your site has no search box of its own, open AI Search from any button. It opens with an empty search box, or with the shopper's last search on this page:

<figure><img src="https://2864633804-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKZ3bWFAIdLAqpW2uafZ4%2Fuploads%2Fgit-blob-0542ea041f5f277703bb83ef6c9361279e50fe7d%2Fai-search-option-c-any-button.png?alt=media" alt=""><figcaption><p>Any button can open AI Search, such as the search icon in your header</p></figcaption></figure>

```html
<button onclick="gleenWidget.openSearch()">
    Search
</button>
```

Or open and close it with the same button:

```html
<button onclick="gleenWidget.toggleSearch()">
    Toggle Search
</button>
```

## Shopify Integration Example

Most Shopify themes use a search form that sends shoppers to `/search` with their text in a field named `q`. Adding this script to `theme.liquid`, just before the closing `</body>` tag, sends every search from those forms to AI Search, including the header search bar:

```html
<script>
document.addEventListener('submit', function (event) {
    var form = event.target;
    if (!form.matches('form[action$="/search"]') || !window.gleenWidget) return;
    var input = form.querySelector('input[name="q"]');
    var query = input ? input.value.trim() : '';
    if (!query) return;
    event.preventDefault();
    window.gleenWidget.openSearch(query);
}, true);
</script>
```

{% hint style="warning" %}
Themes differ. If searches still go to your store's regular search page, your theme may handle its search form with its own script. In that case, add the `openSearch` call where the theme submits the search, as in Option A.
{% endhint %}

## Complete Example

All three steps together on a plain HTML page:

```html
<body>
    ...
    <form id="store-search" action="/search" method="get">
        <input id="search-input" name="q" type="search" placeholder="Search products">
        <button type="submit">Search</button>
    </form>
    ...

    <!-- Step 2: AI Search container -->
    <div id="alhena-conversational-search"></div>

    <!-- Step 1: Alhena SDK -->
    <script>
    document.gleenConfig = {
        company: "YOUR_COMPANY_KEY",
        apiBaseUrl: "https://app.alhena.ai"
    };
    </script>
    <script src="https://app.alhena.ai/sdk/gleenWidget.js"></script>

    <!-- Step 3: send searches to AI Search -->
    <script>
    document.getElementById('store-search').addEventListener('submit', function (event) {
        var query = document.getElementById('search-input').value.trim();
        if (!query || !window.gleenWidget) return;
        event.preventDefault();
        window.gleenWidget.openSearch(query);
    });
    </script>
</body>
```

## JavaScript SDK Reference

| Method                            | Description                                                                                     |
| --------------------------------- | ----------------------------------------------------------------------------------------------- |
| `gleenWidget.openSearch()`        | Opens AI Search. If the shopper searched earlier on this page, their last search is still there |
| `gleenWidget.openSearch(query)`   | Opens AI Search and runs a search for the given text                                            |
| `gleenWidget.closeSearch()`       | Closes AI Search from your own code, for example before your site opens its own popup           |
| `gleenWidget.toggleSearch()`      | Opens AI Search if it is closed, closes it if it is open                                        |
| `gleenWidget.toggleSearch(query)` | Same as `toggleSearch()`; when it opens, it runs a search for the given text                    |

## What Shoppers See

<figure><img src="https://2864633804-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKZ3bWFAIdLAqpW2uafZ4%2Fuploads%2Fgit-blob-c21a36c39fb2fed695ce86bef864d4a39cd5f0e9%2Fai-search-results-page.png?alt=media" alt=""><figcaption><p>AI Search opens on top of your page with the shopper's search, the filters it read, and matching products</p></figcaption></figure>

* AI Search covers the whole page, and your site stops scrolling behind it.
* On phones and tablets, shoppers close it with the close button next to the search box. On desktop they press the Escape key. Your page is right where they left it.
* Product links open in a new tab, so shoppers can come back to their results.
* Searches can be up to 300 characters, enough for a full description of what the shopper wants.

## Verifying Your Installation

After adding the code to your website:

1. Open your website in a browser.
2. Search for something in your own words, such as a product type with a color and a price limit.
3. The AI Search page should open with your search at the top, filters for what you typed, and matching products.
4. Remove one of the filters and check that the results update.

If the Conversational Search start page opens instead, reload the page. If it still opens, AI Search isn't turned on for your store yet. Contact the Alhena team.

If nothing opens, check the browser console for errors, make sure the search container from Step 2 is on the page, and verify your company key.

## Next Steps

* Show suggestions in your own search box as the shopper types with [Search Box Suggestions](https://alhena.gitbook.io/docs/developer-reference/website-sdk/examples/search-suggestions.md)
* Build a fully custom search experience with the [AI Search API](https://alhena.gitbook.io/docs/developer-reference/api/ai-search.md)
