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 ZIMMessagesSearchedResult callback interface and classified by the corresponding conversation.

Sample code
// Search for local text messages containing "zegocloud" of the last seven days from a conversation.

var endTime = Date.now();
// Calculate the timestamp seven days ago.
var startTime = endTime - 7 * 24 * 3600 * 1000;
// The conversation ID.
// In a one-to-one chat, set `conversationID` to the ID of the other user.
// In a group chat, set `conversationID` to the ID of the group.
var conversationID = 'xxxx';
var conversationType = 0;

var config = {
    count: 20, // The number of messages in the search result.
    order: 0, // Specify to search for messages in local storage in reverse chronological order.
    keywords: ['zegocloud'], // Pass in the keyword `zegocloud`. Up to five keywords can be passed in. If you pass in multiple keywords, only local messages containing all these keywords are displayed in the search result.
    messageTypes: [1], // Specify the message type to text message.
    startTime, // Specify the start time of the search.
    endTime, // Specify the end time of the search.
};

zim.searchLocalMessages(conversationID, conversationType, config)
    .then(function ({ messageList }) {
        // Operation succeeded.
    })
    .catch(function (err) {
        // Operation failed.
    });
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 ZIMMessagesGlobalSearchedResult callback interface.

Sample code
// Search for local text messages containing "zegocloud" of the last seven days from all conversations.

var endTime = Date.now();
// Calculate the timestamp seven days ago.
var startTime = endTime - 7 * 24 * 3600 * 1000;

var config = {
    count: 20, // The number of messages in the search result.
    order: 0, // Specify to search for messages in local storage in reverse chronological order.
    keywords: ['zegocloud'], // Pass in the keyword `zegocloud`. Up to five keywords can be passed in. If you pass in multiple keywords, only local messages containing all these keywords are displayed in the search result.
    messageTypes: [1], // Specify the message type to text message.
    startTime, // Specify the start time of the search.
    endTime, // Specify the end time of the search.
};

zim.searchGlobalLocalMessages(config)
    .then(function ({ messageList }) {
        // Operation succeeded.
    })
    .catch(function (err) {
        // Operation failed.
    });
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 ZIMConversationsSearchedResult callback interface.

Sample code
// Search for local text messages containing "zegocloud" of the last seven days and have the result returned as a conversation list.

var endTime = Date.now();
// Calculate the timestamp seven days ago.
var startTime = endTime - 7 * 24 * 3600 * 1000;

var config = {
    totalConversationCount: 20, // The number of messages in the search result.
    conversationMessageCount: 3, // The latest three messages from each conversation are hit.
    nextFlag: 0,
    keywords: ['zegocloud'], // Pass in the keyword `zegocloud`. Up to five keywords can be passed in. If you pass in multiple keywords, only local messages containing all these keywords are displayed in the search result.
    messageTypes: [1], // Specify the message type to text message.
    startTime, // Specify the start time of the search.
    endTime, // Specify the end time of the search.
};

zim.searchLocalConversations(config)
    .then(function ({ conversationSearchInfoList, nextFlag }) {
        // Operation succeeded.
    })
    .catch(function (err) {
        // Operation failed.
    });
1
Copied!

Previous

Set message extension field

Next

Respond to messages with emoticons