Search Messages
The SDK provides a local message search feature, allowing App users to search for messages in a specified conversation by keywords, user ID, and other conditions. It supports searching within a specific time range. The message search only queries messages in the local database and returns a list of messages that contain the specified keyword or meet all search criteria.
Not all message types support keyword search:
- Among the built-in message types, text messages (RCTextMessage), file messages (RCFileMessage), and rich content messages (
RCRichContentMessage
) have implemented thegetSearchableWords
method of theRCMessageCoding
protocol by default. - Custom message types can also support keyword search, but you need to implement it yourself by referring to the documentation. For details, see Custom Message Types.
How to implement global search based on keywords:
- Search all locally stored conversations based on the keyword to obtain a list of conversations that contain the keyword.
- Based on the conversation list data returned by the search, call the method to search a single conversation to find messages that meet the criteria.
Search All Conversations
Starting from version 5.3.0 of RCCoreClient
, it is recommended to use the interface that returns results asynchronously below, while the original synchronous interface is deprecated.
Search all locally stored conversations by keyword to obtain a list of search results (RCSearchConversationResult) that meet the criteria.
NSArray *conversationTypeList = @[@(ConversationType_PRIVATE)];
NSArray *objectNameList = @[@"RC:TXT"];
[[RCCoreClient sharedCoreClient] searchConversations:conversationTypeList
messageType:objectNameList
keyword:@"Search keyword"
completion:^(NSArray<RCSearchConversationResult *> * _Nullable results) {
// Asynchronous callback
}];
Parameter | Type | Description |
---|---|---|
conversationTypeList | NSArray | List of conversation types, including RCConversationType |
objectNameList | NSArray | List of message types, by default only supports built-in types RC:TxtMsg (text message), RC:FileMsg (file message), RC:ImgTextMsg (rich content message). |
keyword | NSString | Keyword. Cannot be empty. |
completion | Block | Asynchronous callback. results returns a list of RCSearchConversationResult. |
Search in a Specified Single Conversation
After obtaining the list of conversations containing the keyword, you can search for messages that meet the criteria in a specified single conversation.
Search Messages by Keyword
Starting from version 5.3.0 of RCCoreClient
, it is recommended to use the interface that returns results asynchronously below, while the original synchronous interface is deprecated.
Search for messages in a specified conversation in local storage by keyword, supporting the search for historical message records before a specified time point. The callback returns a paginated list of messages containing the specified keyword.
[[RCCoreClient sharedCoreClient] searchMessages:ConversationType_PRIVATE
targetId:@"Conversation ID"
keyword:searchText
count:50
startTime:0
completion:^(NSArray<RCMessage *> * _Nullable messages) {
// Asynchronous callback
}];
Parameter | Type | Description |
---|---|---|
conversationType | RCConversationType | Conversation type, for one-to-one chat, pass ConversationType_PRIVATE . |
targetId | NSString | Conversation ID |
keyword | NSString | Keyword. If empty, it defaults to searching all messages that meet the criteria. |
count | int | Maximum number of queries. |
startTime | long long | Start time for querying records. Pass 0 to start searching from the latest message. Pass a non-zero value to search forward from that time. |
completion | Block | Asynchronous callback. messages returns a list of matching RCMessage. |
Search Messages by Keyword within a Specified Time Range
Starting from version 5.3.0 of RCCoreClient
, it is recommended to use the interface that returns results asynchronously below, while the original synchronous interface is deprecated.
The SDK supports limiting the keyword search scope to a specified time range. The callback returns a paginated list of messages containing the specified keyword and meeting the time range requirements.
[[RCCoreClient sharedCoreClient] searchMessages:ConversationType_PRIVATE
targetId:@"Conversation ID"
keyword:searchText
startTime:startTime
endTime:endTime
offset:0
limit:100
completion:^(NSArray<RCMessage *> * _Nullable messages) {
// Asynchronous callback
}];
The limit
parameter controls the number of search results returned, with a range of [1-100]. If it exceeds 100, the default maximum value of 100 is used.
Parameter | Type | Description |
---|---|---|
conversationType | RCConversationType | Conversation type, for one-to-one chat, pass ConversationType_PRIVATE . |
targetId | NSString | Conversation ID |
keyword | NSString | Keyword. If empty, it defaults to searching all messages that meet the criteria. |
startTime | long long | Start time for querying records. Pass 0 to start searching from the latest message. Pass a non-zero value to search forward from that time. |
endTime | long long | End time |
offset | int | Offset. Must be ≧ 0. |
limit | int | Number of search results returned. limit must be greater than 0. The maximum value is 100. If it exceeds 100, 100 records are returned by default. |
completion | Block | Asynchronous callback. messages returns a list of matching RCMessage. |
Search Messages by User ID
Starting from version 5.3.0 of RCCoreClient
, it is recommended to use the interface that returns results asynchronously below, while the original synchronous interface is deprecated.
Search for messages from a specified user ID in local storage, supporting the search for historical message records before a specified time point. The callback returns a paginated list of messages that meet the criteria.
[RCCoreClient sharedCoreClient] searchMessages:ConversationType_PRIVATE
targetId:@"Receiver ID"
userId:@"userId"
count:50
startTime:0
completion:^(NSArray<RCMessage *> * _Nullable messages) {
// Asynchronous callback
}];
Parameter | Type | Description |
---|---|---|
conversationType | RCConversationType | Conversation type, for one-to-one chat, pass ConversationType_PRIVATE . |
targetId | NSString | Conversation ID |
userId | NSString | User ID to search for |
count | int | Maximum number of queries. |
startTime | long long | Start time for querying records. Pass 0 to start searching from the latest message. Pass a non-zero value to search forward from that time. |
completion | Block | Asynchronous callback. messages returns a list of matching RCMessage. |