Catalog index not updating

Help for integrating the Laravel package
Forum rules
Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
kdim95
Advanced
Posts: 207
Joined: 26 Aug 2022, 12:17

Catalog index not updating

Post by kdim95 » 04 Apr 2023, 13:06

Laravel framework version: 9.52.4
Aimeos Laravel version: ~2022.10
PHP Version: 8.2.4
Environment: Linux

Hello,

My catalog index is not updating.

I am importing and updating products the following way:

Code: Select all

// Create managers
$product_manager = \Aimeos\MShop::create( $this->context, 'product' );
$catalog_manager = \Aimeos\MShop::create( $this->context, 'catalog' );
$index_manager = \Aimeos\MShop::create( $this->context, 'index' );

// Begin product changes
$product_manager->begin();

// Get product
$product_item = $product_manager->get( <product id> );

// Remove category list items from product
$category_list_items = $product_item->getListItems( 'catalog', 'default', null, false );				
$product_item->deleteListItems( $category_list_items );

// Get category item
$category_item = $catalog_manager->get( <category id> );

// Add category list item to product
$category_list_item = $catalog_manager->createListItem();
$product_item->addListItem( 'catalog', $category_list_item, $category_item );

// Save product
$product_manager->save( $product_item );

// Commit product changes
$product_manager->commit();
I'm using ElasticSearch indexing.

Code: Select all

'mshop' => [
	'index' => [
		'manager' => [
			'name' => 'ElasticIndex', // Only index existing products from DB
		]
	],
],
The categories just won't appear in the frontend until I do a full index rebuild, using the following console command:

Code: Select all

php artisan aimeos:jobs index/rebuild
What am I doing wrong here?

By the way the example code here is a very cut down version of what I'm actually using, which includes adding attributes, properties, stock items, checking if the individual items exist before updating them, etc.
Last edited by kdim95 on 07 Apr 2023, 10:31, edited 1 time in total.

User avatar
aimeos
Administrator
Posts: 7908
Joined: 01 Jan 1970, 00:00

Re: Catalog index not updating

Post by aimeos » 07 Apr 2023, 08:27

Use the index manager instead of the product manager because the index manager saves the products and updates the index in one step:

Code: Select all

$product_manager = \Aimeos\MShop::create( $this->context, 'product' );
$index_manager = \Aimeos\MShop::create( $this->context, 'index' );
$index_manager->begin();

// Get products
$filter = $product_manager->filter()->add( 'product.id', '==', <product ids> );
$product_items = $product_manager->search( $filter );

// Update products ...

// Save products
$index_manager->save( $product_items );

// Finally commit all product changes after e.g. 100 products
$index_manager->commit();
Also, you can speed up the import drastically if you use optimized code like this one:
https://github.com/aimeos/ai-controller ... #L133-L140
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

kdim95
Advanced
Posts: 207
Joined: 26 Aug 2022, 12:17

Re: Catalog index not updating

Post by kdim95 » 07 Apr 2023, 13:15

Thank you!

By the way I think there is a problem when saving single product items as opposed to arrays of product items

Code: Select all

$index_manager->begin(); // This is not taken in account

$index_manager->save( $single_product_item ); // Item is saved here without calling $manager->commit()

$index_manager->commit(); // This is not taken in account

kdim95
Advanced
Posts: 207
Joined: 26 Aug 2022, 12:17

Re: Catalog index not updating

Post by kdim95 » 07 Apr 2023, 14:42

What would you say is the average time in seconds to save and index 5,000 products?

It currently takes about 1 minute, maybe less for me.

There are properties, attribute items, catalog items, configs, text items, media items, price items added for each product that is being saved using $index_manager->save( $products ).

User avatar
aimeos
Administrator
Posts: 7908
Joined: 01 Jan 1970, 00:00

Re: Catalog index not updating

Post by aimeos » 11 Apr 2023, 07:32

It depends a bit on your hardware but you may get more products updated in the same timeframe with optimized code and when saving batches of products at once (250-500 is a good value if you have no memory contraints).
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply