Message Search
You can use the IMLib SDK's search interfaces to retrieve message lists that match specified criteria such as keywords or message types within conversations, with support for time-based searches. You can also search for message lists in specific conversations based on user userId.
To enable keyword search for messages, the MessageContent must implement the getSearchableWord method:
- Built-in message types including text messages (TextMessage), file messages (FileMessage), and rich content messages (
RichContentMessage) have implemented theMessageContent#getSearchableWordmethod by default. - Custom message types can also support keyword search by implementing this method. For details, see Custom Message Types.
- Search all locally stored conversations by keyword to obtain a list of conversations containing the keyword.
- Using the conversation list returned from the search, call the single conversation search method to find messages that match the criteria.
Search for Matching Conversations
You can use searchConversations to search locally stored conversations that match the specified keyword and message type criteria, returning a list of SearchConversationResult. Starting from version 5.20.0, ultra group conversation information is supported. To enable ultra group functionality, please submit a [ticket].
Interface
RongIMClient.getInstance().searchConversations(keyword, conversationTypes, messageTypeObjectNames,callback);
Parameter Description
| Parameter | Type | Description |
|---|---|---|
| keyword | String | The keyword to search for. |
| conversationTypes | Conversation.ConversationType[] | A list of conversation types, including ConversationType. |
| objectNames | String[] | A list of message types. By default, only built-in types are supported: RC:TxtMsg (text messages), RC:FileMsg (file messages), and RC:ImgTextMsg (rich content messages). |
| callback | RongIMClient.ResultCallback<List<SearchConversationResult>> | Callback. The search result is a list of SearchConversationResult. |
Example Code
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) {
}
});
Search Within a Specific Conversation
You can use different method strategies to search for messages that match specified criteria within a single conversation.
Search Messages by Keyword
You can search for messages containing a specified keyword within a local database for a given conversation, supporting searches for historical messages before a specified timestamp. The callback returns a list of messages matching the search criteria.
Interface
RongIMClient.getInstance().searchMessages(conversationType, targetId, keyword, count, beginTime,callback);
Parameter Description
| Parameter | Type | Description |
|---|---|---|
| conversationType | ConversationType | The conversation type. |
| targetId | String | The conversation ID. |
| keyword | String | The keyword to search for. |
| count | int | The number of results per page. It is recommended to limit this to 100 messages. Passing 0 returns all matching messages. |
| beginTime | long | The starting timestamp for the search. Passing 0 starts the search from the latest message. A non-zero value searches backward from this timestamp. |
| callback | RongIMClient.ResultCallback<List<Message>> | Callback. The search result is a list of Message. |
Example Code
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) {
}
});
Search Messages by Keyword Within a Specified Time Range
You can search for local messages within a specified time range that match the keyword in a given conversation. The callback returns a list of messages matching the search criteria.
Interface
RongCoreClient.getInstance().searchMessages(conversationType, targetId, keyword, startTime, endTime, offset, limit,callback);
Parameter Description
The limit parameter controls the number of search results returned, with a valid range of [1-100]. Values exceeding 100 default to the maximum of 100.
| Parameter | Type | Description |
|---|---|---|
| conversationType | ConversationType | The conversation type. |
| targetId | String | The conversation ID. |
| keyword | String | The keyword to search for. |
| startTime | long | The start timestamp. |
| endTime | long | The end timestamp. |
| offset | int | The offset. |
| limit | int | The number of search results to return. limit must be greater than 0, with a maximum of 100. Values exceeding 100 default to 100. |
| callback | IRongCallback.ResultCallback<List<Message>> | Callback. The search result is a list of Message. |
Example Code
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) {
}
});
Search Messages by User ID
You can search for messages sent by a specified user userId within a given conversation, supporting searches for local historical messages before a specified timestamp. The callback returns a list of messages matching the search criteria.
Interface
RongIMClient.getInstance().searchMessagesByUser(conversationType, targetId, userId, count, beginTime,callback);
Parameter Description
| Parameter | Type | Description |
|---|---|---|
| conversationType | ConversationType | The conversation type. |
| targetId | String | The conversation ID. |
| userId | String | The user ID to query. |
| count | int | The number of search results to return. The maximum is 100. Values exceeding 100 default to 100. |
| beginTime | long | The starting timestamp for the search. Passing 0 starts the search from the latest message. A non-zero value searches backward from this timestamp. |
| callback | IRongCallback.ResultCallback<List<Message>> | Callback. The search result is a list of Message. |
Example Code
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<Message> messages) {
}
@Override
public void onError(RongIMClient.ErrorCode errorCode) {
}
});
Custom Search
Supported since RongCoreClient version 5.22.0.
You can customize search conditions through the SearchMessageParams object and pass it to the searchMessagesWithParams() interface for message searching.
Parameter Description
| Parameter | Type | Description |
|---|---|---|
params | SearchMessageParams | Parameter object |
callback | IRongCoreCallback.ExResultCallback | Result callback |
callbackparameter description:messages: List of returned search results.matchCount: Total number of matches for the search conditions.code: Error code.
Parameter Object Description
The parameter object supports flexible combination of search conditions, including keywords, quantity, sorting, offset, time range, conversation filtering, message filtering, etc.
| Parameter | Type | Description |
|---|---|---|
keyword | String | Search keyword |
limit | Int | Number of search results |
offset | Int | Search offset |
order | Order | Sorting method |
timeRange | TimeRange | Search time range (optional) |
conversationFilter | ConversationFilter | Conversation filter (optional) |
messageFilter | MessageFilter | Message filter (optional) |
- Detailed description of
timeRange:
// Timeline query:
// 0--------startTime--------------------endTime---------Now()
// Ascending order (order == RCOrderAscending) query:
// 0--------startTime|---->--------------endTime---------Now()
// Descending order (order == RCOrderDescending) query:
// 0--------startTime--------------<----|endTime---------Now()
// Descending order with offset query:
// 0--------startTime----------<----|----endTime---------Now()
Example Code
SearchMessageParams params = new SearchMessageParams();
params.setKeyword("123");
params.setLimit(10);
params.setOffset(0);
params.setOrder(SearchMessageParams.Order.DESC);
ConversationFilter conversationFilter = new ConversationFilter();
List<Conversation.ConversationType> conversationTypes = new ArrayList<>();
conversationTypes.add(Conversation.ConversationType.PRIVATE);
conversationTypes.add(Conversation.ConversationType.GROUP);
List<String> targetIds = new ArrayList<>();
targetIds.add("targetId2");
targetIds.add("targetId2");
conversationFilter.setConversationTypes(conversationTypes);
conversationFilter.setTargetIds(targetIds);
params.setConversationFilter(conversationFilter);
TimeRange timeRange = new TimeRange();
timeRange.setStartTime(0);
params.setTimeRange(timeRange);
RongCoreClient.getInstance().searchMessagesWithParams(params, new IRongCoreCallback.ExResultCallback<List<Message>, Integer>() {
@Override
public void onSuccess(List<Message> messages, Integer matchCount) {
// Business processing...
}
@Override
public void onError(IRongCoreEnum.CoreErrorCode error) {
// Error handling...
}
});