Chat / JavaScript
Chat JavaScript v4
Chat JavaScript
Chat
JavaScript
Home
/
Chat
/
JavaScript
/
Message
This is the new Docs for Chat SDK v4 for JavaScript. To see the previous Docs, click here.

Search messages by keyword

You can search messages that contain a search query or a specified keyword in group channels using MessageSearchQueryParams. The query retrieves a list of messages that contain a search term and meet the optional parameter values set in the MessageSearchQuery class. The createMessageSearchQuery method uses various parameters to support complex search conditions.

You can create the query params instance by using either the default values or your own custom values.

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

The query will retrieve a list of match results. Calling the builder method again returns the next page of the results.

const messages = await query.next();

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

query.hasNext

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

query.isLoading

MessageSearchQuery

Parameter nameTypeDescription

keyword

string

Specifies the search term.

* If the specified search term includes special characters or punctuations, it returns an error.

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

string

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)