Skip to main content
Version: v3

Full-Text Search Guide

Full-text search is a common feature used by a lot of modern applications. You might already know that it is possible to implement full-text search by using regular expressions in queries, but this approach will not work efficiently when there are massive objects or fields in a class. To solve the problem, we provide a dedicated full-text search service that you can use in your app.

Enabling Search for Classes

Before using full-text search, you must first enable it for at least one class. For each class you enable the service on, an index will be created for it, and you will be able to use our search component or use the API to search for the objects in it.

The ACLs of the objects in a class still take effect when you use full-text search.

You can enable search for a class by going to Developer Center > Your game > Game Services > Cloud Services > Data Storage > Full-text Search and clicking on Enable search for class:

  • Class: The class you want to enable search on. You can enable search on at most 10 classes.
  • Enabled columns: The fields you want to enable search on. By default, objectId, createdAt, and updatedAt are always enabled. Besides them, you can enable at most 10 custom fields.

If the cloud has not received any full-text search API requests within two weeks after you enabled search for a class, the search service for the class will be disabled.

Search API

We offer a REST API with all the interfaces you need to use the service, and our SDKs encapsulate those interfaces for you to use.

Assuming you have enabled full-text search for a class named GameScore, you can search for objects in this class with keywords:

LCSearchQuery<GameScore> query = new LCSearchQuery<GameScore>("GameScore");
query.QueryString("dennis")
.OrderByDescending("score")
.Limit(10);
LCSearchResponse<GameScore> response = await query.Find();
// The number of documents matching the condition
Debug.Log(response.Hits);
// The documents matching the condition
foreach (GameScore score in response.Results) {

}
// Mark the result of this query so you can use the sid to paginate
Debug.Log(response.Sid);

For the syntax of the query string, see Syntax for q.

Since there is a limit for each query, you may not get all the records that match the query conditions at once. You can call hits() on SearchQuery to get the number of records that match the conditions, and then call find() multiple times on SearchQuery to get the remaining records.

If you cannot preserve the same query object used for querying across different requests, you can use sid for pagination instead. Each query is uniquely marked with the _sid property on SearchQuery. You can reconstruct a query object by calling sid() on SearchQuery to continue the pagination. Each sid is valid for 5 minutes.

You can use SearchSortBuilder to build complex rules for sorting. For example, assuming scores is an array of scores, if you need to sort results in descending order of the average score, and put those who do not have scores to the last:

LCSearchSortBuilder sortBuilder = new LCSearchSortBuilder();
sortBuilder.OrderByAscending("balance", "avg", "last");
searchQuery.SortBy(sortBuilder);

To learn more about the APIs you can use, check out the API docs of our SDKs:

Custom Word List

By default, all String fields will be automatically analyzed through mmseg. You can upload your custom word list as a file by going to Developer Center > Your game > Game Services > Cloud Services > Data Storage > Full-text Search > Custom word list.

The file should be UTF-8-encoded with each word taking up a line. The maximum size of the file is 512 K. Here is an example:

Object-oriented programming
Functional programming
Higher-order function
Responsive design

The word list will take effect in 3 minutes since you upload it. You can check if your word list is in effect with the analyze API (requires Master Key).

Your custom word list will only be applied to new and updated documents/records. To apply it to your existing documents, go to Data Storage > Full-text Search and click on Rebuild index. You should rebuild the index when you update or delete your custom word list, too.