Browse SDKs · uni-app / uni-app x
SDKsuni-app

Search messages

OpenIM uni-app / uni-app x SDK guide for Search messages.

Copy

searchLocalMessages() searches messages already synchronized into the current user's local database. For a group, pass the group's conversationID, not the groupID used when sending. If you only have the group ID, first obtain the conversation ID with Get a conversation ID.

Use a backend search service for cross-user audit, complete server-side history, complex permission filtering, or global ranking. It can return conversationID and clientMsgID so the client can locate each hit.

Create a search query

keywordList accepts one or more terms. A normal search box represents one input, so trim it and reject empty values before calling the SDK.

import {
  OpenIMMessageTypeAtText,
  OpenIMMessageTypeText,
  searchLocalMessages,
  type OpenIMMessageItem,
  type OpenIMSearchMessageResult,
} from '@/uni_modules/unix-openim-sdk'

const result = await searchLocalMessages({
  conversationID,
  keywordList: [keyword.trim()],
  keywordListMatchType: 0,
  senderUserIDList: [],
  messageTypeList: [OpenIMMessageTypeText, OpenIMMessageTypeAtText],
  searchTimePosition: 0,
  searchTimePeriod: 0,
  pageIndex: 1,
  count: 20,
})

Narrow the query by sender, content type, and time window. In OpenIMSearchLocalMessagesParams, every filter and paging field except conversationID is required. Use an empty array for an unrestricted list and the server-defined 0 values when time is unrestricted.

const result = await searchLocalMessages({
  conversationID,
  keywordList: ['release'],
  keywordListMatchType: 0,
  senderUserIDList: [senderUserID],
  messageTypeList: [OpenIMMessageTypeText],
  searchTimePosition,
  searchTimePeriod,
  pageIndex: 1,
  count: 20,
})

Parameters

ParameterTypeRequiredDescription
conversationIDstring or nullNoConversation to search; omit it to search the current locally visible scope.
keywordListstring[]YesSearch terms.
keywordListMatchTypenumberYesMulti-keyword matching mode defined by the SDK contract.
senderUserIDListstring[]YesRestrict to these senders; use an empty array for no restriction.
messageTypeListOpenIMMessageType[]YesRestrict to these content types; use an empty array for no restriction.
searchTimePositionnumberYesEnd of the search window, as a Unix timestamp in seconds.
searchTimePeriodnumberYesNumber of seconds to search backward from the end position.
pageIndexnumberYesPage number; the first page is 1.
countnumberYesNumber of results per page.

Add the appropriate OpenIMMessageType constants when the UI searches images, files, or custom messages. Matching modes, time units, and page numbering must follow the SDK and server contract.

Handle paginated results

The Promise resolves to OpenIMSearchMessageResult | null:

FieldTypeDescription
totalCountnumberTotal number of messages matching the query.
searchResultItemsOpenIMSearchMessageResultItem[]Results grouped by conversation.

Each result item contains:

FieldTypeDescription
conversationIDstringOwning conversation.
conversationTypeOpenIMSessionTypeConversation type.
showName, faceURLstringConversation display name and avatar snapshot.
latestMsgSendTimenumber or nullLatest message time in this result group.
messageCountnumberNumber of matching messages in the group.
messageListOpenIMMessageItem[]Matching messages.

Preserve both the conversation ID and message ID when flattening grouped results:

type SearchMessageRow = {
  conversationID : string
  clientMsgID : string
  message : OpenIMMessageItem
}

function toSearchRows(result : OpenIMSearchMessageResult) : Array<SearchMessageRow> {
  const rows : Array<SearchMessageRow> = []
  result.searchResultItems.forEach((item) => {
    item.messageList.forEach((message) => {
      const clientMsgID = message.clientMsgID
      if (clientMsgID != null) {
        rows.push({ conversationID: item.conversationID, clientMsgID, message })
      }
    })
  })
  return rows
}

Keep all filters unchanged while incrementing pageIndex. When any condition changes, reset the page to 1 and clear old rows. Deduplicate by conversationID:clientMsgID; do not persist selection by array position. A search does not emit message events.

Handle changes to search results

A hit can be revoked or deleted while the search page is open, and newly synchronized messages can change the result set. Use the shared handlers described in Receive messages, Delete saved messages, and Revoke a message. This page owns querying and pagination, not duplicate event registrations.

Navigate with the result's conversationID and clientMsgID. To display nearby chat records, use the complete hit as the start point for Load message context, rather than assembling context with findMessageList().

Re-run the current page query when the UI needs a fresh snapshot. Search Promises, event increments, and reconciliation queries are independent. Clear previous-account search state after login changes.