BlogAnnounced at MongoDB.local NYC 2024: A recap of all announcements and updatesLearn more >>
MongoDB Developer
Realm
plus
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Productschevron-right
Realmchevron-right

How to Do Full-Text Search in a Mobile App with MongoDB Realm

Ferdinando Papale3 min read • Published Jul 14, 2023 • Updated Jul 14, 2023
RealmC#
Facebook Icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
Full-text search is an important feature in modern mobile applications, as it allows you to quickly and efficiently access information within large text datasets. This is fundamental for certain app categories that deal with large amounts of text documents, like news and magazines apps and chat and email applications.
We are happy to introduce full-text search (FTS) support for Realm — a feature long requested by our developers. While traditional search with string matching returns exact occurrences, FTS returns results that contain the words from the query, but respecting word boundaries. For example, looking for the word “cat” with FTS will return only text containing exactly that word, while a traditional search will return also text containing words like “catalog” and “advocating”. Additionally, it’s also possible to specify words that should not be present in the result texts. Another important addition with the Realm-provided FTS is speed: As the index is created beforehand, searches on it are very fast compared to pure string matching.
In this tutorial, we are giving examples using FTS with the .NET SDK, but FTS is also available in the Realm SDK for Kotlin, Dart, and JS, and will soon be available for Swift and Obj-C.
Later, we will show a practical example, but for now, let us take a look at what you need in order to use the new FTS search with the .NET Realm SDK:
  1. Add the [Indexed(IndexType.FullText)] attribute on the string property to create an index for searching.
  2. Running queries
    1. To run Language-Integrated Query (LINQ) queries, use QueryMethods.FullTextSearch. For example: realm.All<Book>().Where(b => QueryMethods.FullTextSearch(b.Summary, "fantasy novel")
    2. To run Filter queries, use the TEXT operator. For example: realm.All<Book>().Filter("Summary TEXT $0", "fantasy novel");
Additionally, words in the search phrase can be prepended with a “-” to indicate that certain words should not occur. For example: realm.All<Book>().Where(b => QueryMethods.FullTextSearch(b.Summary, "fantasy novel -rings")

Search example

In this example, we will be creating a realm with book summaries indexed and searchable by the full-text search. First, we’ll create the object schema for the books and index on the summary property:
Next, we’ll define a few books with summaries and add those to the realm:
And finally, we are ready for searching the summaries as follows:

Additional information

A few important things to keep in mind when using full-text search:
  • Only string properties are valid for an FTS index, also on embedded objects. A collection of strings cannot be indexed.
  • Indexes spanning multiple properties are not supported. For example, if you have a Book object, with Name and Summary properties, you cannot declare a single index that covers both, but you can have one index per property.
  • Doing an FTS lookup for a phrase across multiple properties must be done using a combination of two expressions (i.e., trying to find red ferrari where red appears in property A and ferrari in property B must be done with (A TEXT 'red') AND (B TEXT 'ferrari')).
  • FTS only supports languages that use ASCII and Latin-1 character sets (most western languages). Only sequences of (alphanumeric) characters from these sets will be tokenized and indexed. All others will be considered white space.
  • Searching is case- and diacritics-insensitive, so “Garcon” matches “garçon”.
We understand there are additional features to FTS we could work to add. Please give us feedback and head over to our community forums!

Facebook Icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
Related
Tutorial

Migrating Your iOS App's Synced Realm Schema in Production


Mar 06, 2023 | 9 min read
Tutorial

Build an Infinite Runner Game with Unity and the Realm Unity SDK


Feb 03, 2023 | 12 min read
Article

New Realm Cocoa Data Types


Mar 06, 2023 | 4 min read
Article

Lessons Learned from Building a Game with MongoDB and Unity


May 13, 2022 | 4 min read
Table of Contents
  • Search example