## Description
Implements comprehensive search functionality with advanced filtering
capabilities for Chatwoot (Linear: CW-5956).
This PR adds:
1. **Time-based filtering** for contacts and conversations (SQL-based
search)
2. **Advanced message search** with multiple filters
(OpenSearch/Elasticsearch-based)
- **`from` filter**: Filter messages by sender (format: `contact:42` or
`agent:5`)
- **`inbox_id` filter**: Filter messages by specific inbox
- **Time range filters**: Filter messages using `since` and `until`
parameters (Unix timestamps in seconds)
- **90-day limit enforcement**: Automatically limits searches to the
last 90 days to prevent performance issues
The implementation extends the existing `Enterprise::SearchService`
module for advanced features and adds time filtering to the base
`SearchService` for SQL-based searches.
## API Documentation
### Base URL
All search endpoints follow this pattern:
```
GET /api/v1/accounts/{account_id}/search/{resource}
```
### Authentication
All requests require authentication headers:
```
api_access_token: YOUR_ACCESS_TOKEN
```
---
## 1. Search All Resources
**Endpoint:** `GET /api/v1/accounts/{account_id}/search`
Returns results from all searchable resources (contacts, conversations,
messages, articles).
### Parameters
| Parameter | Type | Description | Required |
|-----------|------|-------------|----------|
| `q` | string | Search query | Yes |
| `page` | integer | Page number (15 items per page) | No |
| `since` | integer | Unix timestamp (contacts/conversations only) | No
|
| `until` | integer | Unix timestamp (contacts/conversations only) | No
|
### Example Request
```bash
curl -X GET "https://app.chatwoot.com/api/v1/accounts/1/search?q=customer" \
-H "api_access_token: YOUR_ACCESS_TOKEN"
```
### Example Response
```json
{
"payload": {
"contacts": [...],
"conversations": [...],
"messages": [...],
"articles": [...]
}
}
```
---
## 2. Search Contacts
**Endpoint:** `GET /api/v1/accounts/{account_id}/search/contacts`
Search contacts by name, email, phone number, or identifier with
optional time filtering.
### Parameters
| Parameter | Type | Description | Required |
|-----------|------|-------------|----------|
| `q` | string | Search query | Yes |
| `page` | integer | Page number (15 items per page) | No |
| `since` | integer | Unix timestamp - filter by last_activity_at | No |
| `until` | integer | Unix timestamp - filter by last_activity_at | No |
### Example Requests
**Basic search:**
```bash
curl -X GET "https://app.chatwoot.com/api/v1/accounts/1/search/contacts?q=john" \
-H "api_access_token: YOUR_ACCESS_TOKEN"
```
**Search contacts active in the last 7 days:**
```bash
SINCE=$(date -v-7d +%s)
curl -X GET "https://app.chatwoot.com/api/v1/accounts/1/search/contacts?q=john&since=${SINCE}" \
-H "api_access_token: YOUR_ACCESS_TOKEN"
```
**Search contacts active between 30 and 7 days ago:**
```bash
SINCE=$(date -v-30d +%s)
UNTIL=$(date -v-7d +%s)
curl -X GET "https://app.chatwoot.com/api/v1/accounts/1/search/contacts?q=john&since=${SINCE}&until=${UNTIL}" \
-H "api_access_token: YOUR_ACCESS_TOKEN"
```
### Example Response
```json
{
"payload": {
"contacts": [
{
"id": 42,
"email": "john@example.com",
"name": "John Doe",
"phone_number": "+1234567890",
"identifier": "user_123",
"additional_attributes": {},
"created_at": 1701234567
}
]
}
}
```
---
## 3. Search Conversations
**Endpoint:** `GET /api/v1/accounts/{account_id}/search/conversations`
Search conversations by display ID, contact name, email, phone number,
or identifier with optional time filtering.
### Parameters
| Parameter | Type | Description | Required |
|-----------|------|-------------|----------|
| `q` | string | Search query | Yes |
| `page` | integer | Page number (15 items per page) | No |
| `since` | integer | Unix timestamp - filter by last_activity_at | No |
| `until` | integer | Unix timestamp - filter by last_activity_at | No |
### Example Requests
**Basic search:**
```bash
curl -X GET "https://app.chatwoot.com/api/v1/accounts/1/search/conversations?q=billing" \
-H "api_access_token: YOUR_ACCESS_TOKEN"
```
**Search conversations active in the last 24 hours:**
```bash
SINCE=$(date -v-1d +%s)
curl -X GET "https://app.chatwoot.com/api/v1/accounts/1/search/conversations?q=billing&since=${SINCE}" \
-H "api_access_token: YOUR_ACCESS_TOKEN"
```
**Search conversations from last month:**
```bash
SINCE=$(date -v-30d +%s)
UNTIL=$(date +%s)
curl -X GET "https://app.chatwoot.com/api/v1/accounts/1/search/conversations?q=billing&since=${SINCE}&until=${UNTIL}" \
-H "api_access_token: YOUR_ACCESS_TOKEN"
```
### Example Response
```json
{
"payload": {
"conversations": [
{
"id": 123,
"display_id": 45,
"inbox_id": 1,
"status": "open",
"messages": [...],
"meta": {...}
}
]
}
}
```
---
## 4. Search Messages (Advanced)
**Endpoint:** `GET /api/v1/accounts/{account_id}/search/messages`
Advanced message search with multiple filters powered by
OpenSearch/Elasticsearch.
### Prerequisites
- OpenSearch/Elasticsearch must be running (`OPENSEARCH_URL` env var
configured)
- Account must have `advanced_search` feature flag enabled
- Messages must be indexed in OpenSearch
### Parameters
| Parameter | Type | Description | Required |
|-----------|------|-------------|----------|
| `q` | string | Search query | Yes |
| `page` | integer | Page number (15 items per page) | No |
| `from` | string | Filter by sender: `contact:{id}` or `agent:{id}` |
No |
| `inbox_id` | integer | Filter by specific inbox ID | No |
| `since` | integer | Unix timestamp - searches from this time (max 90
days ago) | No |
| `until` | integer | Unix timestamp - searches until this time | No |
### Important Notes
- **90-Day Limit**: If `since` is not provided, searches default to the
last 90 days
- If `since` exceeds 90 days, returns `422` error: "Search is limited to
the last 90 days"
- All time filters use message `created_at` timestamp
### Example Requests
**Basic message search:**
```bash
curl -X GET "https://app.chatwoot.com/api/v1/accounts/1/search/messages?q=refund" \
-H "api_access_token: YOUR_ACCESS_TOKEN"
```
**Search messages from a specific contact:**
```bash
curl -X GET "https://app.chatwoot.com/api/v1/accounts/1/search/messages?q=refund&from=contact:42" \
-H "api_access_token: YOUR_ACCESS_TOKEN"
```
**Search messages from a specific agent:**
```bash
curl -X GET "https://app.chatwoot.com/api/v1/accounts/1/search/messages?q=refund&from=agent:5" \
-H "api_access_token: YOUR_ACCESS_TOKEN"
```
**Search messages in a specific inbox:**
```bash
curl -X GET "https://app.chatwoot.com/api/v1/accounts/1/search/messages?q=refund&inbox_id=3" \
-H "api_access_token: YOUR_ACCESS_TOKEN"
```
**Search messages from the last 7 days:**
```bash
SINCE=$(date -v-7d +%s)
curl -X GET "https://app.chatwoot.com/api/v1/accounts/1/search/messages?q=refund&since=${SINCE}" \
-H "api_access_token: YOUR_ACCESS_TOKEN"
```
**Search messages between specific dates:**
```bash
SINCE=$(date -v-30d +%s)
UNTIL=$(date -v-7d +%s)
curl -X GET "https://app.chatwoot.com/api/v1/accounts/1/search/messages?q=refund&since=${SINCE}&until=${UNTIL}" \
-H "api_access_token: YOUR_ACCESS_TOKEN"
```
**Combine all filters:**
```bash
SINCE=$(date -v-14d +%s)
curl -X GET "https://app.chatwoot.com/api/v1/accounts/1/search/messages?q=refund&from=contact:42&inbox_id=3&since=${SINCE}" \
-H "api_access_token: YOUR_ACCESS_TOKEN"
```
**Attempt to search beyond 90 days (returns error):**
```bash
SINCE=$(date -v-120d +%s)
curl -X GET "https://app.chatwoot.com/api/v1/accounts/1/search/messages?q=refund&since=${SINCE}" \
-H "api_access_token: YOUR_ACCESS_TOKEN"
```
### Example Response (Success)
```json
{
"payload": {
"messages": [
{
"id": 789,
"content": "I need a refund for my purchase",
"message_type": "incoming",
"created_at": 1701234567,
"conversation_id": 123,
"inbox_id": 3,
"sender": {
"id": 42,
"type": "contact"
}
}
]
}
}
```
### Example Response (90-day limit exceeded)
```json
{
"error": "Search is limited to the last 90 days"
}
```
**Status Code:** `422 Unprocessable Entity`
---
## 5. Search Articles
**Endpoint:** `GET /api/v1/accounts/{account_id}/search/articles`
Search help center articles by title or content.
### Parameters
| Parameter | Type | Description | Required |
|-----------|------|-------------|----------|
| `q` | string | Search query | Yes |
| `page` | integer | Page number (15 items per page) | No |
### Example Request
```bash
curl -X GET "https://app.chatwoot.com/api/v1/accounts/1/search/articles?q=installation" \
-H "api_access_token: YOUR_ACCESS_TOKEN"
```
### Example Response
```json
{
"payload": {
"articles": [
{
"id": 456,
"title": "Installation Guide",
"slug": "installation-guide",
"portal_slug": "help",
"account_id": 1,
"category_name": "Getting Started",
"status": "published",
"updated_at": 1701234567
}
]
}
}
```
---
## Technical Implementation
### SQL-Based Search (Contacts, Conversations, Articles)
- Uses PostgreSQL `ILIKE` queries by default
- Optional GIN index support via `search_with_gin` feature flag for
better performance
- Time filtering uses `last_activity_at` for contacts/conversations
- Returns paginated results (15 per page)
### Advanced Search (Messages)
- Powered by OpenSearch/Elasticsearch via Searchkick gem
- Requires `OPENSEARCH_URL` environment variable
- Requires `advanced_search` account feature flag
- Enforces 90-day lookback limit via
`Limits::MESSAGE_SEARCH_TIME_RANGE_LIMIT_DAYS`
- Validates inbox access permissions before filtering
- Returns paginated results (15 per page)
---
## Type of change
- [x] New feature (non-breaking change which adds functionality)
- [x] Enhancement (improves existing functionality)
---
## How Has This Been Tested?
### Unit Tests
- **Contact Search Tests**: 3 new test cases for time filtering
(`since`, `until`, combined)
- **Conversation Search Tests**: 3 new test cases for time filtering
- **Message Search Tests**: 10+ test cases covering:
- Individual filters (`from`, `inbox_id`, time range)
- Combined filters
- Permission validation for inbox access
- Feature flag checks
- 90-day limit enforcement
- Error handling for exceeded time limits
### Test Commands
```bash
# Run all search controller tests
bundle exec rspec spec/controllers/api/v1/accounts/search_controller_spec.rb
# Run search service tests (includes enterprise specs)
bundle exec rspec spec/services/search_service_spec.rb
```
### Manual Testing Setup
A rake task is provided to create 50,000 test messages across multiple
inboxes:
```bash
# 1. Create test data
bundle exec rake search:setup_test_data
# 2. Start OpenSearch
mise elasticsearch-start
# 3. Reindex messages
rails runner "Message.search_index.import Message.all"
# 4. Enable feature flag
rails runner "Account.first.enable_features('advanced_search')"
# 5. Test via API or Rails console
```
---
## Checklist
- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my code
- [x] I have commented on my code, particularly in hard-to-understand
areas
- [x] I have made corresponding changes to the documentation (this PR
description)
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged and published in downstream
modules
---
## Additional Notes
### Requirements
- **OpenSearch/Elasticsearch**: Required for advanced message search
- Set `OPENSEARCH_URL` environment variable
- Example: `export OPENSEARCH_URL=http://localhost:9200`
- **Feature Flags**:
- `advanced_search`: Account-level flag for message advanced search
- `search_with_gin` (optional): Account-level flag for GIN-based SQL
search
### Performance Considerations
- 90-day limit prevents expensive long-range queries on large datasets
- GIN indexes recommended for high-volume search on SQL-based resources
- OpenSearch/Elasticsearch provides faster full-text search for messages
### Breaking Changes
- None. All new parameters are optional and backward compatible.
### Frontend Integration
- Frontend PR tracking advanced search UI will consume these endpoints
- Time range pickers should convert JavaScript `Date` to Unix timestamps
(seconds)
- Date conversion: `Math.floor(date.getTime() / 1000)`
### Error Handling
- Invalid `from` parameter format is silently ignored (filter not
applied)
- Time range exceeding 90 days returns `422` with error message
- Missing `q` parameter returns `422` (existing behavior)
- Unauthorized inbox access is filtered out (no error, just excluded
from results)
---------
Co-authored-by: iamsivin <iamsivin@gmail.com>
Co-authored-by: Sivin Varghese <64252451+iamsivin@users.noreply.github.com>
Co-authored-by: Pranav <pranav@chatwoot.com>
Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
380 lines
8.7 KiB
JavaScript
380 lines
8.7 KiB
JavaScript
import ContactAPI from 'dashboard/api/contacts';
|
|
import {
|
|
DATE_RANGE_TYPES,
|
|
generateURLParams,
|
|
parseURLParams,
|
|
fetchContactDetails,
|
|
} from '../searchHelper';
|
|
|
|
// Mock ContactAPI
|
|
vi.mock('dashboard/api/contacts', () => ({
|
|
default: {
|
|
show: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
describe('#generateURLParams', () => {
|
|
it('returns empty object when no parameters provided', () => {
|
|
expect(generateURLParams({})).toEqual({});
|
|
});
|
|
|
|
it('generates params with from parameter', () => {
|
|
const result = generateURLParams({ from: 'agent:123' });
|
|
expect(result).toEqual({ from: 'agent:123' });
|
|
});
|
|
|
|
it('generates params with inbox_id parameter', () => {
|
|
const result = generateURLParams({ in: 456 });
|
|
expect(result).toEqual({ inbox_id: 456 });
|
|
});
|
|
|
|
it('generates params with all basic parameters', () => {
|
|
const result = generateURLParams({
|
|
from: 'contact:789',
|
|
in: 123,
|
|
});
|
|
expect(result).toEqual({
|
|
from: 'contact:789',
|
|
inbox_id: 123,
|
|
});
|
|
});
|
|
|
|
describe('with date range', () => {
|
|
it('generates params with date range type only', () => {
|
|
const result = generateURLParams({
|
|
dateRange: { type: DATE_RANGE_TYPES.LAST_7_DAYS },
|
|
});
|
|
expect(result).toEqual({
|
|
range: 'last_7_days',
|
|
});
|
|
});
|
|
|
|
it('generates params with BETWEEN date range', () => {
|
|
const result = generateURLParams({
|
|
dateRange: {
|
|
type: DATE_RANGE_TYPES.BETWEEN,
|
|
from: 1640995200,
|
|
to: 1672531199,
|
|
},
|
|
});
|
|
expect(result).toEqual({
|
|
range: 'between',
|
|
since: 1640995200,
|
|
until: 1672531199,
|
|
});
|
|
});
|
|
|
|
it('generates params with CUSTOM date range', () => {
|
|
const result = generateURLParams({
|
|
dateRange: {
|
|
type: DATE_RANGE_TYPES.CUSTOM,
|
|
from: 1640995200,
|
|
to: 1672531199,
|
|
},
|
|
});
|
|
expect(result).toEqual({
|
|
range: 'custom',
|
|
since: 1640995200,
|
|
until: 1672531199,
|
|
});
|
|
});
|
|
|
|
it('handles date range with missing from/to values', () => {
|
|
const result = generateURLParams({
|
|
dateRange: {
|
|
type: DATE_RANGE_TYPES.BETWEEN,
|
|
from: null,
|
|
to: undefined,
|
|
},
|
|
});
|
|
expect(result).toEqual({
|
|
range: 'between',
|
|
});
|
|
});
|
|
});
|
|
|
|
it('generates params with all parameters combined', () => {
|
|
const result = generateURLParams({
|
|
from: 'agent:456',
|
|
in: 789,
|
|
dateRange: {
|
|
type: DATE_RANGE_TYPES.BETWEEN,
|
|
from: 1640995200,
|
|
to: 1672531199,
|
|
},
|
|
});
|
|
expect(result).toEqual({
|
|
from: 'agent:456',
|
|
inbox_id: 789,
|
|
range: 'between',
|
|
since: 1640995200,
|
|
until: 1672531199,
|
|
});
|
|
});
|
|
|
|
describe('when advanced search is disabled', () => {
|
|
it('returns empty object when isAdvancedSearchEnabled is false', () => {
|
|
const result = generateURLParams(
|
|
{
|
|
from: 'agent:123',
|
|
in: 456,
|
|
dateRange: {
|
|
type: DATE_RANGE_TYPES.BETWEEN,
|
|
from: 1640995200,
|
|
to: 1672531199,
|
|
},
|
|
},
|
|
false
|
|
);
|
|
expect(result).toEqual({});
|
|
});
|
|
|
|
it('strips all filter params when feature flag is disabled', () => {
|
|
const result = generateURLParams(
|
|
{
|
|
from: 'contact:789',
|
|
in: 123,
|
|
},
|
|
false
|
|
);
|
|
expect(result).toEqual({});
|
|
});
|
|
|
|
it('strips date range params when feature flag is disabled', () => {
|
|
const result = generateURLParams(
|
|
{
|
|
dateRange: {
|
|
type: DATE_RANGE_TYPES.LAST_7_DAYS,
|
|
from: 1640995200,
|
|
to: 1672531199,
|
|
},
|
|
},
|
|
false
|
|
);
|
|
expect(result).toEqual({});
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('#parseURLParams', () => {
|
|
it('returns default values for empty query', () => {
|
|
const result = parseURLParams({});
|
|
expect(result).toEqual({
|
|
from: null,
|
|
in: null,
|
|
dateRange: {
|
|
type: undefined,
|
|
from: null,
|
|
to: null,
|
|
},
|
|
});
|
|
});
|
|
|
|
it('parses from parameter', () => {
|
|
const result = parseURLParams({ from: 'agent:123' });
|
|
expect(result).toEqual({
|
|
from: 'agent:123',
|
|
in: null,
|
|
dateRange: {
|
|
type: undefined,
|
|
from: null,
|
|
to: null,
|
|
},
|
|
});
|
|
});
|
|
|
|
it('parses inbox_id parameter as number', () => {
|
|
const result = parseURLParams({ inbox_id: '456' });
|
|
expect(result).toEqual({
|
|
from: null,
|
|
in: 456,
|
|
dateRange: {
|
|
type: undefined,
|
|
from: null,
|
|
to: null,
|
|
},
|
|
});
|
|
});
|
|
|
|
it('parses explicit range parameter', () => {
|
|
const result = parseURLParams({
|
|
range: 'last_7_days',
|
|
since: '1640995200',
|
|
until: '1672531199',
|
|
});
|
|
expect(result).toEqual({
|
|
from: null,
|
|
in: null,
|
|
dateRange: {
|
|
type: 'last_7_days',
|
|
from: 1640995200,
|
|
to: 1672531199,
|
|
},
|
|
});
|
|
});
|
|
|
|
describe('inferred date range types', () => {
|
|
it('infers BETWEEN type when both since and until are present', () => {
|
|
const result = parseURLParams({
|
|
since: '1640995200',
|
|
until: '1672531199',
|
|
});
|
|
expect(result).toEqual({
|
|
from: null,
|
|
in: null,
|
|
dateRange: {
|
|
type: 'between',
|
|
from: 1640995200,
|
|
to: 1672531199,
|
|
},
|
|
});
|
|
});
|
|
|
|
it('prioritizes explicit range over inferred type', () => {
|
|
const result = parseURLParams({
|
|
range: 'custom',
|
|
since: '1640995200',
|
|
until: '1672531199',
|
|
});
|
|
expect(result).toEqual({
|
|
from: null,
|
|
in: null,
|
|
dateRange: {
|
|
type: 'custom',
|
|
from: 1640995200,
|
|
to: 1672531199,
|
|
},
|
|
});
|
|
});
|
|
});
|
|
|
|
it('parses all parameters combined', () => {
|
|
const result = parseURLParams({
|
|
from: 'contact:789',
|
|
inbox_id: '123',
|
|
range: 'between',
|
|
since: '1640995200',
|
|
until: '1672531199',
|
|
});
|
|
expect(result).toEqual({
|
|
from: 'contact:789',
|
|
in: 123,
|
|
dateRange: {
|
|
type: 'between',
|
|
from: 1640995200,
|
|
to: 1672531199,
|
|
},
|
|
});
|
|
});
|
|
|
|
describe('when advanced search is disabled', () => {
|
|
it('returns empty filters when isAdvancedSearchEnabled is false', () => {
|
|
const result = parseURLParams(
|
|
{
|
|
from: 'agent:123',
|
|
inbox_id: '456',
|
|
range: 'between',
|
|
since: '1640995200',
|
|
until: '1672531199',
|
|
},
|
|
false
|
|
);
|
|
expect(result).toEqual({
|
|
from: null,
|
|
in: null,
|
|
dateRange: {
|
|
type: null,
|
|
from: null,
|
|
to: null,
|
|
},
|
|
});
|
|
});
|
|
|
|
it('ignores all filter params from URL when feature flag is disabled', () => {
|
|
const result = parseURLParams(
|
|
{
|
|
from: 'contact:789',
|
|
inbox_id: '123',
|
|
},
|
|
false
|
|
);
|
|
expect(result).toEqual({
|
|
from: null,
|
|
in: null,
|
|
dateRange: {
|
|
type: null,
|
|
from: null,
|
|
to: null,
|
|
},
|
|
});
|
|
});
|
|
|
|
it('ignores date range params from URL when feature flag is disabled', () => {
|
|
const result = parseURLParams(
|
|
{
|
|
range: 'last_7_days',
|
|
since: '1640995200',
|
|
until: '1672531199',
|
|
},
|
|
false
|
|
);
|
|
expect(result).toEqual({
|
|
from: null,
|
|
in: null,
|
|
dateRange: {
|
|
type: null,
|
|
from: null,
|
|
to: null,
|
|
},
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('#fetchContactDetails', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
it('returns contact data on successful API call', async () => {
|
|
const mockContactData = {
|
|
id: 123,
|
|
name: 'John Doe',
|
|
email: 'john@example.com',
|
|
};
|
|
|
|
ContactAPI.show.mockResolvedValue({
|
|
data: {
|
|
payload: mockContactData,
|
|
},
|
|
});
|
|
|
|
const result = await fetchContactDetails(123);
|
|
expect(result).toEqual(mockContactData);
|
|
expect(ContactAPI.show).toHaveBeenCalledWith(123);
|
|
});
|
|
|
|
it('returns null on API error', async () => {
|
|
ContactAPI.show.mockRejectedValue(new Error('API Error'));
|
|
|
|
const result = await fetchContactDetails(123);
|
|
expect(result).toBeNull();
|
|
expect(ContactAPI.show).toHaveBeenCalledWith(123);
|
|
});
|
|
|
|
it('handles different contact ID types', async () => {
|
|
const mockContactData = { id: 456, name: 'Jane Doe' };
|
|
ContactAPI.show.mockResolvedValue({
|
|
data: { payload: mockContactData },
|
|
});
|
|
|
|
// Test with string ID
|
|
await fetchContactDetails('456');
|
|
expect(ContactAPI.show).toHaveBeenCalledWith('456');
|
|
|
|
// Test with number ID
|
|
await fetchContactDetails(456);
|
|
expect(ContactAPI.show).toHaveBeenCalledWith(456);
|
|
});
|
|
});
|