iachat/config/locales
Vinay Keerthi 59cbf57e20
feat: Advanced Search Backend (#12917)
## 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>
2026-01-07 15:30:49 +05:30
..
am.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
ar.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
az.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
bg.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
bn.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
ca.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
cs.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
da.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
de.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
devise.am.yml chore: Update translations (#7728) 2023-08-15 13:47:38 -07:00
devise.ar.yml chore: Update translations (#9946) 2024-08-16 14:06:32 +05:30
devise.az.yml chore: Update translations (#10549) 2025-01-14 22:00:56 +05:30
devise.bg.yml chore: New Crowdin updates (#3616) 2021-12-19 11:38:02 +05:30
devise.bn.yml chore: Update translations (#12722) 2025-10-27 17:54:15 +05:30
devise.ca.yml Chore: New Crowdin translations (#747) 2020-04-23 23:52:16 +05:30
devise.cs.yml Chore: Add ro, fr, pt_BR translation files (#827) 2020-05-06 14:24:20 +05:30
devise.da.yml chore: Enable Danish language (#1443) 2020-11-24 23:07:51 +05:30
devise.de.yml chore: Update translations (#4099) 2022-03-17 20:02:32 +05:30
devise.el.yml Chore: New Crowdin translations (#747) 2020-04-23 23:52:16 +05:30
devise.en.yml Feature/update confirmation email information (#145) 2019-10-14 14:24:58 +05:30
devise.es.yml chore: New Crowdin Translation updates (#2063) 2021-04-19 15:10:16 +05:30
devise.et.yml chore: Update translations (#12722) 2025-10-27 17:54:15 +05:30
devise.fa.yml chore: Update translations, Add Norwegian(no) (#1675) 2021-01-28 14:03:52 +05:30
devise.fi.yml chore: Update translations (#1502) 2020-12-09 16:43:51 +05:30
devise.fr.yml Chore: New Crowdin translations (#747) 2020-04-23 23:52:16 +05:30
devise.he.yml chore: Update translations (#6895) 2023-04-17 14:45:49 +05:30
devise.hi.yml Chore: Include Tamil, Arabic, other language updates (#1018) 2020-07-08 00:59:30 +05:30
devise.hr.yml chore: Update translations (#9946) 2024-08-16 14:06:32 +05:30
devise.hu.yml chore: New translation updates 2021-03-16 00:00:53 +05:30
devise.hy.yml chore: Update translations 2023-03-08 14:00:00 +05:30
devise.id.yml chore: Enable Finnish, Indonesian languages (#1495) 2020-12-08 00:05:07 +05:30
devise.is.yml chore: Update translations from Crowdin (#5810) 2022-11-08 09:36:24 -08:00
devise.it.yml chore: Update to the latest translations (#3923) 2022-02-07 17:19:34 +05:30
devise.ja.yml chore: Update translations from Crowdin (#10686) 2025-02-12 12:34:34 +05:30
devise.ka.yml chore: Enable Thai language (th), update translations (#5095) 2022-07-26 09:52:59 +05:30
devise.ko.yml chore: Update translations from Crowdin (#5810) 2022-11-08 09:36:24 -08:00
devise.lt.yml chore: Update translations (#7347) 2023-06-19 17:27:43 +05:30
devise.lv.yml chore: Update translations from Crowdin (#5523) 2022-09-28 21:59:41 -07:00
devise.ml.yml chore: Update to the latest translations (#3923) 2022-02-07 17:19:34 +05:30
devise.ms.yml chore: New Translation updates (#5287) 2022-08-19 13:55:23 +05:30
devise.ne.yml chore: Update translations, Add Norwegian(no) (#1675) 2021-01-28 14:03:52 +05:30
devise.nl.yml Add new translations (#862) 2020-05-16 18:07:19 +05:30
devise.no.yml chore: Update translations, Add Norwegian(no) (#1675) 2021-01-28 14:03:52 +05:30
devise.pl.yml Chore: Add ro, fr, pt_BR translation files (#827) 2020-05-06 14:24:20 +05:30
devise.pt_BR.yml chore: Update translations (#1502) 2020-12-09 16:43:51 +05:30
devise.pt.yml Chore: Add pt and pt_BR translations (#813) 2020-05-04 17:33:55 +05:30
devise.ro.yml chore: Update translations from Crowdin (#4652) 2022-05-09 18:57:05 +05:30
devise.ru.yml chore: Enable Russian, update translations (#1159) 2020-08-22 17:54:16 +05:30
devise.sh.yml chore: Update translations (#6751) 2023-03-27 14:45:50 +05:30
devise.sk.yml chore: New Crowdin updates (#3616) 2021-12-19 11:38:02 +05:30
devise.sl.yml chore: Update translations (#10549) 2025-01-14 22:00:56 +05:30
devise.sq.yml chore: Update translations (#8016) 2023-09-29 15:46:29 +05:30
devise.sr.yml chore: Update translations from Crowdin (#5202) 2022-08-08 21:14:50 +05:30
devise.sv.yml chore: New translation updates 2021-03-16 00:00:53 +05:30
devise.ta.yml Chore: Include Tamil, Arabic, other language updates (#1018) 2020-07-08 00:59:30 +05:30
devise.th.yml chore: Update translations from Crowdin (#4652) 2022-05-09 18:57:05 +05:30
devise.tl.yml chore: Update translations from Crowdin (#9835) 2024-07-25 11:58:22 -07:00
devise.tr.yml chore: Enable Czech(cs), Turkish(tr), update translations (#1466) 2020-12-01 13:15:17 +05:30
devise.uk.yml chore: Update translations from Crowdin (#4652) 2022-05-09 18:57:05 +05:30
devise.ur_IN.yml chore: Update translations from Crowdin (#4665) 2022-05-13 11:38:49 +05:30
devise.ur.yml chore: Update translations from Crowdin (#4652) 2022-05-09 18:57:05 +05:30
devise.vi.yml chore: Enable Japanese, update translation from Crowdin (#1417) 2020-11-16 23:11:14 +05:30
devise.zh_CN.yml chore: Enable the languages no, zh-CN, hu (#2135) 2021-04-20 16:22:03 +05:30
devise.zh_TW.yml chore: Enable Danish language (#1443) 2020-11-24 23:07:51 +05:30
devise.zh.yml chore: New Translation updates (#3371) 2021-11-12 23:40:08 +05:30
el.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
en.yml feat: Advanced Search Backend (#12917) 2026-01-07 15:30:49 +05:30
es.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
et.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
fa.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
fi.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
fr.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
he.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
hi.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
hr.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
hu.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
hy.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
id.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
is.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
it.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
ja.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
ka.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
ko.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
lt.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
lv.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
ml.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
ms.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
ne.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
nl.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
no.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
pl.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
pt_BR.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
pt.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
ro.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
ru.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
secure_password.en.yml feat: Improved password security policy (#2345) 2021-06-07 17:26:08 +05:30
sh.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
sk.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
sl.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
sq.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
sr.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
sv.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
ta.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
th.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
tl.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
tr.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
uk.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
ur_IN.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
ur.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
vi.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
zh_CN.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
zh_TW.yml chore: Update translations (#13105) 2025-12-18 01:39:35 -08:00
zh.yml chore: New Translation updates (#3371) 2021-11-12 23:40:08 +05:30