Trong bài viết này chúng ta sẽ tìm hiểu về các câu lệnh cần thiết khi làm việc với indexer trong Magento 2.
Xem danh sách indexer
Xem danh sách indexer:
1 |
php bin/magento indexer:info |
Chúng ta sẽ được danh sách indexer như bên dưới:
1 2 3 4 5 6 7 8 9 10 11 |
design_config_grid Design Config Grid customer_grid Customer Grid catalog_category_product Category Products catalog_product_category Product Categories catalogrule_rule Catalog Rule Product catalog_product_attribute Product EAV inventory Inventory catalogrule_product Catalog Product Rule cataloginventory_stock Stock catalog_product_price Product Price catalogsearch_fulltext Catalog Search |
Indexer status
Bạn có thể xem status của một indexer cụ thể hoặc tất cả. Mục đích để kiểm tra xem có indexer nào cần phải reindex hay không.
Xem status của một indexer cụ thể:
1 |
php bin/magento indexer:status [indexer] |
Xem status của tất cả indexer:
1 |
php bin/magento indexer:status |
Kết quả:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
+---------------------------+-----------------------------+------------------+-----------+---------------------+---------------------+ | ID | Title | Status | Update On | Schedule Status | Schedule Updated | +---------------------------+-----------------------------+------------------+-----------+---------------------+---------------------+ | catalogrule_product | Catalog Product Rule | Reindex required | Save | | | | catalogrule_rule | Catalog Rule Product | Reindex required | Save | | | | catalogsearch_fulltext | Catalog Search | Reindex required | Save | | | | catalog_category_product | Category Products | Reindex required | Save | | | | customer_grid | Customer Grid | Ready | Save | | | | design_config_grid | Design Config Grid | Ready | Save | | | | scconnector_google_feed | Google Product Feed | Reindex required | Schedule | idle (9 in backlog) | 2021-01-11 13:19:16 | | scconnector_google_remove | Google Product Removal Feed | Reindex required | Schedule | idle (9 in backlog) | 2021-01-11 13:19:15 | | inventory | Inventory | Reindex required | Save | | | | catalog_product_category | Product Categories | Reindex required | Save | | | | catalog_product_attribute | Product EAV | Reindex required | Save | | | | catalog_product_price | Product Price | Reindex required | Save | | | | cataloginventory_stock | Stock | Reindex required | Save | | | +---------------------------+-----------------------------+------------------+-----------+---------------------+---------------------+ |
Reindex
Có thể reindex cho tất cả hoặc một indexer cụ thể. Lệnh này chỉ reindex một lần, để luôn reindex thì bạn phải chạy cron job.
Reindex một index cụ thể:
1 |
php bin/magento indexer:reindex [indexer] |
Trong đó [indexer] là danh indexer mà bạn muốn reindex được phân cách nhau bằng khoảng trắng.
Reindex all:
1 |
php bin/magento indexer:reindex |
Sample result:
1 2 3 4 5 6 7 8 9 10 11 |
Design Config Grid index has been rebuilt successfully in <time> Customer Grid index has been rebuilt successfully in <time> Category Products index has been rebuilt successfully in <time> Product Categories index has been rebuilt successfully in <time> Catalog Rule Product index has been rebuilt successfully in <time> Product EAV index has been rebuilt successfully in <time> Inventory index has been rebuilt successfully in <time> Catalog Product Rule index has been rebuilt successfully in <time> Stock index has been rebuilt successfully in <time> Product Price index has been rebuilt successfully in <time> Catalog Search index has been rebuilt successfully in <time> |
Việc reindex all có thể sẽ mất nhiều thời gian đối với các cửa hàng có số lượng sản phẩm, category, customer… lớn.
Reset indexer
Sử dụng để invalidate tất cả indexer hoặc một indexer cụ thể. Invalidate ở đây là chuyển status của indexer thành invalid tức là trạng thái cần phải reindex lại.
Reset các indexer cụ thể:
1 |
bin/magento indexer:reset [indexer] |
Trong đó [indexer] là danh sách các indexer mà bạn muốn invalidate được phân cách nhau bởi khoảng trắng.
Reset all:
1 |
bin/magento indexer:reset |
Sample result:
1 2 3 4 5 6 7 8 9 10 11 |
Design Config Grid indexer has been invalidated. Customer Grid indexer has been invalidated. Category Products indexer has been invalidated. Product Categories indexer has been invalidated. Catalog Rule Product indexer has been invalidated. Product EAV indexer has been invalidated. Inventory indexer has been invalidated. Catalog Product Rule indexer has been invalidated. Stock indexer has been invalidated. Product Price indexer has been invalidated. Catalog Search indexer has been invalidated. |
Cấu hình indexers
Indexer sẽ chạy ở hai mode:
-
Update on save (
realtime
): Dữ liệu sẽ được cập nhật ngay sau khi có sự thay đổi trong admin. Ví dụ category products index sẽ được reindex lại sau khi có sản phẩm được gán cho một category. Đây cũng là mode mặc định. -
Update by schedule (
schedule
): reindex theo lịch chạy của cron job.
Xem thêm về cách chuyển đổi giữa các mode trong admin panel: https://vi-magento.com/indexing-magento
Xem mode hiện tại
Xem mode của indexer cụ thể:
1 |
php bin/magento indexer:show-mode [indexer] |
View all:
1 |
php bin/magento indexer:show-mode |
Sample result:
1 2 3 4 5 6 7 8 9 10 11 |
Design Config Grid: Update on Save Customer Grid: Update on Save Category Products: Update on Save Product Categories: Update on Save Catalog Rule Product: Update on Save Product EAV: Update on Save Inventory: Update on Save Catalog Product Rule: Update on Save Stock: Update on Save Product Price: Update on Save Catalog Search: Update on Save |
Configure indexers
Trước khi chuyển đổi indexer mode, bạn nên đặt trang web của mình ở chế độ maintenance và tắt cron. Điều này sẽ đảm bảo bạn không bị khóa cơ sở dữ liệu.
1 |
php bin/magento indexer:set-mode {realtime|schedule} [indexer] |
Trong đó:
-
realtime
: Set các indexer đã chọn thành Update on Save mode. -
schedule
: Set các indexer đã chọn thành Update by schedule mode. - [indexer] là danh sách các indexer được phân cách nhau bởi khoảng trắng. Bỏ qua nếu bạn muốn set tất cả indexer.
Ví dụ, nếu bạn chỉ muốn set Update on Schedule mode cho indexer category products, bạn phải chạy câu lệnh sau:
1 |
php bin/magento indexer:set-mode schedule catalog_category_product |
Sample result:
1 |
Index mode for Indexer Category Products was changed from 'Update on Save' to 'Update by Schedule' |
Xem thêm tại https://devdocs.magento.com/guides/v2.4/config-guide/cli/config-cli-subcommands-index.html