搜索消息
SDK 从 1.1.0 版本开始提供了本地消息搜索功能,允许 App 用户通过关键词、用户 userId 等条件搜索指定的单个会话中的消息,支持按时间段搜索。消息搜索仅查询本地数据库中的消息,返回包含指定关键字或符合全部搜索条件的消息列表。
支持关键字搜索的消息:
- 内置的消息类型中文本消息(TextMessage),文件消息(FileMessage).
- 自定义消息类型实现
getSearchableWord
也可以支持关键字搜索,需要您参考文档自行实现。详见自定义消息类型。
如何实现基于关键字的全局搜索:
- 根据关键字搜索本地存储的全部会话,获取包含关键字的会话列表。
- 根据搜索会话返回的会话列表数据,调用搜索单个会话的方法,搜索符合条件的消息。
根据关键字搜索本地会话
按关键字搜索本地存储的所有会话,获取符合条件的会话列 表。请使用 searchConversationsWithResult
方法。
接口原型
TypeScript
/**
* 根据关键字搜索本地会话。
* @param conTypes 搜索的会话类型列表
* @param keyword 搜索的关键字,长度范围 [1, 256]
* @param objNameList 消息类型数组。用于搜索指定类型的消息;为空代表所有所有类型消息
* @returns 搜索到的会话列表
* @version 1.2.0
*/
public searchConversationsWithResult(
typeList: List<ConversationType>,
keyword: string,
objNameList: List<string> | null,
): Promise<IAsyncResult<List<SearchConversationResult>>>
参数说明
参数名 | 类型 | 详细说明 |
---|---|---|
typeList | List | 要搜索的会话类型列表(如单聊/群聊/系统会话等) |
keyword | string | 搜索的关键字,长度范围 [1, 256]) |
objNameList | List | 消息类型数组。用于搜索指定类型的消息;为空代表所有所有类型消息 |
示例代码
TypeScript
let conTypeList = new List<ConversationType>();
conTypeList.add(ConversationType.Private);
conTypeList.add(ConversationType.Group);
let keyword = "关键字";
IMEngine.getInstance().searchConversationsWithResult(conTypeList, keyword, null).then(result => {
if (EngineError.Success !== result.code) {
// 搜索会话失败
return;
}
if (!result.data) {
// 搜索的会话内容 为空
return;
}
// 搜索的结果
let searchResultList = result.data as List<SearchConversationResult>;
});
在指定单个会话中搜索
获取包含关键词的会话列表后,可以搜索指定单个会话中符合条件的消息。
根据关键字搜索消息
您可以在本地存储中根据关键字搜索指定会话中的消息,支持搜索指定时间点之前的历史消息记录。回调中分页返回包含指定关键字的消息列表。
接口原型
TypeScript
public searchMessages(conId: ConversationIdentifier, keyword: string, objNameList: List<string> | null, startTime: number, count: number): Promise<IAsyncResult<List<Message>>>;
参数说明
参数 | 类型 | 说明 |
---|---|---|
conId | ConversationIdentifier | 会话标识。 |
keyword | string | 搜索的关键字。 |
objectNames | string 集合 | 消息类型列表,用于搜索指定类型的消息;为空代表所有所有类型消息。 |
startTime | number | 查询记录的起始时间,毫秒时间戳。传 0 时从最新消息开始搜索。非 0 时从该时间往前搜索。 |
count | number | 每页的数量,每页数量建议最多 100 条。传 0 时返回所有搜索到的消息。 |
示例代码
TypeScript
let conId = new ConversationIdentifier();
conId.conversationType = ConversationType.Private;
conId.targetId = "会话 ID";
let keyword = "需要搜索的关键字";
let objNameList = new List<string>();
objNameList.add(TextMessageObjectName);
let startTime = Date.now();
let count = 10;
IMEngine.getInstance().searchMessages(conId, keyword, objNameList, startTime, count).then(result => {
if (EngineError.Success !== result.code) {
// 搜索消息失败
return;
}
if (!result.data) {
// 搜索消息为空
return;
}
// 搜索到的消息
let msgList = result.data as List<Message>;
});
根据关键字搜索指定时间段的消息
您可以将关键字搜索的范围限制在指定时间段内。回调中分页返回包含指定关键字和时间段要求的消息列表。
接口原型
TypeScript
public earchMessagesInTimeRange(conId: ConversationIdentifier, keyword: string, option: ISearchMessageInTimeRangeOption): Promise<IAsyncResult<List<Message>>>;
参数说明
limit
参数控制返回的搜索结果数量,取值范围为 [1-100]。超过 100 默认使用最大值 100。
参数 | 类型 | 说明 |
---|---|---|
conId | ConversationIdentifier | 会话标识 |
keyword | String | 搜索的关键字 |
option | ISearchMessageInTimeRangeOption | 搜素配置 |
示例代码
TypeScript
let conId = new ConversationIdentifier();
conId.conversationType = ConversationType.Private;
conId.targetId = "会话 ID";
let keyword = "需要搜索的关键字";
let option: ISearchMessageInTimeRangeOption = {
startTime: Date.now() - 24 * 60 * 60 * 1000,
endTime: Date.now(),
offset: 0,
limit: 10
}
IMEngine.getInstance().searchMessagesInTimeRange(conId, keyword, option).then(result => {
if (EngineError.Success !== result.code) {
// 搜索消息失败
result;
}
if (!result.data) {
// 搜素消息为空
return;
}
// 搜索的消息列表
let msgList = result.data as List<Message>;
});
根据用户 ID 搜索消息
您可以在本地存储中根据搜索来自指定用户 userId 的消息,支持搜索指定时间点之前的历史消息记录。回调中分页返回包含符合条件的消息列表。
接口原型
TypeScript
public searchMessagesByUser(conId: ConversationIdentifier, userId: string, startTime: number, count: number): Promise<IAsyncResult<List<Message>>>;
参数说明
参数 | 类型 | 说明 |
---|---|---|
conId | ConversationIdentifier | 会话标识 |
userId | string | 要查询的用户 ID。 |
startTime | number | 查询记录的起始时间,毫秒时间戳。传 0 时从最新消息开始搜索。非 0 时从该时间往前搜索。 |
count | number | 返回的搜索结果数量。最大值为 100。超过 100 时默认返回 100 条。 |
示例代码
TypeScript
let conId = new ConversationIdentifier();
conId.conversationType = ConversationType.Private;
conId.targetId = "会话 ID";
let userId = "用户 ID";
let startTime = Date.now();
let count = 10;
IMEngine.getInstance().searchMessagesByUser(conId, userId, startTime, count).then(result => {
if (EngineError.Success !== result.code) {
// 搜索消息失败
result;
}
if (!result.data) {
// 搜素消息为空
return;
}
// 搜索的消息列表
let msgList = result.data as List<Message>;
});
根据用户 ID 数组搜索消息
提示
SDK 1.3.0 版本后支持该方法。
您可以在本地存储中根据搜索来自指定用户 userId 数组的消息,支持搜索指定时间点之前的历史消息记录。回调中分页返回包含符合条件的消息列表。
接口原型
TypeScript
public searchMessagesByUsers(conId: ConversationIdentifier, sendIds: Array<string>, objNameArray: Array<string> | null, startTime: number, count: number, order: Order): Promise<IAsyncResult<List<Message>>>;
参数说明
参数 | 类型 | 说明 |
---|---|---|
conId | ConversationIdentifier | 会话标识 |
userIdArray | string 数组 | 要查询的用户 ID 数组。 |
startTime | number | 查询记录的起始时间,毫秒时间戳。传 0 时从最新消息开始搜索。非 0 时从该时间往前搜索。 |
count | number | 返回的搜索结果数量。最大值为 100。超过 100 时默认返回 100 条。 |
order | Order | 查询的顺序 |
示例代码
TypeScript
let conId = new ConversationIdentifier();
conId.conversationType = ConversationType.Private;
conId.targetId = "TestTargetId"; // 按需填写实际的会话 id
let userIdArray = ["UserId1","UserId2"];
let objNameArray: Array<string> | null = null;
let startTime = Date.now();
let count = 10;
let order = Order.Ascending;
IMEngine.getInstance().searchMessagesByUsers(conId, userIdArray, objNameArray, startTime, count, order).then(result => {
if (EngineError.Success !== result.code) {
// 失败
return;
}
if (!result.data) {
// 数据为空
return;
}
// 搜索到的消息列表
let msgList = result.data as List<Message>;
})