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

Search messages by a keyword

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 value set in the MessageSearchQueryParams class.

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

class CustomViewController: ViewController {
    var query: MessageSearchQuery?
    
    // ...
    
    func createQuery() {
        self.query = SendbirdChat.createMessageSearchQuery { params in
            params.keyword = "Sendbird"
            params.limit = 10
        }
    }
}

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

class CustomViewController: ViewController {
    var query: MessageSearchQuery?
    
    // ...

    func loadNextPage() {
        self.query?.loadNextPage { users, error in
            guard error == nil else {
                // Handle error.
                return
            }
        }
    }
}

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

self.query?.hasNext

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

self.query?.isLoading

MessageSearchQueryBuilder

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

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

UInt

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

exactMatch

Bool

Determines whether to search for messages that exactly match the search term. If set to false, it returns partially matched messages that contain the search term. (Default: false)

messageTimestampFrom

Int64

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

messageTimestampTo

Int64

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

order

MessageSearchQueryOrder

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

reverse

Bool

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