> ## Documentation Index
> Fetch the complete documentation index at: https://www.cometchat.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Search Messages

> Add full-text search across conversations and messages to a CometChat iOS UI Kit v6 app, open the result tapped, and scope a search to one chat.

> [accordion] AI Integration Quick Reference
>
> ```json theme={null}
> {
>   "products": ["CometChatSwiftUI", "CometChatUIKit"],
>   "component": "CometChatSearch",
>   "results": "onConversationClick: (Conversation) / set(onConversationClick:) (Conversation, IndexPath); onMessageClick: (BaseMessage) / set(onMessageClick:)",
>   "scope to a chat": "uid: / guid: (SwiftUI); user / group properties or set(user:) / set(group:) (UIKit)",
>   "filters": "SearchFilter: unread, groups, photos, videos, links, documents, audio — searchFilters, initialSearchFilter",
>   "scopes": "SearchScope: conversations, messages — searchScopes",
>   "entry": "CometChatConversations onSearchClick (SwiftUI) / onSearchClick (UIKit); a chat's header menu"
> }
> ```

`CometChatSearch` searches the user's conversations and messages as they type, with filter
chips for unread chats, groups, photos, videos, links, documents and audio. You open it from
the conversations list, or scope it to one chat from that chat's header, and route the result
the user taps.

## Components

| Component | Role |
| - | - |
| `CometChatSearch` | The search screen: a search bar, filter chips, conversation and message results. |
| `CometChatConversations` | Its search field is a button that calls `onSearchClick` — open search from there. |
| `CometChatMessageList` | Jumps to and highlights a message found: `goToMessageId:` / `goToMessage(withId:)`. |

## Integration Steps

### 1. Open search from the conversations list

The conversations list's search field does not filter the list; it calls `onSearchClick`.

> [tab] SwiftUI
>
> ```swift ChatsWithSearch.swift theme={null}
> import SwiftUI
> import CometChatSwiftUI
> import CometChatSDK
>
> struct ChatsWithSearch: View {
>     @State private var searching = false
>
>     var body: some View {
>         CometChatConversations(
>             onItemClick: { conversation in print("open", conversation.conversationId ?? "") },
>             onSearchClick: { searching = true }
>         )
>         .navigationDestination(isPresented: $searching) {
>             CometChatSearch(
>                 onConversationClick: { conversation in print("open", conversation.conversationId ?? "") },
>                 onMessageClick: { message in print("open at", message.id) },
>                 onBack: { searching = false }
>             )
>         }
>     }
> }
> ```

> [tab] UIKit
>
> ```swift theme={null}
> let conversations = CometChatConversations()
> conversations.set(onSearchClick: { [weak conversations] in
>     let search = CometChatSearch()
>     search.set(onConversationClick: { conversation, _ in
>         print("open", conversation.conversationId ?? "")
>     })
>     search.set(onMessageClick: { message in
>         print("open at", message.id)
>     })
>     conversations?.navigationController?.pushViewController(search, animated: true)
> })
> ```

### 2. Handle a conversation result

A conversation result hands you the `Conversation`. Its `conversationWith` is the `User` or
the `Group` — open your chat screen with it, as a tap in the conversations list does.

### 3. Handle a message result

A message result hands you the `BaseMessage`. Open its chat and jump to it:

* a group message's chat is `message.receiver` as a `Group`;
* a one-to-one message's chat is the other person — `message.receiver` when you sent it,
  `message.sender` otherwise;
* a reply in a thread has a `parentMessageId`; open the thread at the reply, or the chat at
  the parent.

> [tab] SwiftUI
>
> ```swift ChatAtMessage.swift theme={null}
> import SwiftUI
> import CometChatSwiftUI
> import CometChatSDK
>
> struct ChatAtMessage: View {
>     let user: CometChatSDK.User?
>     let group: CometChatSDK.Group?
>     /// The list jumps to it, highlights it, and sets it back to nil.
>     @State var jumpTo: MessageID?
>
>     var body: some View {
>         // … the header and the composer as bars over the list …
>         CometChatMessageList(user: user, group: group, goToMessageId: $jumpTo)
>     }
> }
> ```

> [tab] UIKit
>
> ```swift theme={null}
> let messageList = CometChatMessageList()
> messageList.set(user: guideUser)
> // After a message hit, or later on the open chat:
> messageList.goToMessage(withId: 42)
> ```

Host the header and the composer over the list as [Conversation List + Message View](https://www.cometchat.com/docs/ui-kit/ios/v6/conversation-message-view.md#step-2-set-up-the-message-screen) shows.

### 4. Scope search to one chat (optional)

Pass the chat's user or group and the search covers only its messages. Open it from the
chat's header menu:

> [tab] SwiftUI
>
> ```swift theme={null}
> CometChatSearch(uid: guideUser.uid, onMessageClick: { message in print("jump to", message.id) })
>
> // A group:
> CometChatSearch(guid: guideGroup.guid, onMessageClick: { message in print("jump to", message.id) })
> ```

> [tab] UIKit
>
> ```swift theme={null}
> let search = CometChatSearch()
> search.user = guideUser        // or search.group = guideGroup
> search.set(onMessageClick: { message in print("jump to", message.id) })
> ```

A hit in the chat that opened the search should take the user back there and jump the list to
the message, rather than open the chat a second time.

### 5. Configure filters and scopes (optional)

> [tab] SwiftUI
>
> ```swift theme={null}
> CometChatSearch(
>     searchScopes: [.messages],                       // conversations, messages
>     searchFilters: [.photos, .videos, .documents],   // which chips, in this order
>     initialSearchFilter: .photos                     // pre-selected
> )
> ```

> [tab] UIKit
>
> ```swift theme={null}
> let search = CometChatSearch()
> search.searchScopes = [.messages]
> search.searchFilters = [.photos, .videos, .documents]
> search.initialSearchFilter = .photos
> ```

`conversationsRequestBuilder:` and `messagesRequestBuilder:` narrow the requests further —
see [Search](https://www.cometchat.com/docs/ui-kit/ios/v6/search.md).

## Feature Matrix

| Feature | SwiftUI | UIKit |
| - | - | - |
| Search screen | `CometChatSearch(...)` | `CometChatSearch()` |
| Conversation result | `onConversationClick:` | `set(onConversationClick:)` |
| Message result | `onMessageClick:` | `set(onMessageClick:)` |
| Scope to a chat | `uid:` / `guid:` | `user` / `group` |
| Chips | `searchFilters:`, `initialSearchFilter:` | `searchFilters`, `initialSearchFilter` |
| Results kinds | `searchScopes:` | `searchScopes` |
| Jump to the message | `goToMessageId:` | `goToMessage(withId:)` |

## Next Steps

> [card] [Search](https://www.cometchat.com/docs/ui-kit/ios/v6/search.md)
>
> Every option of the search screen

> [card] [Conversations](https://www.cometchat.com/docs/ui-kit/ios/v6/conversations.md)
>
> The list search opens from

> [card] [Threaded Messages](https://www.cometchat.com/docs/ui-kit/ios/v6/guide-threaded-messages.md)
>
> Open a thread at a reply

> [card] [All Guides](https://www.cometchat.com/docs/ui-kit/ios/v6/guide-overview.md)
>
> Browse the other guides


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.