Browse SDKs · Android
Platform
SDKsAndroid

Search messages

Use searchLocalMessages to search messages synchronized to the current account’s local storage.

Copy

Perform keyword searches across messages stored in the current account's local database. You can scope searches to a specific conversation, filter by sender, restrict message types (such as text or files), and bound results by time ranges. Passing null for conversationID executes a global search across all local conversations.

Code example

String conversationID = null; // Pass null for global search across all conversations, or a specific ID
List<String> keywords = Collections.singletonList("proposal review");
int keywordMatchType = 1; // 1: match any keyword (OR), 0: match all keywords (AND)
List<String> senderIDs = null;
List<Integer> messageTypes = null;
int searchTimePosition = 0;
int searchTimePeriod = 0;
int pageIndex = 0;
int count = 20;

OpenIMClient.getInstance().messageManager.searchLocalMessages(
    new OnBase<SearchResult>() {
        @Override
        public void onSuccess(SearchResult result) {
            // result.getSearchResultItems() groups matching messages by conversation
        }

        @Override
        public void onError(int code, String error) {
            // Failed to search messages
        }
    },
    conversationID,
    keywords,
    keywordMatchType,
    senderIDs,
    messageTypes,
    searchTimePosition,
    searchTimePeriod,
    pageIndex,
    count
);

Keyword match modes

The keywordListMatchType parameter controls boolean keyword logic:

ModeValueDescription
Match all (AND)0Message content must contain all terms in the keyword list.
Match any (OR)1Message content must contain at least one term in the keyword list.

Parameters

ParameterTypeRequiredDescription
baseOnBase<SearchResult>YesCallback handler for the asynchronous request.
conversationIDStringNoTarget conversation ID to scope search. Pass null to search globally across all conversations.
keywordListList<String>YesList of search keywords. The search should not be initiated if keywords are empty.
keywordListMatchTypeintYesMatch logic: 0 for AND, 1 for OR.
senderUserIDListList<String>NoRestrict search to messages sent by specified user IDs. Pass null or empty list for all senders.
messageTypeListList<Integer>NoRestrict search to specific message types (such as text, image, etc.). Pass null or empty list for all types.
searchTimePositionintYesAnchor start timestamp in seconds; pass 0 to search backward from current time.
searchTimePeriodintYesTime span in seconds to search backward from the anchor; pass 0 for unbounded time.
pageIndexintYesPage index for pagination, starting from 0.
countintYesMaximum number of results to return per page.

Return value

Upon success, onSuccess returns a SearchResult object containing:

FieldDescription
totalCountTotal number of matching message records found.
searchResultItemsList of result items grouped by conversation (List<SearchResultItem>).

Each SearchResultItem contains the conversation's conversationID, display name, avatar, matched messageCount, and matched messageList. When the user selects a search result, call Load message context to locate the message and reconstruct its surrounding conversation thread.

State changes after the call

  1. Local search execution: This is a read-only query executed entirely against local SQLite storage without issuing network calls.