搜索本地消息
SDK 从 5.3.4 版本 开始支持超级群本地消息搜索功能。
SDK 允许 App 通过关键词、用户 ID 等条件搜索指定超级群频道或多个频道中已拉取到本地的消息,支持按时间段搜索。
并非所有消息类型均支持关键字搜索:
- 内置的消息类型中文本消息(TextMessage),文件消息(FileMessage),和图文消息
RichContentMessage
类型默认实现了MessageContent#getSearchableWord
方法。 - 自定义消息类型也可以支持关键字搜索,需要您参考文档自行实现。详见自定义消息类型。
请注意,消息搜索仅查询本地数据库。超级群默认每次连接时只同步频道最后一条消息,因此超级群业务本地消息记录可能不完整,需要 App 自行将消息拉取到本地(一般在进入会话页面时主动获取历史消息)。
融云提供搜索超级群历史消息记录的 IM Server API。详见服务端文档搜索超级群消息。
搜索会话
按关键字搜索本地所有会话。返回符合条件的会话列表 SearchConversationResult。请注意,超级群业务中单个会话(conversation
)仅对应单个超级群频道。
String keyword = "搜索的关键字";
Conversation.ConversationTypes[] conversationTypes = {ConversationType.ULTRA_GROUP};
String[] messageTypeObjectNames = {"RC:TxtMsg"};
ChannelClient.getInstance().searchConversationForAllChannel(keyword, conversationTypes, messageTypeObjectNames,
new IRongCoreCallback.ResultCallback<List<SearchConversationResult>>() {
@Override
public void onSuccess(List<SearchConversationResult> searchConversationResults) {
}
@Override
public void onError(IRongCoreEnum.CoreErrorCode errorCode) {
}
});
如果搜索条件与搜索结果包含多个会话类型,您可以通过返回结果中 Conversation 对象的会话类型(ConversationType
)字段进行分类或筛选。
参数 | 类型 | 说明 |
---|---|---|
keyword | String | 搜索的关键字 |
conversationTypes | Conversation.ConversationType[] | 会话类型列表,包含 ConversationType,例如 ConversationType.ULTRA_GROUP 、ConversationTypes.PRIVATE ,ConversationTypes.GROUP 。 |
objName | String[] | 消息类型列表,默认仅支持内置类型 RC:TxtMsg (文本消息)、RC:FileMsg (文件消息)、RC:ImgTextMsg (图文消息 )。 |
callback | IRongCoreCallback.ResultCallback<List<SearchConversationResult>> | 回调。搜索结果为 SearchConversationResult 列表。请注意,针对超级群,单个会话(conversation )对应单个超级群频道。 |
搜索单个频道的消息
获取包含关键词的会话列表后,可以搜索指定单个会话中符合条件的消息。在实现超级群消息搜索时,App 可以先通过搜索会话获取符合条件的超级群会话列表,再搜索指定频道中符合条件的消息。
搜索单个频道的消息需要 App 同时提供超级群 ID(targetId
) 与 频道 ID(channelId
)。
根据关键字搜索指定频道的消息
在本地存储中根据关键字搜索指定频道会话中的消息,支持搜索指定时间点之前的历史消息记录。回调中分页返回包含指定关键字的消息列表。
ConversationType conversationType = ConversationType.ULTRA_GROUP;
String targetId = "超级群 ID";
String channelId = "超级群频道 ID";
String keyword = "搜索的关键字";
int count = 20;
long beginTime = 122323344;
ChannelClient.getInstance().searchMessages(conversationType, targetId, channelId, keyword, count, beginTime,
new IRongCoreCallback.ResultCallback<List<Message>>() {
@Override
public void onSuccess(List<Message> messages) {
}
@Override
public void onError(IRongCoreEnum.CoreErrorCode errorCode) {
}
});
参数 | 类型 | 说明 |
---|---|---|
conversationType | ConversationType | 会话类型。超级群会话传入 ConversationType.ULTRA_GROUP 。 |
targetId | String | 超级群 ID。 |
channelId | String | 超级群频道 ID。 |
keyword | String | 搜索的关键字。 |
count | int | 每页的数量,每页数量建议最多 100 条。传 0 时返回所有搜索到的消息。 |
beginTime | long | 查询记录的起始时间。传 0 时从最新消息开始搜索。非 0 时从该时间往前搜索。 |
callback | IRongCoreCallback.ResultCallback<List<Message>> | 回调。搜索结果为 Message 列表。 |
根据关键字搜索指定时间段内指定频道的消息
SDK 支持将关键字搜索的范围限制在指定时间段内。回调中分页返回包含指定关键字和时间段要求的消息列表。
ConversationType conversationType = ConversationType.ULTRA_GROUP;
String targetId = "超级群 ID";
String channelId = "超级群频道 ID";
String keyword = "123";
long startTime = 0;
long endTime = 1585815113;
int offset = 0;
int limit = 80;
ChannelClient.getInstance().searchMessages(conversationType, targetId, channelId, keyword, startTime, endTime, offset, limit,
new IRongCoreCallback.ResultCallback<List<Message>>(){
@Override
public void onSuccess(List<Message> messages) {
}
@Override
public void onError(IRongCoreEnum.CoreErrorCode e) {
}
});
limit
参数控制返回的搜索结果数量,取值范围为 [1-100]。超过 100 默认使用最大值 100。
参数 | 类型 | 说明 |
---|---|---|
conversationType | ConversationType | 会话类型。超级群会话传入 ConversationType.ULTRA_GROUP 。 |
targetId | String | 超级群 ID |
channelId | String | 超级群频道 ID。 |
keyword | String | 搜索的关键字 |
startTime | long | 开始时间 |
endTime | long | 结束时间 |
offset | int | 偏移量 |
limit | int | 返回的搜索结果数量,limit 需大于 0。最大值为 100。超过 100 时默认返回 100 条。 |
callback | IRongCallback.ResultCallback<List<Message>> | 回调。搜索结果为 Message 列表。 |
根据用户 ID 搜索指定频道的消息
在本地存储中根据搜索来自指定用户 ID 的消息,支持搜索指定时间点之前的历史消息记录。回调中分页返回包含符合条件的消息列表。
ConversationType conversationType = ConversationType.ULTRA_GROUP;
String targetId = "超级群 ID";
String channelId = "超级群频道 ID";
String userId = "查询的用户 Id";
int count = 20;
long beginTime = 1585815113;
ChannelClient.getInstance().searchMessagesByUser(conversationType, targetId, channelId, userId, count, beginTime,
new IRongCoreCallback.ResultCallback<Message>() {
@Override
public void onSuccess(List<Message> messages) {
}
@Override
public void onError(IRongCoreEnum.CoreErrorCode errorCode) {
}
});
参数 | 类型 | 说明 |
---|---|---|
conversationType | ConversationType | 会话类型。超级群会话传入 ConversationType.ULTRA_GROUP 。 |
targetId | String | 超级群 ID。 |
channelId | String | 超级群频道 ID。 |
userId | String | 要查询的用户 ID。 |
count | int | 返回的搜索结果数量。最大值为 100。超过 100 时默认返回 100 条。 |
beginTime | long | 查询记录的起始时间。传 0 时从最新消息开始搜索。非 0 时从该时间往前搜索。 |
callback | IRongCallback.ResultCallback<List<Message>> | 回调。搜索结果为 Message 列表。 |
搜索多个频道的消息
App 如需搜索指定超级群的消息,并已持有超级群 ID(targetId
),可以直接使用以下接口搜索本地已接收、已存储的消息。符合搜索条件的消息可能来自多个频道。
根据关键字搜索指定超级群本地所有频道的消息
搜索指定超级群存储在本地的消息,支持搜索指定时间点之前的历史消息记录。回调中分页返回包含指定关键字的消息列表。
String targetId = "超级群 ID";
ConversationType conversationType = ConversationType.ULTRA_GROUP;
String keyword = "搜索的关键字";
int count = 20;
long timestamp = "122323344";
ChannelClient.getInstance().searchMessageForAllChannel(targetId, conversationType, keyword, count, timestamp,
new IRongCoreCallback.ResultCallback<List<Message>>() {
@Override
public void onSuccess(List<Message> messages) {
}
@Override
public void onError(IRongCoreEnum.CoreErrorCode errorCode) {
}
});
参数 | 类型 | 说明 |
---|---|---|
targetId | String | 超级群 ID。 |
conversationType | ConversationType | 会话类型。超级群会话传入 ConversationType.ULTRA_GROUP 。 |
keyword | String | 搜索的关键字。 |
count | int | 每页的数量,每页数量建议最多 100 条。传 0 时返回所有搜索到的消息。 |
beginTime | long | 查询记录的起始时间。传 0 时从最新消息开始搜索。非 0 时从该时间往前搜索。 |
callback | IRongCoreCallback.ResultCallback<List<Message>> | 回调。搜索结果为 Message 列表。 |