/ SDKs / JavaScript
SDKs
Chat SDKs JavaScript v4
Chat SDKs JavaScript
Chat SDKs
JavaScript
Version 4

Search messages by a keyword

Copy link

Message search allows you to retrieve a list of messages that contain a search query or a specified keyword in group channels by implementing MessageSearchQuery. The query retrieves a list of messages that contain a search term and meet the optional parameter values set in MessageSearchQueryParams. The createMessageSearchQuery method uses various parameters to support complex search conditions.

Note: Punctuations and special characters are ignored while indexing, so unless they're being used for advanced search functionalities, they should be removed or replaced in the search term for best results.

You can create the query instance by following the code below.

JavaScriptTypeScript
const params = {
    keyword: 'Sendbird',
    channelUrl: '',
    channelCustomType: '',
    limit: 20,
    exactMatch: false,
    messageTimestampFrom: null,
    messageTimestampTo: null,
    order: 'score',
reverse: false,
};
const query = sb.createMessageSearchQuery(params);

Then, the query retrieves a list of match results. Calling the builder method again returns the next page of the results.

JavaScriptTypeScript
const messages = await query.next();

Use the hasNext method to see if there is a next page.

JavaScriptTypeScript
query.hasNext

Use the isLoading method to see if the search results are loading.

JavaScriptTypeScript
query.isLoading

Advanced search allows the SDK to create and support complicated search conditions, improving search results. Search functionalities such as wildcard, fuzzy search, logical operators, and synonym search are supported for the keyword parameter. You can use these functionalities by setting advancedQuery to true. See the advanced search section in our Platform API Docs for more information.

  • Wildcard: Include ? or * in search terms. ? matches a single character while * can match zero or more characters.

  • Fuzzy search: Add ~ at the end of search terms. Fuzzy search shows similar terms to the search term determined by a Levenshtein edit distance. If your search term is less than two characters, only exact matches are returned. If the search term has between three and five characters, only one character is edited. If the search term is longer than five characters, up to two characters are edited.

  • Logical operators: Use AND and OR to search for more than one term. The logical operators must be uppercase. You can also use parentheses to group multiple search terms or specify target fields. If you want to look for search terms in not only the content of the message but also specified target fields of the message, such as custom type or data, you can specify the field and search term as a key-value item.

MessageSearchQuery

Copy link

You can build the query class using the following parameters which allow you to add various search options.

Parameter nameTypeDescription

keyword

string

Specifies the search term.

* Special characters and punctuations aren't indexed, so including them in the keyword may return unexpected search results.

channelUrl

string

Specifies the URL of the target channel.

channelCustomType

string

Specifies the custom channel type.

limit

number

Specifies the number of messages to return per page. Acceptable values are 1 to 99, inclusive. (Default: 20)

exactMatch

boolean

Determines whether to search for messages that exactly match the search term. If set to false, it will return partial matches that contain the search term. (Default: false)

messageTimestampFrom

number

Restricts the search scope to messages sent after the specified value in Unix milliseconds format. This includes the messages sent exactly on the timestamp. (Default: 0)

messageTimestampTo

number

Restricts the search scope to messages sent before the specified value in Unix milliseconds format. This includes the messages sent exactly on the timestamp. (Default: 0)

order

MessageSearchOrder

Determines by which field the results are sorted. Acceptable values are the following:
- MessageSearchOrder.SCORE (default): search relevance score.
- MessageSearchOrder.TIMESTAMP: the time when a message was created.

reverse

boolean

Determines whether to sort the results in reverse order. If set to false, they're sorted in a descending order. (Default: false)