2018-06-23 16:28:00 +02:00
|
|
|
# Construct a narrow
|
|
|
|
|
|
|
|
A **narrow** is a set of filters for Zulip messages, that can be based
|
2024-05-14 21:04:46 +02:00
|
|
|
on many different factors (like sender, channel, topic, search
|
2022-10-28 19:42:37 +02:00
|
|
|
keywords, etc.). Narrows are used in various places in the the Zulip
|
2018-08-28 02:37:37 +02:00
|
|
|
API (most importantly, in the API for fetching messages).
|
2018-06-23 16:28:00 +02:00
|
|
|
|
2018-08-28 02:37:37 +02:00
|
|
|
It is simplest to explain the algorithm for encoding a search as a
|
2022-10-28 19:42:37 +02:00
|
|
|
narrow using a single example. Consider the following search query
|
|
|
|
(written as it would be entered in the Zulip web app's search box).
|
2024-05-14 21:04:46 +02:00
|
|
|
It filters for messages sent to channel `announce`, not sent by
|
2022-10-28 19:42:37 +02:00
|
|
|
`iago@zulip.com`, and containing the words `cool` and `sunglasses`:
|
2018-06-23 16:28:00 +02:00
|
|
|
|
|
|
|
```
|
2024-05-14 21:04:46 +02:00
|
|
|
channel:announce -sender:iago@zulip.com cool sunglasses
|
2018-06-23 16:28:00 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
This query would be JSON-encoded for use in the Zulip API using JSON
|
|
|
|
as a list of simple objects, as follows:
|
|
|
|
|
2019-06-16 02:53:16 +02:00
|
|
|
```json
|
2018-06-23 16:28:00 +02:00
|
|
|
[
|
|
|
|
{
|
2024-05-14 21:04:46 +02:00
|
|
|
"operator": "channel",
|
2018-06-23 16:28:00 +02:00
|
|
|
"operand": "announce"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"operator": "sender",
|
|
|
|
"operand": "iago@zulip.com",
|
|
|
|
"negated": true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"operator": "search",
|
|
|
|
"operand": "cool sunglasses"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
```
|
|
|
|
|
2022-10-28 19:42:37 +02:00
|
|
|
The Zulip help center article on [searching for messages](/help/search-for-messages)
|
|
|
|
documents the majority of the search/narrow options supported by the
|
|
|
|
Zulip API.
|
|
|
|
|
2024-05-14 21:04:46 +02:00
|
|
|
Note that many narrows, including all that lack a `channel` or `channels`
|
2022-10-28 19:42:37 +02:00
|
|
|
operator, search the current user's personal message history. See
|
|
|
|
[searching shared history](/help/search-for-messages#searching-shared-history)
|
|
|
|
for details.
|
|
|
|
|
2024-03-25 17:21:11 +01:00
|
|
|
Clients should note that the `is:unread` filter takes advantage of the
|
|
|
|
fact that there is a database index for unread messages, which can be an
|
|
|
|
important optimization when fetching messages in certain cases (e.g.
|
|
|
|
when [adding the `read` flag to a user's personal
|
|
|
|
messages](/api/update-message-flags-for-narrow)).
|
|
|
|
|
2024-04-12 13:46:36 +02:00
|
|
|
**Changes**: In Zulip 9.0 (feature level 250), support was added for
|
2024-05-14 21:04:46 +02:00
|
|
|
two filters related to channel messages: `channel` and `channels`. The
|
2024-04-12 13:46:36 +02:00
|
|
|
`channel` operator is an alias for the `stream` operator. The `channels`
|
|
|
|
operator is an alias for the `streams` operator. Both `channel` and
|
|
|
|
`channels` return the same exact results as `stream` and `streams`
|
|
|
|
respectively.
|
|
|
|
|
2024-04-22 12:14:11 +02:00
|
|
|
In Zulip 9.0 (feature level 249), support was added for a new filter,
|
|
|
|
`has:reaction`, which returns messages that have at least one [emoji
|
|
|
|
reaction](/help/emoji-reactions).
|
2024-04-02 06:24:20 +02:00
|
|
|
|
|
|
|
In Zulip 7.0 (feature level 177), support was added
|
2023-04-20 15:03:07 +02:00
|
|
|
for three filters related to direct messages: `is:dm`, `dm` and
|
|
|
|
`dm-including`. The `dm` operator replaced and deprecated the
|
|
|
|
`pm-with` operator. The `is:dm` filter replaced and deprecated
|
|
|
|
the `is:private` filter. The `dm-including` operator replaced and
|
|
|
|
deprecated the `group-pm-with` operator.
|
|
|
|
|
|
|
|
The `dm-including` and `group-pm-with` operators return slightly
|
|
|
|
different results. For example, `dm-including:1234` returns all
|
|
|
|
direct messages (1-on-1 and group) that include the current user
|
|
|
|
and the user with the unique user ID of `1234`. On the other hand,
|
|
|
|
`group-pm-with:1234` returned only group direct messages that included
|
|
|
|
the current user and the user with the unique user ID of `1234`.
|
|
|
|
|
|
|
|
Both `dm` and `is:dm` are aliases of `pm-with` and `is:private`
|
|
|
|
respectively, and return the same exact results that the deprecated
|
|
|
|
filters did.
|
|
|
|
|
2022-10-28 19:42:37 +02:00
|
|
|
## Narrows that use IDs
|
|
|
|
|
2023-07-17 15:39:05 +02:00
|
|
|
### Message IDs
|
|
|
|
|
2022-10-28 19:42:37 +02:00
|
|
|
The `near` and `id` operators, documented in the help center, use message
|
|
|
|
IDs for their operands.
|
|
|
|
|
|
|
|
* `near:12345`: Search messages around the message with ID `12345`.
|
|
|
|
* `id:12345`: Search for only message with ID `12345`.
|
|
|
|
|
2023-07-17 15:39:05 +02:00
|
|
|
The message ID operand for the `id` operator may be encoded as either a
|
|
|
|
number or a string. The message ID operand for the `near` operator must
|
|
|
|
be encoded as a string.
|
|
|
|
|
|
|
|
**Changes**: Prior to Zulip 8.0 (feature level 194), the message ID
|
|
|
|
operand for the `id` operator needed to be encoded as a string.
|
|
|
|
|
|
|
|
|
|
|
|
```json
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"operator": "id",
|
|
|
|
"operand": 12345
|
|
|
|
}
|
|
|
|
]
|
|
|
|
```
|
|
|
|
|
2024-05-14 21:04:46 +02:00
|
|
|
### Channel and user IDs
|
2023-07-17 15:39:05 +02:00
|
|
|
|
2022-10-28 19:42:37 +02:00
|
|
|
There are a few additional narrow/search options (new in Zulip 2.1)
|
2024-05-14 21:04:46 +02:00
|
|
|
that use either channel IDs or user IDs that are not documented in the
|
2022-10-28 19:42:37 +02:00
|
|
|
help center because they are primarily useful to API clients:
|
2019-06-08 23:21:01 +02:00
|
|
|
|
2024-05-14 21:04:46 +02:00
|
|
|
* `channel:1234`: Search messages sent to the channel with ID `1234`.
|
2022-10-28 19:42:37 +02:00
|
|
|
* `sender:1234`: Search messages sent by user ID `1234`.
|
2023-04-20 15:03:07 +02:00
|
|
|
* `dm:1234`: Search the direct message conversation between
|
2019-09-17 02:16:44 +02:00
|
|
|
you and user ID `1234`.
|
2023-04-20 15:03:07 +02:00
|
|
|
* `dm:1234,5678`: Search the direct message conversation between
|
2019-09-17 02:16:44 +02:00
|
|
|
you, user ID `1234`, and user ID `5678`.
|
2023-04-20 15:03:07 +02:00
|
|
|
* `dm-including:1234`: Search all direct messages (1-on-1 and group)
|
|
|
|
that include you and user ID `1234`.
|
2019-08-30 01:15:19 +02:00
|
|
|
|
2023-11-28 18:12:31 +01:00
|
|
|
!!! tip ""
|
|
|
|
|
|
|
|
A user ID can be found by [viewing a user's profile][view-profile]
|
2024-05-14 21:04:46 +02:00
|
|
|
in the web or desktop apps. A channel ID can be found when [browsing
|
|
|
|
channels][browse-channels] in the web or desktop apps.
|
2023-11-28 18:12:31 +01:00
|
|
|
|
2019-09-17 02:16:44 +02:00
|
|
|
The operands for these search options must be encoded either as an
|
2022-10-28 19:42:37 +02:00
|
|
|
integer ID or a JSON list of integer IDs. For example, to query
|
2023-06-16 01:15:50 +02:00
|
|
|
messages sent by a user 1234 to a direct message thread with yourself,
|
|
|
|
user 1234, and user 5678, the correct JSON-encoded query is:
|
2019-08-30 01:15:19 +02:00
|
|
|
|
|
|
|
```json
|
|
|
|
[
|
|
|
|
{
|
2023-04-20 15:03:07 +02:00
|
|
|
"operator": "dm",
|
2019-09-17 02:16:44 +02:00
|
|
|
"operand": [1234, 5678]
|
2019-08-30 01:15:19 +02:00
|
|
|
},
|
|
|
|
{
|
2019-09-17 02:16:44 +02:00
|
|
|
"operator": "sender",
|
|
|
|
"operand": 1234
|
2019-08-30 01:15:19 +02:00
|
|
|
}
|
|
|
|
]
|
|
|
|
```
|
2023-11-28 18:12:31 +01:00
|
|
|
|
|
|
|
[view-profile]: /help/view-someones-profile
|
2024-05-14 21:04:46 +02:00
|
|
|
[browse-channels]: /help/introduction-to-channels#browse-and-subscribe-to-channels
|