Files
leadchat/enterprise/lib/tasks/search.rake
Pranav 0c2ab7f5e7 feat(ee): Setup advanced, performant message search (#12193)
We now support searching within the actual message content, email
subject lines, and audio transcriptions. This enables a faster, more
accurate search experience going forward. Unlike the standard message
search, which is limited to the last 3 months, this search has no time
restrictions.

The search engine also accounts for small variations in queries. Minor
spelling mistakes, such as searching for slck instead of Slack, will
still return the correct results. It also ignores differences in accents
and diacritics, so searching for Deja vu will match content containing
Déjà vu.


We can also refine searches in the future by criteria such as:
- Searching within a specific inbox
- Filtering by sender or recipient
- Limiting to messages sent by an agent


Fixes https://github.com/chatwoot/chatwoot/issues/11656
Fixes https://github.com/chatwoot/chatwoot/issues/10669
Fixes https://github.com/chatwoot/chatwoot/issues/5910



---

Rake tasks to reindex all the messages. 

```sh
bundle exec rake search:all
```

Rake task to reindex messages from one account only
```sh
bundle exec rake search:account ACCOUNT_ID=1
```
2025-08-28 10:10:28 +05:30

50 lines
1.4 KiB
Ruby

module Tasks::SearchTaskHelpers
def check_opensearch_config
if ENV['OPENSEARCH_URL'].blank?
puts 'Skipping reindex as OPENSEARCH_URL is not configured'
return false
end
true
end
def reindex_account(account)
Messages::ReindexService.new(account: account).perform
puts "Reindex task queued for account #{account.id}"
end
end
namespace :search do
desc 'Reindex messages using searchkick'
include Tasks::SearchTaskHelpers
desc 'Reindex messages for all accounts'
task all: :environment do
next unless check_opensearch_config
puts 'Starting reindex for all accounts...'
account_count = Account.count
puts "Found #{account_count} accounts"
Account.find_each.with_index(1) do |account, index|
puts "[#{index}/#{account_count}] Reindexing messages for account #{account.id}"
reindex_account(account)
end
puts 'Reindex task queued for all accounts'
end
desc 'Reindex messages for a specific account: rake search:account ACCOUNT_ID=1'
task account: :environment do
next unless check_opensearch_config
account_id = ENV.fetch('ACCOUNT_ID', nil)
account = Account.find_by(id: account_id)
if account.nil?
puts 'Please provide a valid account ID. Account not found'
next
end
puts "Reindexing messages for account #{account.id}"
reindex_account(account)
end
end