From 4753d7872b08558ac4ecada762367348b3389991 Mon Sep 17 00:00:00 2001 From: jitendra Date: Wed, 26 Oct 2022 23:04:37 +0530 Subject: [PATCH] Finalized elastic search changes --- .../products/accordians/inventories.blade.php | 2 +- .../Webkul/Product/src/Helpers/Indexer.php | 23 +++++ .../Indexers/ElasticSearch/Product.php | 99 +++++++++++++------ .../Webkul/Product/src/Listeners/Product.php | 20 +++- .../src/Providers/EventServiceProvider.php | 3 + .../Repositories/ElasticSearchRepository.php | 21 +++- .../src/Repositories/ProductRepository.php | 26 +---- .../Webkul/Product/src/Type/AbstractType.php | 31 +++--- 8 files changed, 143 insertions(+), 82 deletions(-) diff --git a/packages/Webkul/Admin/src/Resources/views/catalog/products/accordians/inventories.blade.php b/packages/Webkul/Admin/src/Resources/views/catalog/products/accordians/inventories.blade.php index f6133dc33..3d4483c3a 100755 --- a/packages/Webkul/Admin/src/Resources/views/catalog/products/accordians/inventories.blade.php +++ b/packages/Webkul/Admin/src/Resources/views/catalog/products/accordians/inventories.blade.php @@ -1,6 +1,6 @@ {!! view_render_event('bagisto.admin.catalog.product.edit_form_accordian.inventories.before', ['product' => $product]) !!} - +
{!! view_render_event('bagisto.admin.catalog.product.edit_form_accordian.inventories.controls.before', ['product' => $product]) !!} diff --git a/packages/Webkul/Product/src/Helpers/Indexer.php b/packages/Webkul/Product/src/Helpers/Indexer.php index b89487058..3d38fcd90 100644 --- a/packages/Webkul/Product/src/Helpers/Indexer.php +++ b/packages/Webkul/Product/src/Helpers/Indexer.php @@ -130,4 +130,27 @@ class Indexer } } } + + /** + * Delete elastic search indices + * + * @param \Webkul\Product\Contracts\Product $product + * @return void + */ + public function deleteElasticSearch($product) + { + if (core()->getConfigData('catalog.products.storefront.search_mode') != 'elastic') { + return; + } + + foreach (core()->getAllChannels() as $channel) { + foreach ($channel->locales as $locale) { + $this->elasticSearchIndexer + ->setProduct($product) + ->setChannel($channel) + ->setLocale($locale) + ->delete(); + } + } + } } \ No newline at end of file diff --git a/packages/Webkul/Product/src/Helpers/Indexers/ElasticSearch/Product.php b/packages/Webkul/Product/src/Helpers/Indexers/ElasticSearch/Product.php index 9483de988..bc45e7422 100644 --- a/packages/Webkul/Product/src/Helpers/Indexers/ElasticSearch/Product.php +++ b/packages/Webkul/Product/src/Helpers/Indexers/ElasticSearch/Product.php @@ -85,15 +85,41 @@ class Product */ public function refresh() { + if ( + ! $this->product->status + || ! $this->product->visible_individually + ) { + return $this->delete(); + } + $params = [ 'index' => $this->getIndexName(), 'id' => $this->product->id, - 'body' => $this->getElasticProperties(), + 'body' => $this->getElasticProperties(), ]; Elasticsearch::index($params); } + /** + * Delete product indices + * + * @return void + */ + public function delete() + { + $params = [ + 'index' => $this->getIndexName(), + 'id' => $this->product->id, + ]; + + try { + \Elasticsearch::delete($params); + } catch(\Exception $e) {} + + return; + } + /** * Refresh product indices * @@ -113,24 +139,12 @@ class Product { $properties = [ 'id' => $this->product->id, + 'sku' => $this->product->sku, 'category_ids' => $this->product->categories->pluck('id')->toArray(), 'created_at' => $this->product->created_at, ]; - $attributes = $this->attributeRepository->scopeQuery(function ($query) { - return $query->where(function ($ab) { - return $ab->orWhereIn('code', [ - 'sku', - 'name', - 'status', - 'visible_individually', - 'url_key', - 'short_description', - 'description', - ]) - ->orWhere('is_filterable', 1); - }); - })->get(); + $attributes = $this->getAttributes(); foreach ($attributes as $attribute) { $attributeValue = $this->getAttributeValue($attribute); @@ -153,6 +167,36 @@ class Product return $properties; } + /** + * Returns attributes to index + * + * @return void + */ + public function getAttributes() + { + static $attributes = []; + + if (count($attributes)) { + return $attributes; + } + + $attributes = $this->attributeRepository->scopeQuery(function ($query) { + return $query->where(function ($qb) { + return $qb->orWhereIn('code', [ + 'name', + 'new', + 'featured', + 'url_key', + 'short_description', + 'description', + ]) + ->orWhere('is_filterable', 1); + }); + })->get(); + + return $attributes; + } + /** * Returns filterable attribute values * @@ -162,32 +206,23 @@ class Product */ public function getAttributeValue($attribute) { + $attributeValues = $this->product->attribute_values + ->where('attribute_id', $attribute->id); + if ($attribute->value_per_channel) { if ($attribute->value_per_locale) { - $attributeValue = $this->product->attribute_values + $attributeValues = $attributeValues ->where('channel', $this->channel->code) - ->where('locale', $this->locale->code) - ->where('attribute_id', $attribute->id) - ->first(); + ->where('locale', $this->locale->code); } else { - $attributeValue = $this->product->attribute_values - ->where('channel', $this->channel->code) - ->where('attribute_id', $attribute->id) - ->first(); + $attributeValues = $attributeValues->where('channel', $this->channel->code); } } else { if ($attribute->value_per_locale) { - $attributeValue = $this->product->attribute_values - ->where('locale', $this->locale->code) - ->where('attribute_id', $attribute->id) - ->first(); - } else { - $attributeValue = $this->product->attribute_values - ->where('attribute_id', $attribute->id) - ->first(); + $attributeValues = $attributeValues->where('locale', $this->locale->code)->first(); } } - return $attributeValue; + return $attributeValues->first(); } } \ No newline at end of file diff --git a/packages/Webkul/Product/src/Listeners/Product.php b/packages/Webkul/Product/src/Listeners/Product.php index 7b8bbcad0..a93fb2442 100644 --- a/packages/Webkul/Product/src/Listeners/Product.php +++ b/packages/Webkul/Product/src/Listeners/Product.php @@ -2,6 +2,7 @@ namespace Webkul\Product\Listeners; +use Webkul\Product\Repositories\ProductRepository; use Webkul\Product\Repositories\ProductBundleOptionProductRepository; use Webkul\Product\Repositories\ProductGroupedProductRepository; use Webkul\Product\Helpers\Indexer; @@ -11,12 +12,14 @@ class Product /** * Create a new listener instance. * + * @param \Webkul\Product\Repositories\ProductRepository $productRepository * @param \Webkul\Product\Repositories\ProductBundleOptionProductRepository $productBundleOptionProductRepository * @param \Webkul\Product\Repositories\ProductGroupedProductRepository $productGroupedProductRepository * @param \Webkul\Product\Helpers\Indexer $indexer * @return void */ public function __construct( + protected ProductRepository $productRepository, protected ProductBundleOptionProductRepository $productBundleOptionProductRepository, protected ProductGroupedProductRepository $productGroupedProductRepository, protected Indexer $indexer @@ -25,7 +28,7 @@ class Product } /** - * Update or create product price indices + * Update or create product indices * * @param \Webkul\Product\Contracts\Product $product * @return void @@ -36,7 +39,7 @@ class Product } /** - * Update or create product price indices + * Update or create product indices * * @param \Webkul\Product\Contracts\Product $product * @return void @@ -52,6 +55,19 @@ class Product $this->refreshElasticSearchIndices($product); } + /** + * Delete product indices + * + * @param integer $productId + * @return void + */ + public function beforeDelete($productId) + { + $product = $this->productRepository->find($productId); + + $this->indexer->deleteElasticSearch($product); + } + /** * Update or create product inventory indices * diff --git a/packages/Webkul/Product/src/Providers/EventServiceProvider.php b/packages/Webkul/Product/src/Providers/EventServiceProvider.php index 03a218482..99a73936b 100644 --- a/packages/Webkul/Product/src/Providers/EventServiceProvider.php +++ b/packages/Webkul/Product/src/Providers/EventServiceProvider.php @@ -27,6 +27,9 @@ class EventServiceProvider extends ServiceProvider 'catalog.product.update.after' => [ 'Webkul\Product\Listeners\Product@afterUpdate', ], + 'catalog.product.delete.before' => [ + 'Webkul\Product\Listeners\Product@beforeDelete', + ], 'checkout.order.save.after' => [ 'Webkul\Product\Listeners\Order@afterCreate', ], diff --git a/packages/Webkul/Product/src/Repositories/ElasticSearchRepository.php b/packages/Webkul/Product/src/Repositories/ElasticSearchRepository.php index 4f816a961..3b1a1e79d 100755 --- a/packages/Webkul/Product/src/Repositories/ElasticSearchRepository.php +++ b/packages/Webkul/Product/src/Repositories/ElasticSearchRepository.php @@ -2,6 +2,7 @@ namespace Webkul\Product\Repositories; +use Illuminate\Support\Str; use Elasticsearch; use Elasticsearch\Common\Exceptions\Missing404Exception; use Webkul\Attribute\Repositories\AttributeRepository; @@ -75,11 +76,6 @@ class ElasticSearchRepository { $params = request()->input(); - $params = array_merge($params, [ - 'status' => 1, - 'visible_individually' => 1, - ]); - $filterableAttributes = $this->attributeRepository ->getProductDefaultAttributes(array_keys($params)); @@ -153,6 +149,21 @@ class ElasticSearchRepository */ public function getSortOptions($options) { + if ($options['order'] == 'rand') { + return [ + '_script' => [ + 'type' => 'number', + 'script' => [ + 'source' => '(doc[\'_id\'].value + params.salt).hashCode()', + 'params' => [ + 'salt' => Str::random(40), + ], + ], + 'order' => 'asc', + ], + ]; + } + $sort = $options['sort']; if ($options['sort'] == 'name') { diff --git a/packages/Webkul/Product/src/Repositories/ProductRepository.php b/packages/Webkul/Product/src/Repositories/ProductRepository.php index 84e91d5d3..4c3e503fb 100755 --- a/packages/Webkul/Product/src/Repositories/ProductRepository.php +++ b/packages/Webkul/Product/src/Repositories/ProductRepository.php @@ -156,23 +156,6 @@ class ProductRepository extends Repository return $product; } - /** - * Get product related to category. - * - * @param int $categoryId - * @return \Illuminate\Support\Collection - */ - public function getProductsRelatedToCategory($categoryId = null) - { - $qb = $this->model->leftJoin('product_categories', 'products.id', '=', 'product_categories.product_id'); - - if ($categoryId) { - $qb->where('product_categories.category_id', $categoryId); - } - - return $qb->get(); - } - /** * Get all products. * @@ -186,7 +169,6 @@ class ProductRepository extends Repository } else { return $this->searchFromDatabase($categoryId); } - } /** @@ -389,10 +371,6 @@ class ProductRepository extends Repository $items = $indices['total'] ? $query->get() : []; - $currentPage = Paginator::resolveCurrentPage('page'); - - $limit = $this->getPerPageLimit($params); - $results = new LengthAwarePaginator($items, $indices['total'], $limit, $currentPage, [ 'path' => request()->url(), 'query' => request()->query(), @@ -412,8 +390,8 @@ class ProductRepository extends Repository { $limit = $params['limit'] ?? 9; - if (core()->getConfigData('catalog.products.storefront.products_per_page')) { - $pages = explode(',', core()->getConfigData('catalog.products.storefront.products_per_page')); + if ($productsPerPage = core()->getConfigData('catalog.products.storefront.products_per_page')) { + $pages = explode(',', $productsPerPage); $limit = $params['limit'] ?? current($pages); } diff --git a/packages/Webkul/Product/src/Type/AbstractType.php b/packages/Webkul/Product/src/Type/AbstractType.php index 52daabd4b..f5ff74546 100644 --- a/packages/Webkul/Product/src/Type/AbstractType.php +++ b/packages/Webkul/Product/src/Type/AbstractType.php @@ -212,33 +212,28 @@ abstract class AbstractType : null; } + $attributeValues = $product->attribute_values + ->where('attribute_id', $attribute->id); + if ($attribute->value_per_channel) { if ($attribute->value_per_locale) { - $productAttributeValue = $product->attribute_values + $attributeValues = $attributeValues ->where('channel', $attribute->value_per_channel ? $data['channel'] : null) - ->where('locale', $attribute->value_per_locale ? $data['locale'] : null) - ->where('attribute_id', $attribute->id) - ->first(); + ->where('locale', $attribute->value_per_locale ? $data['locale'] : null); } else { - $productAttributeValue = $product->attribute_values - ->where('channel', $attribute->value_per_channel ? $data['channel'] : null) - ->where('attribute_id', $attribute->id) - ->first(); + $attributeValues = $attributeValues + ->where('channel', $attribute->value_per_channel ? $data['channel'] : null); } } else { if ($attribute->value_per_locale) { - $productAttributeValue = $product->attribute_values - ->where('locale', $attribute->value_per_locale ? $data['locale'] : null) - ->where('attribute_id', $attribute->id) - ->first(); - } else { - $productAttributeValue = $product->attribute_values - ->where('attribute_id', $attribute->id) - ->first(); + $attributeValues = $attributeValues + ->where('locale', $attribute->value_per_locale ? $data['locale'] : null); } } - if (! $productAttributeValue) { + $attributeValue = $attributeValues->first(); + + if (! $attributeValue) { $this->attributeValueRepository->create([ 'product_id' => $product->id, 'attribute_id' => $attribute->id, @@ -247,7 +242,7 @@ abstract class AbstractType 'locale' => $attribute->value_per_locale ? $data['locale'] : null, ]); } else { - $productAttributeValue->update([$attribute->column_name => $data[$attribute->code]]); + $attributeValue->update([$attribute->column_name => $data[$attribute->code]]); if ( $attribute->type == 'image'