Search messages
Search through locally synchronized messages on the current device using keywords and filter criteria.
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
| Parameter | Type | Required | Description |
|---|---|---|---|
conversationID | string | Yes | Target conversation identifier; pass empty string "" for all local conversations. |
keywords | string[] | Yes | Array of non-empty search keywords. |
keywordMatchType | KeywordListMatchType | Yes | Match type: match any keyword or match all keywords. |
senderUserIDs | string[] | Yes | Array of sender user IDs to filter by; pass empty array [] for no restriction. |
messageTypes | ContentType[] | Yes | Array of content types to filter by; pass empty array [] for all types. |
searchTimePosition | number | Yes | UTC second timestamp search start point; 0 denotes current time. |
searchTimePeriod | number | Yes | Number of seconds backward from start point to search; 0 denotes unlimited. |
pagination | RequestPagination | Yes | Pagination 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:
| Property | Type | Description |
|---|---|---|
count | number | Total number of matching messages found across all queried conversations. |
searchResultItems | SearchResultItem[] | 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.
Was this page helpful?