Finalized elastic search changes

This commit is contained in:
jitendra 2022-10-26 23:04:37 +05:30
parent dca642e425
commit 4753d7872b
8 changed files with 143 additions and 82 deletions

View File

@ -1,6 +1,6 @@
{!! view_render_event('bagisto.admin.catalog.product.edit_form_accordian.inventories.before', ['product' => $product]) !!}
<accordian title="{{ __('admin::app.catalog.products.inventories') }}" :active="true">
<accordian title="{{ __('admin::app.catalog.products.inventories') }}" :active="false">
<div slot="body">
{!! view_render_event('bagisto.admin.catalog.product.edit_form_accordian.inventories.controls.before', ['product' => $product]) !!}

View File

@ -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();
}
}
}
}

View File

@ -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();
}
}

View File

@ -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
*

View File

@ -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',
],

View File

@ -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') {

View File

@ -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);
}

View File

@ -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'