# Search for Activities

The `queryActivities` endpoint is designed for exploratory search and filtering across all activities. Example use-cases:

- A search bar where users can search by text
- A "My posts" page that shows a user their own posts across all feeds (useful for applications like Reddit, where users post across different communities instead of posting to a single feed like `user:alice`)

>
> **Info:** This endpoint is not a replacement for `getOrCreateFeed` - signs that you're using `queryActivities` instead of `getOrCreateFeed`:
>
> - You're using complicated filter expressions to tailor the search to the specific user
> - You're calling `queryActivities` automatically any time users open your application
>
> `getOrCreateFeed` has all the performance optimizations and rate limit settings to support those patterns; it also supports activity filters. Check out the [activity selectors guide](https://getstream.io/activity-feeds/docs/android/activity-selectors/) for more information.
>

## Scope

### When called from client-side

`queryActivities` endpoint searches for activities in `public` and `visible` feeds. It also respects activity visibility:

- `private` activities are only returned for the activity author
- if an activity has `tag` visibilty, and user doesn't have access to the full activity, only a preview is returned (for text based searches the search is performed across the whole activity text, not just the preview)

### When called from server-side

`queryActivities` endpoint searches for activities in all feeds, regardless of visibility. It also respects activity visibility:

- `private` activities are not returned, unless `include_private_activities` is set to `true` [see example below](#querying-private-activities))
- activities hidden by moderation (`shadow` or `remove`) are not returned, unless `include_moderated_activities` is set to `true` [see example below](#querying-moderated-activities)
- activities with `tag` visibility are returned with full content, not just a preview
- `user_id` can be passed to turn a server-side request into a client-side request [see example below](#user_id-for-server-side-requests)

## Examples

You can query & search activities. Here's an example of how to query activities:

```kotlin label="Kotlin"
val query = ActivitiesQuery(
    filter = ActivitiesFilterField.activityType.equal("post"),
    sort = listOf(ActivitiesSort(ActivitiesSortField.CreatedAt, SortDirection.REVERSE)),
    limit = 10
)
val activityList = client.activityList(query = query)
val activities: Result<List<ActivityData>> = activityList.get()
```

```java label="Java"
Map<String, Object> filter = new HashMap<>();
filter.put("activity_type", "post");

QueryActivitiesRequest request =
    QueryActivitiesRequest.builder().limit(10).filter(filter).build();

QueryActivitiesResponse response = feeds.queryActivities(request).execute().getData();
```

When searching activities, the `activity.current_feed` field contains information about the feed the activity belongs to. However, if an activity is posted to multiple feeds, this field will be empty. In this case, you can use the `activity.feeds` array to read all feed IDs the activity was posted to and fetch feeds separately if needed.

Search filter syntax emulates a [MongoDB style query syntax](https://getstream.io/docs/platform/query-syntax-operators/). (It emulates it, we don't use MongoDB, but the query syntax is nice here).

### Querying activities by text

```kotlin label="Kotlin"
// search for activities where the text includes the word 'popularity'.
val query = ActivitiesQuery(
    filter = ActivitiesFilterField.text.query("popularity")
)
val activityList = client.activityList(query = query)
val activities: Result<List<ActivityData>> = activityList.get()
```

```java label="Java"
Map<String, Object> filter = new HashMap<>();
filter.put("activity_type", "post");

QueryActivitiesRequest request =
    QueryActivitiesRequest.builder().limit(10).filter(filter).build();

QueryActivitiesResponse response = feeds.queryActivities(request).execute().getData();
```

### Querying activities by search data

Consider this example activity:

```json
{
  "id": "activity-123",
  "type": "post",
  "text": "Check out our spring sale!",
  "search_data": {
    "campaign": {
      "id": "spring-sale-2025",
      "location": {
        "mall": "yorkdale",
        "city": "toronto",
        "country": "canada"
      }
    }
  }
  // ... other activity fields
}
```

You can search this activity using the `search_data` field in several ways:

```kotlin label="Kotlin"
// search for activities associated with the campaign ID 'spring-sale-2025'
val searchValue = mapOf("campaign" to mapOf("id" to "spring-sale-2025"))
val query = ActivitiesQuery(
    filter = ActivitiesFilterField.searchData.contains(searchValue)
)
val activityList = client.activityList(query = query)
val activities: Result<List<ActivityData>> = activityList.get()

// search for activities where the campaign took place in a mall
val query2 = ActivitiesQuery(
    filter = ActivitiesFilterField.searchData.pathExists("campaign.location.mall")
)
val activityList2 = client.activityList(query = query2)
val activities2: Result<List<ActivityData>> = activityList2.get()
```

```java label="Java"
Map<String, Object> filter = new HashMap<>();
filter.put("activity_type", "post");

QueryActivitiesRequest request =
    QueryActivitiesRequest.builder().limit(10).filter(filter).build();

QueryActivitiesResponse response = feeds.queryActivities(request).execute().getData();
```

### Querying private activities

For server-side requests, you can set `include_private_activities` to `true` to include private activities in the results (the default is `false`).

```java label="Java"
Map<String, Object> filter = new HashMap<>();
// your filter here

QueryActivitiesRequest request = QueryActivitiesRequest.builder()
    .filter(filter)
    .includePrivateActivities(true)
    .build();

QueryActivitiesResponse response = feeds.queryActivities(request).execute().getData();
```

### Querying soft-deleted activities

By default, `queryActivities` excludes activities that have been soft-deleted (removed via `DELETE /activities/{id}` without `hard=true`). Set `include_soft_deleted_activities` to `true` (the default is `false`) to include them in the results. This flag is server-side only: client-side requests ignore it, and a server-side request that also sets `user_id` or `user` is rejected.

Soft-deleted activities returned this way include a `deleted_at` timestamp. Non-deleted activities in the same response omit that field.

```java label="Java"
Map<String, Object> filter = new HashMap<>();
// your filter here

QueryActivitiesRequest request = QueryActivitiesRequest.builder()
    .filter(filter)
    .includeSoftDeletedActivities(true)
    .build();

QueryActivitiesResponse response = feeds.queryActivities(request).execute().getData();
```

### Querying moderated activities

By default, activities hidden by moderation are not returned: a `shadow`ed activity is visible only to its author, and a `remove`d activity is visible to nobody. Set `include_moderated_activities` to `true` (the default is `false`) to include them, for example to build an internal moderation dashboard. Activities with any other moderation action (`flag`, `mask`, `keep`, ...) are always returned.

Each returned activity carries its `moderation_action`, so you can tell shadowed and removed activities apart from the rest. `include_moderated_activities` and `include_soft_deleted_activities` are independent: an activity that was both moderated and soft-deleted needs both flags.

>
> **Warning:** `include_moderated_activities` is **server-side only**. Client-side requests ignore it, and a server-side request that also sets `user_id` or `user` is rejected.
>

Moderation webhooks and the review queue give you activity IDs, so the recommended pattern is to look activities up by ID. Listing every shadowed activity of an app (filtering on `moderation_action`) is not supported.

```java label="Java"
Map<String, Object> filter = new HashMap<>();
filter.put("id", Map.of("$in", List.of("activity_123", "activity_456")));

QueryActivitiesRequest request = QueryActivitiesRequest.builder()
    .filter(filter)
    .includeModeratedActivities(true)
    .build();

QueryActivitiesResponse response = feeds.queryActivities(request).execute().getData();
```

The same rules apply to the embedded `parent` of a shared activity: a shadowed, removed, private or expired parent, or one authored by a blocked user, is omitted from the response unless the flags above allow it.

### `user_id` for server-side requests

For server-side requests, you can provide a `user_id` to perform the search as if it were made by that specific user. It turns the server-side request into a client-side request, so `include_private_activities`, `include_expired_activities`, `include_soft_deleted_activities` and `include_moderated_activities` can't be combined with it.

```java label="Java"
Map<String, Object> filter = new HashMap<>();
// your filter here

QueryActivitiesRequest request = QueryActivitiesRequest.builder()
    .filter(filter)
    .userId("john")
    .build();

QueryActivitiesResponse response = feeds.queryActivities(request).execute().getData();
```

## Activities Queryable Built-In Fields

| name            | type                                              | description                                                                                               | supported operations                                               | example                                                                                      |
| --------------- | ------------------------------------------------- | --------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------ | -------------------------------------------------------------------------------------------- |
| `id`            | string or list of strings                         | The ID of the activity                                                                                    | `$in`, `$eq`                                                       | `{ id: { $in: [ 'abc', 'xyz' ] } }`                                                          |
| `activity_type` | string or list of strings                         | The type of the activity                                                                                  | `$in`, `$eq`                                                       | `{ activity_type: { $in: [ 'abc', 'xyz' ] } }`                                               |
| `user_id`       | string or list of strings                         | The ID of the user who created the activity                                                               | `$in`, `$eq`                                                       | `{ user_id: { $in: [ 'abc', 'xyz' ] } }`                                                     |
| `text`          | string                                            | The text content of the activity                                                                          | `$eq`, `$q`, `$autocomplete`                                       | `{ text: { $q: 'popularity' } }`                                                             |
| `search_data`   | object                                            | The extra metadata for search indexing                                                                    | `$contains`, `$path_exists`                                        | `{ search_data: { $contains: { 'category': 'sports', 'status': 'active' } } }`               |
| `interest_tags` | list of strings                                   | Tags for user interests                                                                                   | `$eq`, `$contains`                                                 | `{ interest_tags: { $in: [ 'sports', 'music' ] } }`                                          |
| `filter_tags`   | list of strings                                   | Tags for filtering                                                                                        | `$eq`, `$contains`, `$in`                                          | `{ filter_tags: { $in: [ 'categoryA', 'categoryB' ] } }`                                     |
| `created_at`    | string, must be formatted as an RFC3339 timestamp | The time the activity was created                                                                         | `$eq`, `$gt`, `$lt`, `$gte`, `$lte`                                | `{ created_at: { $gte: '2023-12-04T09:30:20.45Z' } }`                                        |
| `popularity`    | number                                            | The popularity score of the activity                                                                      | `$eq`, `$ne`, `$gt`, `$lt`, `$gte`, `$lte`                         | `{ popularity: { $gte: 70 } }`                                                               |
| `near`          | object                                            | GEO point and a distance (in km) to search for activities within                                          | `$eq`                                                              | `{ near: { $eq: { lat: 40.0, lng: -74.0, distance: 200 } } }`                                |
| `within_bounds` | object                                            | GEO bounds to search for activities within                                                                | `$eq`                                                              | `{ within_bounds: { $eq: { ne_lat: 40.0, ne_lng: -115.0, sw_lat: 32.0, sw_lng: -125.0 } } }` |
| `hidden`        | bool                                              | `true` if an activity was hidden by the user. Hidden activities are excluded unless specified with filter | `$eq`                                                              | `{ hidden: { $eq: true }`                                                                    |
| `custom.<key>`  | string, number, boolean, or JSON (as stored)      | Values from the activity `custom` object. **Enterprise only.**                                            | `$eq`, `$gt`, `$gte`, `$lt`, `$lte`, `$in`, `$exists`, `$contains` | `{ 'custom.score': { $gte: 8 } }`                                                            |

### `$contains` on custom fields

The shape of the `$contains` value has to match the shape of the stored custom value:

- A scalar value matches when the custom field is an array containing that element. With `custom: { tags: ['urgent', 'billing'] }`, the filter `{ 'custom.tags': { $contains: 'urgent' } }` matches.
- An object value matches when the custom field is an object containing every given key and value (a subset match, the same as `search_data`). With `custom: { extra_context: { objectType: 'DONATION', target: '5379275' } }`, the filter `{ 'custom.extra_context': { $contains: { objectType: 'DONATION' } } }` matches.

A scalar value never matches an object-valued field, and an object value never matches an array-valued field. To require several array elements at once, combine one `$contains` per element with `$and`.

The filter syntax also supports `$or` and `$and`:

```kotlin label="Kotlin"
// Get all the activities where filter tags contain both "green" and "orange"
val filter = Filters.and(
  ActivitiesFilterField.filterTags.`in`("green"),
  ActivitiesFilterField.filterTags.`in`("orange"),
)
```

```java label="Java"
// Get all the activities where filter tags contain both "green" and "orange"
Map<String, Object> filter = new HashMap<>();
List<Map<String, Object>> andConditions = List.of(
    Map.of("filter_tags", List.of("green")),
    Map.of("filter_tags", List.of("orange"))
);
filter.put("$and", andConditions);
```

Be sure to reach out to support if you need additional query activity capabilities.

## Activities sort options

Fields:

- `created_at`
- `popularity`

Direction: `1` or `-1`

---

For the most recent version of this documentation, visit [https://getstream.io/activity-feeds/docs/android/query-activities/](https://getstream.io/activity-feeds/docs/android/query-activities/).