Search

Last Edit: Jan 14 2021

Message search is built-in to the chat API. You can enable and/or disable the search indexing on a per channel type basis. The command shown below selects the channels in which John is a member. Next, it searches the messages in those channels for the keyword “'supercalifragilisticexpialidocious'”:


const filters = { members: { $in: ['john'] } };

const search = await client.search(
   filters,
   'supercalifragilisticexpialidocious',
   { limit: 2, offset: 0 },
);
                    

int offset = 0;
int limit = 10;
String query = "supercalifragilisticexpialidocious";
ArrayList<String> searchUsersList = new ArrayList();
searchUsersList.add("john");
FilterObject channelFilter = Filters.in("members", searchUsersList);
FilterObject messageFilter = Filters.autocomplete("text", query);

client.searchMessages(
        new SearchMessagesRequest(
                offset,
                limit,
                channelFilter,
                messageFilter
        )
).enqueue(listResult -> {
    if (listResult.isSuccess()) {
        List<Message> messages = listResult.data();
    } else {
        Log.e(TAG, String.format("There was an error %s", listResult.error(), listResult.error().getCause()));
    }
    return Unit.INSTANCE;
});
                    

let query = SearchQuery(filter: .in("members", ["john"]),
                        query: "supercalifragilisticexpialidocious",
                        pagination: [.channelsPageSize])
Client.shared.search(query: query) { (result) in
    // handle result
}
                    

val offset = 0
val limit = 10
val query = "supercalifragilisticexpialidocious"
val channelFilter: FilterObject = Filters.`in`("members", listOf("john"))
val messageFilter: FilterObject = Filters.autocomplete("text", query)

client.searchMessages(
    SearchMessagesRequest(
        offset, 
        limit, 
        channelFilter, 
        messageFilter
    )
).enqueue {
    if (it.isSuccess) {
        val messages: List<Message> = it.data()
    } else {
        Log.e(TAG, String.format("There was an error %s", it.error(), it.error().cause))
    }
}
                    

$filters = [ 'type' => 'messaging' , 'members' => [ '$in' => ['jenny'] ]];

$response = $client->search(
	$filters,
	'Hey',
	['limit' => 2, 'offset' => 0]
);
                    

filters = { "type": "messaging", "members": { "$in": ["john"] } };

results = client.search(
   filters,
   'supercalifragilisticexpialidocious',
   limit=2,
   offset=0,
);
                    

Pagination works via the standard limit and offset parameters. The first argument, filter, uses a MongoDB style query expression.

We do not run MongoDB on the backend, so only a subset of the standard MongoDB filters are supported.

Additionally, this endpoint can be used to search for messages that have attachments.


// Search by Attachment
const searchByAttachment = async () => {
  const messageFilters = { attachments: { $exists: true } };
  const response = await channel.search(messageFilters);
  return response;
};