Skip to main content

Search messages

The Nexconn SDK supports searching messages by keyword, message type, sender, and time range.

Search within a channel

Use createSearchMessagesQuery() to create a paginated search query.

kotlin
val channel = DirectChannel("userId")

val params = SearchMessagesQueryParams(
keyword = "hello",
channelTypes = listOf(channel.channelType),
channelIds = listOf(channel.channelId),
senderUserIds = listOf("user_001"),
messageTypes = listOf(MessageType.TEXT, MessageType.IMAGE),
startTime = startTimestamp,
endTime = endTimestamp,
pageSize = 20,
isAscending = false // false: newest first, true: oldest first
)

val query = BaseChannel.createSearchMessagesQuery(params)

query.loadNextPage { pageResult, error ->
if (error == null && pageResult != null) {
val messages = pageResult.data
Log.d("Search", "Found ${messages.size} messages")
if (messages.size >= params.pageSize) {
// Load more results
}
}
}

Parameters

SearchMessagesQueryParams

ParameterTypeDescription
keywordStringThe search keyword (required).
startTimeLongStart timestamp in milliseconds. Default: 0 (no limit).
endTimeLongEnd timestamp in milliseconds. Default: 0 (no limit).
channelTypesList<ChannelType>?Filter by channel types. null searches all types.
channelIdsList<String>?Filter by channel IDs. null searches all channels.
subChannelIdsList<String>?Sub-channel ID filter (community channels). null searches all sub-channels.
senderUserIdsList<String>?Filter by sender IDs. null means no sender filter.
messageTypesList<String>?Filter by message types. null means no type filter.
pageSizeIntResults per page. Default: 20. Range: (0, 100].
isAscendingBooleanSort order. Default: false (newest first).

Search across all channels

Use the static BaseChannel.createSearchMessagesQuery() to search globally.

kotlin
val params = SearchMessagesQueryParams(
keyword = "hello",
channelTypes = listOf(ChannelType.DIRECT, ChannelType.GROUP),
pageSize = 20
)

val query = BaseChannel.createSearchMessagesQuery(params)

query.loadNextPage { pageResult, error ->
if (error == null && pageResult != null) {
val messages = pageResult.data
Log.d("Search", "Global search found ${messages.size} messages")
}
}

Important notes

  • Searchable message types: text, file, and rich text messages.
  • Custom messages must implement MessageContent.getSearchableWord() to be searchable.
  • Search operates on the local database only.
  • Use pagination to avoid loading too many results at once.