logo
On this page

Search for local messages


Overview

With the ZIM SDK, you can search for local messages of individual or group conversations by keywords, user ID, and other conditions. You can also search for conversations based on local messages.

Search local messages of a specific conversation

After creating a ZIM object and logging in, call the searchLocalMessages interface, pass in the parameters conversationID, conversationType, and config, and set the search conditions (such as keywords) to get the list of local messages that meet the conditions in a specific conversation.

The list of messages that meet the conditions will be returned through the ZIMMessagesSearchedCallback callback interface and classified by the corresponding conversation.

Sample code
// Search for local text messages containing "zegocloud" in a single session within 7 days.
auto endTimestamp = std::chrono::duration_cast<std::chrono::milliseconds>(
                        std::chrono::system_clock::now().time_since_epoch())
                        .count();
auto startTimestamp = endTimestamp - (7 * 24 * 60 * 60 * 1000);

// Conversation ID
// For one-on-one conversations, the conversationID is the userID of the other party.
// For group conversations, the conversationID is the groupID of the group.
std::string conversationID = "conv";
auto conversationType = zim::ZIMConversationType::ZIM_CONVERSATION_TYPE_PEER;

auto searchConfig = zim::ZIMMessageSearchConfig();
// Number of search results
searchConfig.count = 20;
// Specify the search order as descending from the last (time) message stored locally
searchConfig.order = zim::ZIMMessageOrder::ZIM_MESSAGE_ORDER_DESCENDING;
// Set the keyword to "zegocloud", supports up to 5 keywords. When multiple keywords are set, the search results only show local messages that contain all the keywords at the same time.
searchConfig.keywords.emplace_back("zegocloud");
// Specify the message type as text
searchConfig.messageTypes.emplace_back(zim::ZIMMessageType::ZIM_MESSAGE_TYPE_TEXT);
// Start time of the search
searchConfig.startTime = startTimestamp;
// End time of the search
searchConfig.endTime = endTimestamp;

zim_->searchLocalMessages(
    conversationID, conversationType, searchConfig,
    [=](const std::string &conversationID, zim::ZIMConversationType conversationType,
        const std::vector<std::shared_ptr<zim::ZIMMessage>> &messageList,
        const std::shared_ptr<zim::ZIMMessage> &nextMessage,
        const zim::ZIMError &errorInfo) {
        // Developers can listen to this callback to get the searched message list.
    });
1
Copied!

Search for global local messages

Search results categorized by conversation

After creating a ZIM object and logging in, call the searchGlobalLocalMessages interface, pass in the config parameter to set the search conditions (such as keywords), and globally search for local messages that meet the conditions.

The list of matched messages will be returned and categorized by conversation through the ZIMMessagesGlobalSearchedCallback callback interface.

Sample code
// Globally search for local text messages containing "zegocloud" within 7 days.
auto endTimestamp = std::chrono::duration_cast<std::chrono::milliseconds>(
                        std::chrono::system_clock::now().time_since_epoch())
                        .count();
auto startTimestamp = endTimestamp - (7 * 24 * 60 * 60 * 1000);

auto searchConfig = zim::ZIMMessageSearchConfig();
// Number of search results
searchConfig.count = 20;
// Specify the search order as descending from the last (time) message stored locally
searchConfig.order = zim::ZIMMessageOrder::ZIM_MESSAGE_ORDER_DESCENDING;
// Set the keyword to "zegocloud", supports up to 5 keywords. When multiple keywords are set, the search results only show local messages that contain all the keywords at the same time.
searchConfig.keywords.emplace_back("zegocloud");
// Specify the message type as text
searchConfig.messageTypes.emplace_back(zim::ZIMMessageType::ZIM_MESSAGE_TYPE_TEXT);
// Start time of the search
searchConfig.startTime = startTimestamp;
// End time of the search
searchConfig.endTime = endTimestamp;

zim_->searchGlobalLocalMessages(
    searchConfig, [=](const std::vector<std::shared_ptr<zim::ZIMMessage>> &messageList,
                        const std::shared_ptr<zim::ZIMMessage> &nextMessage,
                        const zim::ZIMError &errorInfo) {
        // Developers can listen to this callback to get the searched conversation list.
    });
1
Copied!

Search for conversations Based on Local Messages

After creating a ZIM object and logging in, call the searchLocalConversations interface, pass in the config parameter to set the search conditions related to local messages, and globally search for conversations that meet the conditions.

The list of matched conversations will be returned through the ZIMConversationsSearchedCallback callback interface.

Sample code
auto endTimestamp = std::chrono::duration_cast<std::chrono::milliseconds>(
                        std::chrono::system_clock::now().time_since_epoch())
                        .count();
auto startTimestamp = endTimestamp - (7 * 24 * 60 * 60 * 1000);

// Search for local text messages containing the keyword "zegocloud" within 7 days to obtain the corresponding conversation list
auto searchConfig = zim::ZIMConversationSearchConfig();
searchConfig.totalConversationCount = 10;
searchConfig.conversationMessageCount = 3;
searchConfig.nextFlag = 0;
searchConfig.keywords.emplace_back("zegocloud");
searchConfig.messageTypes.emplace_back(zim::ZIMMessageType::ZIM_MESSAGE_TYPE_TEXT);
searchConfig.startTime = startTimestamp;
searchConfig.endTime = endTimestamp;

zim_->searchLocalConversations(
    searchConfig,
    [=](const std::vector<zim::ZIMConversationSearchInfo> &conversationSearchInfoList,
        unsigned int nextFlag, const zim::ZIMError &errorInfo) {
        // Developers can listen to this callback to get the searched conversation information
    });
1
Copied!

Previous

Set message extension field

Next

Respond to messages with emoticons