# Console Command
The plugin provides a set of console commands to assist in working with the index or to perform maintenance tasks.
# Index stats
To display statistics of the current indexes:
$ ./craft elastic/index/stats
Index stats of site "en":
Current index in use: craftcms_en_1650984786
Elements in index: 958831
Stored data: 184,821 MB
Index stats of site "de":
Current index in use: craftcms_de_1650984886
Elements in index: 958831
Stored data: 165,325 MB
# Element source
To display the source on an element in the current indexes:
$ ./craft elastic/index/source 5
Index source of element "The Test" for site "en":
╔══════════════════════════════╤═══════════════════════════════════════════════════════════════════════╗
║ Field handle │ Source ║
╟──────────────────────────────┼───────────────────────────────────────────────────────────────────────╢
║ slug │ the-test ║
╟──────────────────────────────┼───────────────────────────────────────────────────────────────────────╢
║ title │ The Test ║
╟──────────────────────────────┼───────────────────────────────────────────────────────────────────────╢
║ ... │ ... ║
# List all indexes
To display a list of all available aliases and indexes:
$ ./craft elastic/index/list
Aliases
╔══════════════════╤═════════════════════════════╗
║ Alias │ Current index ║
╟──────────────────┼─────────────────────────────╢
║ craftcms_de │ craftcms_de_1650984886 ║
╟──────────────────┼─────────────────────────────╢
║ craftcms_en │ craftcms_en_1650984786 ║
╚══════════════════╧═════════════════════════════╝
Indexes
╔════════╤═════════════════════════════╤════════╤═══════════╤═════════╗
║ Health │ Index │ Status │ Documents │ Size ║
╟────────┼─────────────────────────────┼────────┼───────────┼─────────╢
║ green │ craftcms_en_1650984786 │ open │ 958831 │ 182.8mb ║
╟────────┼─────────────────────────────┼────────┼───────────┼─────────╢
║ green │ craftcms_de_1650984886 │ open │ 958831 │ 165.3mb ║
╟────────┼─────────────────────────────┼────────┼───────────┼─────────╢
║ yellow │ craftcms_de_1649058790 │ open │ 580695 │ 145.9mb ║
╟────────┼─────────────────────────────┼────────┼───────────┼─────────╢
║ yellow │ craftcms_en_1650449067 │ open │ 580695 │ 181.4mb ║
╚════════╧═════════════════════════════╧════════╧═══════════╧═════════╝
The list shows all indexes, including indexes that are no longer in use. Health is the cluster Health of the index.
# Re-index Index
Sometimes it may be necessary to update the index on the Elasticsearch cluster with the current mapping (fields). This can be done with the following command:
$ ./craft elastic/index/reindex
Do you want to reindex the source of the current index "craftcms_en_1650984786" for the site with the handle "en" to a new index? (yes|no) [no]:yes
The process of reindexing can take some time. It depends on many different conditions. Do not interrupt this process and wait until it is finished.
Reindexing index for site "en":
Old index: craftcms_en_1650984786
New index: craftcms_en_1651060965
Finished after: 2 Minuten, 45 Sekunden
Total of 958831 elements migrated.
Do you want to reindex the source of the current index "craftcms_de_1650984886" for the site with the handle "de" to a new index? (yes|no) [no]:yes
Reindexing index for site "de":
Old index: craftcms_de_1650984886
New index: craftcms_de_1651061137
Finished after: 2 Minuten, 12 Sekunden
Total of 958831 elements migrated.
The re-index creates a new index and changes the pointer on the alias from the old to the new index. The old index remains. It can be deleted later.
# Delete Index
You can delete the Elasticsearch index for Craft with the following command:
$ ./craft elastic/index/delete
Do you want to delete the index for the site with the handle "en"? (yes|no) [no]:yes
Do you want to delete the index for the site with the handle "de"? (yes|no) [no]:yes
If re-indexing has been done, the old indexes will remain. The following command deletes all unused indexes:
$ ./craft elastic/index/delete --orphaned-only
Do you want to delete all orphaned indexes for the site with the handle "en"? (yes|no) [no]:yes
Do you want to delete all orphaned indexes for the site with the handle "de"? (yes|no) [no]:yes
# Clone Index
If you have different development stages, you can, for example, clone the index from the current production to the test environment. As long as both stages are using the same Elasticsearch cluster and have different index names configured in the settings (for example via .env).
To clone an index, you need the index name of the source and the handle of the destination site:
$ ./craft elastic/index/clone craftcms_prod_en en
Index cloned.
# Index all elements from Craft into the current Elasticsearch index
One step in the migration is the point when all the elements from Craft need to be added to the new Elasticsearch index. The following command will help you to do this:
$ ./craft elastic/elements/index
Index all 456621 elements of type 'craft\elements\Asset'? (yes|no) [no]:
Index all 7 elements of type 'craft\elements\Category'? (yes|no) [no]:
Index all 329047 elements of type 'craft\elements\Entry'? (yes|no) [no]:
Index all 8653 elements of type 'craft\elements\Tag'? (yes|no) [no]:
Index all 37 elements of type 'craft\elements\User'? (yes|no) [no]:
Index all 251160 elements of type 'company\elements\Member'? (yes|no) [no]:
Index all 85182 elements of type 'putyourlightson\campaign\elements\ContactElement'? (yes|no) [no]:
The command asks for each element type in Craft whether it should be indexed now. Then for each element a job is created in the queue. The priority of the jobs is set to 2048 (if the queue backend supports this). All jobs from Craft itself are prioritized with 1024 and are therefore processed before.
TIP
We recommend using a separate queue and a different queue backend (for example Redis or AWS SQS) to process the high number of jobs with multiple workers simultaneously and without performance impact.
To use a different queue component or to set a different priority, you can pass arguments to the command:
$ ./craft elastic/elements/index [siteHandle] [queue] [priority]
# e.g. queue with separate queue with the component name "migrationQueue":
$ ./craft elastic/elements/index "*" migrationQueue 1024
# Re-index to one or both search indexes
Wenn irgendwas schief gelaufen ist, dann bietet der folgende Befehl die Möglichkeite alle Elemente ab einem anzugebenden Zeitpunkt wieder in einen oder beide Suchen aufzunehmen:
$ ./craft elastic/migration/reindex <date> [toDatabase] [toElasticsearch]
e.g. re-index all elements only to Craft\'s database search, starting from 12/25/2021:
./craft.sh elastic/migration/reindex 2021-12-25 1 0
Re-index all elements created or updated since 2021-12-25T00:00:00+0100? (yes|no) [no]:yes
Job queued.
This command creates only one job in the queue, which in turn creates all required jobs.
# Truncate unused Craft's full-text search table
After all the elements have been added to the new Elasticsearch index and the transition mode has been deactivated, you can empty the unused table in the database and thus free up the space.
WARNING
If you have truncated the table, you can switch back transition mode, but then the search is useless in most cases, because only the elements after the deactivation of the transition mode are re-indexed. All elements before that are missing.
The command:
$ ./craft elastic/migration/truncate-table
Do you want to truncate Craft's full-text search database table? (yes|no) [no]yes
Backup the database? (yes|no) [no]:yes
Backing up the database ... done
Tuncating searchindex...
Table truncated.