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
|
2024-05-20 22:16:21 +02:00
|
|
|
keywords, etc.). Narrows are used in various places in 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
|
2024-07-04 12:14:37 +02:00
|
|
|
important optimization when fetching messages in certain cases (e.g.,
|
2024-03-25 17:21:11 +01:00
|
|
|
when [adding the `read` flag to a user's personal
|
|
|
|
messages](/api/update-message-flags-for-narrow)).
|
|
|
|
|
2024-07-17 04:57:40 +02:00
|
|
|
## Changes
|
|
|
|
|
2024-07-17 05:00:55 +02:00
|
|
|
* In Zulip 9.0 (feature level 271), support was added for a new filter
|
|
|
|
operator, `with`, which uses a [message ID](#message-ids) for its
|
|
|
|
operand, and is designed for creating permanent links to topics.
|
2024-07-17 04:57:40 +02:00
|
|
|
|
|
|
|
* In Zulip 9.0 (feature level 265), support was added for a new
|
|
|
|
`is:followed` filter, matching messages in topics that the current
|
|
|
|
user is [following](/help/follow-a-topic).
|
|
|
|
|
|
|
|
* In Zulip 9.0 (feature level 250), support was added for two filters
|
|
|
|
related to stream messages: `channel` and `channels`. The `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.
|
|
|
|
|
|
|
|
* 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).
|
|
|
|
|
|
|
|
* In Zulip 7.0 (feature level 177), support was added 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.
|
2023-04-20 15:03:07 +02:00
|
|
|
|
2022-10-28 19:42:37 +02:00
|
|
|
## Narrows that use IDs
|
|
|
|
|
2023-07-17 15:39:05 +02:00
|
|
|
### Message IDs
|
|
|
|
|
2024-07-17 05:00:55 +02:00
|
|
|
The `near`, `id` and `with` operators use message IDs for their
|
|
|
|
operands. The `near` and `id` operators are documented in the help
|
|
|
|
center [here](/help/search-for-messages#search-by-message-id).
|
|
|
|
|
|
|
|
The `with` operator is designed to be used for permanent links to topics,
|
|
|
|
which means they should continue to work when the topic is
|
|
|
|
[moved](/help/move-content-to-another-topic) or
|
|
|
|
[resolved](/help/resolve-a-topic). If the message with the specified ID
|
|
|
|
exists, and can be accessed by the user, then it will return messages
|
|
|
|
with the `channel`/`topic`/`dm` operators corresponding to the current
|
|
|
|
conversation containing that message, and replacing any such filters
|
|
|
|
included in the narrow.
|
|
|
|
|
|
|
|
* `with:12345`: Search for the conversation that contains the message
|
|
|
|
with ID `12345`.
|
2022-10-28 19:42:37 +02:00
|
|
|
* `near:12345`: Search messages around the message with ID `12345`.
|
2024-07-17 05:00:55 +02:00
|
|
|
* `id:12345`: Search for only the message with ID `12345`.
|
2022-10-28 19:42:37 +02:00
|
|
|
|
2024-06-28 08:57:02 +02:00
|
|
|
The message ID operand for the `with` and `id` operators may be encoded
|
|
|
|
as either a number or a string. The message ID operand for the `near`
|
|
|
|
operator must be encoded as a string.
|
2023-07-17 15:39:05 +02:00
|
|
|
|
|
|
|
**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
|