跳到主要内容

搜索消息

您可以通过 IMLib SDK 的相关搜索接口通过关键词、消息类型等条件搜索会话中的符合条件的消息列表,同时支持按时间段搜索。您也可以通过用户 userId 搜索指定会话中符合条件的消息列表。

支持关键字搜索的消息需要实现 MessageContentgetSearchableWord 方法:

  • 内置的消息类型中文本消息(TextMessage),文件消息(FileMessage),和图文消息RichContentMessage 类型默认实现了 MessageContent#getSearchableWord 方法。
  • 自定义消息类型也可以支持关键字搜索,需要您参考文档自行实现。详见自定义消息类型
  1. 根据关键字搜索本地存储的全部会话,获取包含关键字的会话列表。
  2. 根据搜索会话返回的会话列表数据,调用搜索单个会话的方法,搜索符合条件的消息。

搜索符合条件的会话

您可以通过 searchConversations 按关键字、消息类型搜索本地存储的符合查找条件的所有会话列表 SearchConversationResult。自 5.20.0 版本开始,支持返回超级群会话信息,如果需要超级群功能,请通过提交工单开启。

接口

Java
RongIMClient.getInstance().searchConversations(keyword, conversationTypes, messageTypeObjectNames,callback);

参数说明

参数类型说明
keywordString搜索的关键字
conversationTypesConversation.ConversationType[]会话类型列表,包含 ConversationType
objectNamesString[]消息类型列表,默认仅支持内置类型 RC:TxtMsg(文本消息)、RC:FileMsg(文件消息)、RC:ImgTextMsg(图文消息 )。
callbackRongIMClient.ResultCallback<List<SearchConversationResult>>回调。搜索结果为 SearchConversationResult 列表。

示例代码

Java
String 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) {

}
});

在指定单个会话中搜索

您可以通过不同的方法策略来搜索指定单个会话中符合条件的消息。

根据关键字搜索消息

您可以在本地数据库中根据关键字搜索指定会话中的消息,支持搜索指定时间点之前的历史消息记录,回调中返回符合搜索条件的消息列表。

接口

Java
RongIMClient.getInstance().searchMessages(conversationType, targetId, keyword, count, beginTime,callback);

参数说明

参数类型说明
conversationTypeConversationType会话类型。
targetIdString会话 ID。
keywordString搜索的关键字。
countint每页的数量,每页数量建议最多 100 条。传 0 时返回所有搜索到的消息。
beginTimelong查询记录的起始时间。传 0 时从最新消息开始搜索。非 0 时从该时间往前搜索。
callbackRongIMClient.ResultCallback<List<Message>>回调。搜索结果为 Message 列表。

示例代码

Java
ConversationType conversationType = ConversationType.PRIVATE;
String targetId = " 会话 Id ";
String 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) {

}
});

根据关键字搜索指定时间段的消息

您可以在指定会话中搜索指定的时间范围内,符合关键字的本地消息,回调中返回符合搜索条件的消息列表。

接口

Java
RongCoreClient.getInstance().searchMessages(conversationType, targetId, keyword, startTime, endTime, offset, limit,callback);

参数说明

limit 参数控制返回的搜索结果数量,取值范围为 [1-100]。超过 100 默认使用最大值 100。

参数类型说明
conversationTypeConversationType会话类型
targetIdString会话 Id
keywordString搜索的关键字
startTimelong开始时间
endTimelong结束时间
offsetint偏移量
limitint返回的搜索结果数量,limit 需大于 0。最大值为 100。超过 100 时默认返回 100 条。
callbackIRongCallback.ResultCallback<List<Message>>回调。搜索结果为 Message 列表。

示例代码

Java
ConversationType conversationType = ConversationType.PRIVATE;
String targetId = " 会话 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) {

}
});

根据用户 ID 搜索消息

您可以在指定会话中搜索指定用户 userId 发送的消息,支持搜索指定时间点之前的本地的历史消息记录,回调中返回符合搜索条件的消息列表。

接口

Java
RongIMClient.getInstance().searchMessagesByUser(conversationType, targetId, userId, count, beginTime,callback);

参数说明

参数类型说明
conversationTypeConversationType会话类型
targetIdString会话 ID。
userIdString要查询的用户 ID。
countint返回的搜索结果数量。最大值为 100。超过 100 时默认返回 100 条。
beginTimelong查询记录的起始时间。传 0 时从最新消息开始搜索。非 0 时从该时间往前搜索。
callbackIRongCallback.ResultCallback<List<Message>>回调。搜索结果为 Message 列表。

示例代码

Java
ConversationType conversationType = ConversationType.PRIVATE;
String targetId = " 会话 Id ";
String userId = "查询的用户 Id";
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) {

}
});

自定义搜索

提示

自 5.22.0 版本 RongCoreClient 开始支持。

您可通过 SearchMessageParams 对象自定义搜索条件,并将该对象传入接口 searchMessagesWithParams() 进行消息搜索。

参数说明

参数类型说明
paramsSearchMessageParams参数对象
callbackIRongCoreCallback.ExResultCallback结果回调
  • callback 参数说明:
    • messages:返回的搜索消息列表。
    • matchCount:搜索条件匹配到的总数。
    • code:错误码。

参数对象说明

参数对象支持灵活组合搜索条件,包括关键字、数量、排序、偏移、时间范围、会话过滤、消息过滤等。

参数类型说明
keywordString关键字
limitInt搜索数量
offsetInt搜索偏移量
orderOrder排序方式
timeRangeTimeRange搜索时间范围(可选)
conversationFilterConversationFilter会话过滤(可选)
messageFilterMessageFilter消息过滤(可选)
  • timeRange 时间范围详细说明:
Java
// 查询时间轴:
// 0--------startTime--------------------endTime---------Now()
// 正序(order == RCOrderAscending)查询:
// 0--------startTime|---->--------------endTime---------Now()
// 倒序(order == RCOrderDescending)查询:
// 0--------startTime--------------<----|endTime---------Now()
// 倒序有位移 (offset) 查询:
// 0--------startTime----------<----|----endTime---------Now()

示例代码

Java
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) {
// 业务处理...
}
@Override
public void onError(IRongCoreEnum.CoreErrorCode error) {
// 错误处理...
}
});