Browse SDKs · HarmonyOS
Platform
SDKsHarmonyOSEnterprise

Search messages

Search through locally synchronized messages on the current device using keywords and filter criteria.

Copy

Local search queries device storage without making backend search requests. Passing an empty string "" for conversationID searches across all local conversations; for group chats, supply the conversation ID rather than the groupID.

Parameters

ParameterTypeRequiredDescription
conversationIDstringYesTarget conversation identifier; pass empty string "" for all local conversations.
keywordsstring[]YesArray of non-empty search keywords.
keywordMatchTypeKeywordListMatchTypeYesMatch type: match any keyword or match all keywords.
senderUserIDsstring[]YesArray of sender user IDs to filter by; pass empty array [] for no restriction.
messageTypesContentType[]YesArray of content types to filter by; pass empty array [] for all types.
searchTimePositionnumberYesUTC second timestamp search start point; 0 denotes current time.
searchTimePeriodnumberYesNumber of seconds backward from start point to search; 0 denotes unlimited.
paginationRequestPaginationYesPagination object with pageNumber and showNumber.

Code example

import sdk, {
  ContentType,
  KeywordListMatchType,
  SearchLocalMessagesReq,
  SearchLocalMessagesResp,
} from '@openimsdk/imsdk';

const request: SearchLocalMessagesReq = {
  conversationID: conversationID,
  keywords: [searchText.trim()],
  keywordMatchType: KeywordListMatchType.KeywordMatchOr,
  senderUserIDs: [],
  messageTypes: [ContentType.Text, ContentType.AtText],
  searchTimePosition: 0,
  searchTimePeriod: 0,
  pagination: { pageNumber: 1, showNumber: 20 },
};
const response: SearchLocalMessagesResp = await sdk.searchLocalMessages(request);
renderSearchResults(response.searchResultItems, response.count);

Return value

Returns a Promise resolving to SearchLocalMessagesResp:

PropertyTypeDescription
countnumberTotal number of matching messages found across all queried conversations.
searchResultItemsSearchResultItem[]Array of search result items grouped by conversation.

State changes after the call

Search results are grouped by conversation. When navigating to a search hit, use the item's conversationID and the message's clientMsgID. Reset pagination to page 1 whenever search filters or keywords change. This query searches only local messages synchronized to the current device; to jump to a message and expand surrounding context, see Load message context.