> 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/developer-reference/website-sdk/examples/search-suggestions.md).

# Search Box Suggestions

Show Alhena's searches and AI questions in your own search box, with an Ask AI row for what the shopper typed.

This example adds Alhena's suggestions to your store's search box. As the shopper types, the dropdown shows:

1. **Ask AI:** first, what the shopper typed, which opens the Alhena chat with it. Then Alhena's AI questions for what they typed.
2. **Searches** that complete what the shopper typed. Picking one runs your store's search.

When the shopper taps into the empty box, the dropdown shows AI questions and popular searches for the page they're on.

## What You'll Learn

* Ask for suggestions with `getSearchSuggestions()` as the shopper types
* Add your own first row that asks the AI about what the shopper typed
* Open the Alhena chat for Ask AI rows with `sendMessage()`

## Prerequisites

* Alhena SDK installed on your website
* Search suggestions turned on for your store by the Alhena team

## Code Example

```html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Search Box Suggestions</title>
    <style>
        .search { position: relative; max-width: 560px; }
        .search input { width: 100%; padding: 10px 14px; font-size: 16px; }
        .suggestions {
            position: absolute; left: 0; right: 0; top: 100%;
            margin: 4px 0 0; padding: 6px 0; list-style: none;
            background: #fff; border: 1px solid #ddd; border-radius: 8px;
        }
        .suggestions[hidden] { display: none; }
        .suggestions .label { padding: 6px 14px; font-size: 12px; text-transform: uppercase; color: #888; }
        .suggestions .row { padding: 8px 14px; cursor: pointer; }
        .suggestions .row:hover { background: #f3f3f3; }
        .suggestions .ai { color: #5b35c4; }
    </style>
</head>
<body>

<!-- Your search box. Pressing Enter runs your store's search. -->
<form class="search" action="/search">
    <input id="search-input" name="q" type="search" autocomplete="off" placeholder="What are you looking for?">
    <ul class="suggestions" id="suggestions" hidden></ul>
</form>

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

<!-- Suggestions -->
<script>
var input = document.getElementById('search-input');
var list = document.getElementById('suggestions');
var latestRequest = 0;

function runStoreSearch(text) {
    window.location.href = '/search?q=' + encodeURIComponent(text);
}

function addLabel(text) {
    var label = document.createElement('li');
    label.className = 'label';
    label.textContent = text;
    list.appendChild(label);
}

function addRow(text, className, onPick) {
    var row = document.createElement('li');
    row.className = 'row ' + className;
    row.textContent = text;
    // Keeps the search box focused until the click lands
    row.addEventListener('mousedown', function (event) { event.preventDefault(); });
    row.addEventListener('click', function () {
        list.hidden = true;
        onPick();
    });
    list.appendChild(row);
}

function showSuggestions() {
    var typed = input.value;
    var request = ++latestRequest;

    window.gleenWidget.getSearchSuggestions(typed).then(function (suggestions) {
        // Answers can arrive out of order: draw only the newest one
        if (request !== latestRequest) return;
        list.innerHTML = '';
        var typedText = typed.trim();

        if (typedText || suggestions.aiQuestions.length) {
            addLabel('Ask AI');
        }

        // Your own first row: ask the AI about exactly what the shopper typed
        if (typedText) {
            addRow(typedText, 'ai', function () {
                window.gleenWidget.sendMessage(typedText);
            });
        }

        // Alhena's AI questions: open the chat with the question
        suggestions.aiQuestions.forEach(function (question) {
            addRow(question.text, 'ai', function () {
                window.gleenWidget.sendMessage(question.text);
            });
        });

        if (suggestions.searches.length) {
            addLabel(typedText ? 'Searches' : 'Popular searches');
        }

        // Alhena's searches: run your own search
        suggestions.searches.forEach(function (search) {
            addRow(search.text, 'search', function () {
                runStoreSearch(search.text);
            });
        });

        list.hidden = list.children.length === 0;
    });
}

input.addEventListener('focus', showSuggestions);
input.addEventListener('input', showSuggestions);
input.addEventListener('blur', function () { list.hidden = true; });
</script>

</body>
</html>
```

## How It Works

### Asking for suggestions

Call `getSearchSuggestions()` when the shopper taps into the box and on every change after that. Pass the box's value as it is, including a trailing space: it tells Alhena the last word is finished.

You don't need to wait between keystrokes. Answers are quick, repeated text is answered from memory, and a slow answer comes back empty after a second. Answers can arrive out of order, so draw only the newest one.

### The first row: ask the AI about what the shopper typed

This row is yours. Alhena doesn't send it. It appears as soon as the shopper types and shows their own words. Picking it calls `sendMessage()`, which opens the Alhena chat and sends the text as the shopper's message.

Style it like the other Ask AI rows, so shoppers see all three as ways to ask the AI.

### Picking a suggestion

`getSearchSuggestions()` only returns the rows. What a pick does is up to you. In this example:

| The shopper picks | Call                         | What happens                               |
| ----------------- | ---------------------------- | ------------------------------------------ |
| Your first row    | `sendMessage(typedText)`     | The chat opens and answers what they typed |
| An AI question    | `sendMessage(question.text)` | The chat opens and answers the question    |
| A search          | Your own search              | Your store's search runs for that text     |

### Where suggestions come from

* **Your catalog.** Alhena writes searches for each product category from its products.
* **Your shoppers' searches.** Searches made by several shoppers on your site rank first, most searched first.
* **AI questions.** Each search comes with its own AI questions.

## Tips

* Keep Alhena's rows in the order given. The best ones come first.
* Show the dropdown for an empty box too. On a product or collection page, those rows fit the page.
* If your dropdown has room for fewer rows, show the first AI questions and searches and drop the rest.

## Related

* [getSearchSuggestions(typedText)](/docs/developer-reference/website-sdk/javascript-api.md#getsearchsuggestions-typedtext)
* [sendMessage(message)](/docs/developer-reference/website-sdk/javascript-api.md#sendmessage-message-options)
* [Calling the endpoint directly](/docs/developer-reference/website-sdk/javascript-api.md#calling-the-endpoint-directly)
