Skip to main content

Introducing full-text search

· 5 min read
Ümit Çakmak

We are excited to announce the release of Full-text Search functionality in your app database. Full-text search refers to searching text inside large amount of text data and returning results that contain some or all of the words from the query.

It is helpful to make an analogy to gain an understanding of full-text Search. Imagine that your application is a book and the database engine would be the table of contents of this book. If you want to find chapter 5 in this book, the table of contents is a very efficient way to find it. You simply look in the table of contents, jump straight to chapter 5 and find the content you wanted.

Now imagine that you want to recall a quote that you have read in the book. You do not know which chapter it is in, and you only remember that it contained the word "gift". The table of contents is not much useful in this case, because it only contains chapter numbers and titles. So you are stuck scanning the entire book to find the quote. Unless, your book has an index.

A book index is an ordered listing of the words that occur in the book, like the glassory of the book. Each entry in the index will contain a list of page numbers where that word occurs. This allows the reader to quickly find any information in the book relevant to the word "gift" for instance.

Full-text search works similarly; it lets the users find a word or phrase anywhere within the database or document. Additionally, full-text search also enables fuzzy search, delivering accurate results even with a typo or a spelling mistake.

How does full-text search work?

Full-text search is primarily used to search large amounts of text. For example, a search engine will use a full-text search to look for keywords in all the web pages that it indexed. The key to this technique is indexing.

Essentially, the indexing process goes through each text field of a dataset. For each word, it will start by removing any diacritics (marks placed above or below letters, such as é, à, ü, and ç). Then, based on the used language, the algorithms will remove filler words and only keep the stem of the terms. This way, "to drink," "drinking," and "drank" are all classified as the same "drink" keyword. It then changes the casing to use only either uppercase or lowercase. The exact indexing process is determined by the analyzer that is used.

After indexing your data, which acts as a glossary of all the words in the indexed fields with reference to the specific document (e.g., object), you can run search queries. When a query is performend, the search engine searches the index and finds all matching documents.

How to use full-text search in Altogic?

To do a full-text search, you need to create full-text search indices in your database. You can index text or rich text fields in your data model. While adding fields to your app data model, you can mark a text or rich-text field as full-text searchable and select the analyzer that will be used to index data. Similary, from the field details view, you can also enable/disable full-text search indexing for a text or full-text field.

Create new field dialog: Full-text search indexing

Create new field dialog

Edit field properties view

Edit field dialog

Following the index creation you can run queries to search data. If you are using Altogic Designer to build your cloud functions you can use the Get Multi Objects by Full-Text Search node and specify the model, field, search text and select query parameters. If you are using the Altogic Client Library, you can use the QueryBuilder.searchFuzzy method to perform full-text search.

Running full-text search queries through Altogic Designer

Run full-text search query

Running full-text search queries through Altogic Client API

You can use the searchFuzzy method of QueryBuilder to perform fuzzy search on your full-text search indexed fields.

import { createClient } from "altogic";

let envUrl = process.env.REACT_APP_ALTOGIC_ENV_URL;
let clientKey = process.env.REACT_APP_ALTOGIC_CLIENT_KEY;

// Create Altogic client instance
const altogic = createClient(envUrl, clientKey);

let searchText = 'life is a gift';

// Gets the search result for `life is a gift` from blogs model and searches the 'title' field
// which must be covered by a full-text search index and automatically sorts by the scoring result
let result = await altogic.db
.model("blogs")
.limit(25)
.searchFuzzy('title', searchText);

If you have any questions about full-text search or want to share what you have built, please post a message in our community forum or discord channel.