The search API allows clients to perform full text search across events in all rooms that the user has been in, including those that they have left. Only events that the user is allowed to see will be searched, e.g. it won’t include events in rooms that happened after you left.

Client behaviour

There is a single HTTP API for performing server-side search, documented below.

POST /_matrix/client/r0/search


Performs a full text search across different categories.

Rate-limited: Yes
Requires authentication: Yes

Request

Request parameters

query parameters
Name Type Description
next_batch string The point to return events from. If given, this should be a next_batch result from a previous call to this endpoint.

Request body

Name Type Description
search_categories Categories Required: Describes which categories to search in and their criteria.
Categories
Name Type Description
room_events Room Events Criteria Mapping of category name to search criteria.
Room Events Criteria
Name Type Description
event_context Include Event Context Configures whether any context for the events returned are included in the response.
filter Filter This takes a filter.
groupings Groupings Requests that the server partitions the result set based on the provided list of keys.
include_state Include current state Requests the server return the current state for each room returned.
keys [string] The keys to search. Defaults to all.
order_by Ordering The order in which to search for results. By default, this is "rank".
search_term string Required: The string to search events for
Include Event Context
Name Type Description
after_limit After limit How many events after the result are returned. By default, this is 5.
before_limit Before limit How many events before the result are returned. By default, this is 5.
include_profile Return profile information Requests that the server returns the historic profile information for the users that sent the events that were returned. By default, this is false.
Filter
Name Type Description
contains_url boolean If true, includes only events with a url key in their content. If false, excludes those events. If omitted, url key is not considered for filtering.
include_redundant_members boolean If true, sends all membership events for all events, even if they have already been sent to the client. Does not apply unless lazy_load_members is true. See Lazy-loading room members for more information. Defaults to false.
lazy_load_members boolean If true, enables lazy-loading of membership events. See Lazy-loading room members for more information. Defaults to false.
limit integer The maximum number of events to return.
not_rooms [string] A list of room IDs to exclude. If this list is absent then no rooms are excluded. A matching room will be excluded even if it is listed in the 'rooms' filter.
not_senders [string] A list of sender IDs to exclude. If this list is absent then no senders are excluded. A matching sender will be excluded even if it is listed in the 'senders' filter.
not_types [string] A list of event types to exclude. If this list is absent then no event types are excluded. A matching type will be excluded even if it is listed in the 'types' filter. A ‘*’ can be used as a wildcard to match any sequence of characters.
rooms [string] A list of room IDs to include. If this list is absent then all rooms are included.
senders [string] A list of senders IDs to include. If this list is absent then all senders are included.
types [string] A list of event types to include. If this list is absent then all event types are included. A '*' can be used as a wildcard to match any sequence of characters.
Groupings
Name Type Description
group_by Groups List of groups to request.
Group
Name Type Description
key Group Key Key that defines the group.

Request body example

{
  "search_categories": {
    "room_events": {
      "groupings": {
        "group_by": [
          {
            "key": "room_id"
          }
        ]
      },
      "keys": [
        "content.body"
      ],
      "order_by": "recent",
      "search_term": "martians and men"
    }
  }
}

Responses

Status Description
200 Results of the search.
400 Part of the request was invalid.
429 This request was rate-limited.

200 response

Results
Name Type Description
search_categories Result Categories Required: Describes which categories to search in and their criteria.
Result Categories
Name Type Description
room_events Result Room Events Mapping of category name to search criteria.
Result Room Events
Name Type Description
count integer An approximate count of the total number of results found.
groups { string: Group Key}

Any groups that were requested.

The outer string key is the group key requested (eg: room_id or sender). The inner string key is the grouped value (eg: a room’s ID or a user’s ID).

highlights Highlights List of words which should be highlighted, useful for stemming which may change the query terms.
next_batch Next Batch Token that can be used to get the next batch of results, by passing as the next_batch parameter to the next call. If this field is absent, there are no more results.
results Results List of results in the requested order.
state { string: Room State}

The current state for every room in the results. This is included if the request had the include_state key set with a value of true.

The string key is the room ID for which the State Event array belongs to.

Result
Name Type Description
context Event Context Context for result, if requested.
rank number A number that describes how closely this result matches the search. Higher is closer.
result Event The event that matched.
Event Context
Name Type Description
end End Token Pagination token for the end of the chunk
events_after Events After Events just after the result.
events_before Events Before Events just before the result.
profile_info { string: User Profile}

