# Searching a Database

{% hint style="info" %}
This example uses the same database described in the example for [Connecting a Database Using Google Sheets](https://docs.idew.org/code-chatbot/code-mods/connecting-a-database-using-google-sheets), which has column names *id, term, definition, and keywords*. Your database will likely have different column names.
{% endhint %}

![](/files/-LOKFC5g4rYoTB6z_Vq2)

## 1. Create the Rivescript Trigger

Create a trigger in your chatbot script like the one below that will respond to the user typing "search" followed by a word. The response will begin with "Searching..." to provide feedback to the user, then call a `searchTerms()` function that we will define next.

```javascript
+ search *
- Searching... <call>searchTerms <star></call>
```

## 2. Create the "searchTerms" Function to Process the Search

The function below will process the search and return HTML about the results.

{% code lineNumbers="true" %}

```javascript
> object searchTerms javascript
  var filtered = chatbot.dbFilter2(chatbot.db, "term", args); //used to be args[0]
  var terms = filtered.map(function(row) {
      return `${row.term}: ${row.definition}<hr>`;
    });
  var reply = "Didn't find any matches.";
  if (terms.length) reply = "Here is what I found.<br>" + terms.join(" ");
  chatbot.postReply(reply, 2000);
  return '';
< object
```

{% endcode %}

Let's step through what is happening above...

**Line 2** uses the `chatbot.dbFilter2()` function. Notice there are 3 arguments used by the function.\
&#x20;    \- `chatbot.db` is the database to be searched.\
&#x20;    \- `"term"` is the column name to be searched. In this case we will search each term name.\
&#x20;    \- `args` represents the text the user has typed in for the search.\
The variable `filtered` now contains the matches of our search.

**Lines 3-5** takes each matched item and creates HTML for the term and definition that will be displayed to the user.

**Line 6** defines a variable for our reply string. We start with "Didn't find any matches" as a default.

**Line 7** modifies our reply with the HTML of terms and definitions if there were any matches.

**Line 8** uses the chatbot.postReply() function to display the reply after 2 seconds.

**Line 9** simply returns an empty string so that you don't get an `undefined` showing up in your chatbot in certain cases. The reply text is taken care of in the lines above.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.idew.org/code-chatbot/code-mods/searching-a-database.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
