Search Messages
The SDK provides a local message search feature, allowing app users to search for messages in a specific conversation by keywords, user ID, and other criteria. It supports searching within a specified 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 conditions.
Not all message types support keyword search:
- Among the built-in message types, text messages (TextMessage), file messages (FileMessage), and rich content messages (
RichContentMessage
) have implemented theMessageContent#getSearchableWord
method 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 for messages in a single conversation that meet the criteria.
Search All Conversations
Search all locally stored conversations by keyword to obtain a list of conversations that meet the criteria SearchConversationResult.
String keyword = "search keyword";
Conversation.ConversationTypes[] conversationTypes = {ConversationTypes.PRIVATE, ConversationTypes.GROUP};
String[] messageTypeObjectNames = {"RC:TxtMsg"};
RongIMClient.getInstance().searchConversations(keyword, conversationTypes, messageTypeObjectNames,
new RongIMClient.ResultCallback<List<SearchConversationResult>>() {
@Override
public void onSuccess(List<SearchConversationResult> searchConversationResults) {
}
@Override
public void onError(RongIMClient.ErrorCode errorCode) {
}
});
Parameter | Type | Description |
---|---|---|
keyword | String | Search keyword |
conversationTypes | Conversation.ConversationType[] | Conversation type list, including ConversationType. |
objectNames | String[] | Message type list, default only supports built-in types RC:TxtMsg (text message), RC:FileMsg (file message), RC:ImgTextMsg (rich content message). |
callback | RongIMClient.ResultCallback<List<SearchConversationResult>> | Callback. The search result is a list of SearchConversationResult. |
Search in a Specific Single Conversation
After obtaining a list of conversations that contain the keyword, you can search for messages that meet the criteria in a specific single conversation.
Search Messages by Keyword
Search for messages in a specific conversation in local storage based on the keyword. It supports searching for historical message records before a specified time point. The callback returns a paginated list of messages that contain the specified keyword.
ConversationType conversationType = ConversationType.PRIVATE;
String targetId = "conversation Id";
String keyword = "search keyword";
int count = 20;
long beginTime = 122323344;
RongIMClient.getInstance().searchMessages(conversationType, targetId, keyword, count, beginTime,
new RongIMClient.ResultCallback<List<Message>>() {
@Override
public void onSuccess(List<Message> messages) {
}
@Override
public void onError(RongIMClient.ErrorCode errorCode) {
}
});
Parameter | Type | Description |
---|---|---|
conversationType | ConversationType | Conversation type. |
targetId | String | Conversation ID. |
keyword | String | Search keyword. |
count | int | Number of items per page, recommended maximum of 100 items. Pass 0 to return all searched messages. |
beginTime | long | Start time of the query. Pass 0 to start searching from the latest message. Pass a non-zero value to search from that time backward. |
callback | RongIMClient.ResultCallback<List<Message>> | Callback. The search result is a list of Message. |
Search Messages by Keyword within a Specified Time Range
The RongIMClient
does not provide this method. Please use RongCoreClient.
The SDK supports limiting the keyword search to a specified time range. The callback returns a paginated list of messages that contain the specified keyword and meet the time range requirements.
ConversationType conversationType = ConversationType.PRIVATE;
String targetId = "conversation Id";
String keyword = "123";
long startTime = 0;
long endTime = 1585815113;
int offset = 0;
int limit = 80;
RongCoreClient.getInstance().searchMessages(conversationType, targetId, keyword, startTime, endTime, offset, limit,
new IRongCoreCallback.ResultCallback<List<Message>>(){
@Override
public void onSuccess(List<Messag> messages) {
}
@Override
public void onError(IRongCoreEnum.CoreErrorCode e) {
}
});
The limit
parameter controls the number of search results returned, with a range of [1-100]. If it exceeds 100, the maximum value of 100 is used by default.
Parameter | Type | Description |
---|---|---|
conversationType | ConversationType | Conversation type |
targetId | String | Conversation Id |
keyword | String | Search keyword |
startTime | long | Start time |
endTime | long | End time |
offset | int | Offset |
limit | int | Number of search results returned, limit must be greater than 0. The maximum value is 100. If it exceeds 100, 100 items are returned by default. |
callback | IRongCallback.ResultCallback<List<Message>> | Callback. The search result is a list of Message. |
Search Messages by User ID
Search for messages from a specified user ID in local storage. It supports searching for historical message records before a specified time point. The callback returns a paginated list of messages that meet the criteria.
ConversationType conversationType = ConversationType.PRIVATE;
String targetId = "conversation Id";
String userId = "user Id to query";
int count = 20;
long beginTime = 1585815113;
RongIMClient.getInstance().searchMessagesByUser(conversationType, targetId, userId, count, beginTime,
new RongIMClient.ResultCallback<Message>() {
@Override
public void onSuccess(List<Messag> messages) {
}
@Override
public void onError(RongIMClient.ErrorCode errorCode) {
}
});
Parameter | Type | Description |
---|---|---|
conversationType | ConversationType | Conversation type |
targetId | String | Conversation ID. |
userId | String | User ID to query. |
count | int | Number of search results returned. The maximum value is 100. If it exceeds 100, 100 items are returned by default. |
beginTime | long | Start time of the query. Pass 0 to start searching from the latest message. Pass a non-zero value to search from that time backward. |
callback | IRongCallback.ResultCallback<List<Message>> | Callback. The search result is a list of Message. |