The historic profile information of the users that sent the events returned.

The string key is the user ID for which the profile belongs to.

start Start Token Pagination token for the start of the chunk
Event
Name Type Description
content object Required: The fields in this object will vary depending on the type of event. When interacting with the REST API, this is the HTTP body.
event_id string Required: The globally unique event identifier.
origin_server_ts integer Required: Timestamp in milliseconds on originating homeserver when this event was sent.
room_id string Required: The ID of the room associated with this event. Will not be present on events that arrive through /sync, despite being required everywhere else.
sender string Required: Contains the fully-qualified ID of the user who sent this event.
type string Required: The type of event. This SHOULD be namespaced similar to Java package naming conventions e.g. ‘com.example.subdomain.event.type’
unsigned UnsignedData Contains optional extra information about the event.
UnsignedData
Name Type Description
age integer The time in milliseconds that has elapsed since the event was sent. This field is generated by the local homeserver, and may be incorrect if the local time on at least one of the two servers is out of sync, which can cause the age to either be negative or greater than it actually is.
redacted_because Event The event that redacted this event, if any.
transaction_id string The client-supplied transaction ID, for example, provided via PUT /_matrix/client/r0/rooms/{roomId}/send/{eventType}/{txnId}, if the client being given the event is the same one which sent it.
User Profile
Name Type Description
avatar_url Avatar Url
displayname Display name
{
  "search_categories": {
    "room_events": {
      "count": 1224,
      "groups": {
        "room_id": {
          "!qPewotXpIctQySfjSy:localhost": {
            "next_batch": "BdgFsdfHSf-dsFD",
            "order": 1,
            "results": [
              "$144429830826TWwbB:localhost"
            ]
          }
        }
      },
      "highlights": [
        "martians",
        "men"
      ],
      "next_batch": "5FdgFsd234dfgsdfFD",
      "results": [
        {
          "rank": 0.00424866,
          "result": {
            "content": {
              "body": "This is an example text message",
              "format": "org.matrix.custom.html",
              "formatted_body": "<b>This is an example text message</b>",
              "msgtype": "m.text"
            },
            "event_id": "$144429830826TWwbB:localhost",
            "origin_server_ts": 1432735824653,
            "room_id": "!qPewotXpIctQySfjSy:localhost",
            "sender": "@example:example.org",
            "type": "m.room.message",
            "unsigned": {
              "age": 1234
            }
          }
        }
      ]
    }
  }
}

429 response

Name Type Description
errcode string Required: The M_LIMIT_EXCEEDED error code
error string A human-readable error message.
retry_after_ms integer The amount of time in milliseconds the client should wait before trying the request again.
{
  "errcode": "M_LIMIT_EXCEEDED",
  "error": "Too many requests",
  "retry_after_ms": 2000
}

Search Categories

The search API allows clients to search in different categories of items. Currently the only specified category is room_events.

room_events

This category covers all events that the user is allowed to see, including events in rooms that they have left. The search is performed on certain keys of certain event types.

The supported keys to search over are:

The search will not include rooms that are end to end encrypted.

The results include a rank key that can be used to sort the results by relevancy. The higher the rank the more relevant the result is.

The value of count gives an approximation of the total number of results. Homeservers may give an estimate rather than an exact value for this field.

Ordering

The client can specify the ordering that the server returns results in. The two allowed orderings are:

The default ordering is rank.

Groups

The client can request that the results are returned along with grouping information, e.g. grouped by room_id. In this case the response will contain a group entry for each distinct value of room_id. Each group entry contains at least a list of the event_ids that are in that group, as well as potentially other metadata about the group.

The current required supported groupings are:

Pagination

The server may return a next_batch key at various places in the response. These are used to paginate the results. To fetch more results, the client should send the same request to the server with a next_batch query parameter set to that of the token.

The scope of the pagination is defined depending on where the next_batch token was returned. For example, using a token inside a group will return more results from within that group.

The currently supported locations for the next_batch token are:

A server need not support pagination, even if there are more matching results. In that case, they must not return a next_batch token in the response.

Security considerations

The server must only return results that the user has permission to see.