Sunday, June 1, 2025
  • Home
  • About Us
  • Disclaimer
  • Contact Us
  • Terms & Conditions
  • Privacy Policy
T3llam
  • Home
  • App
  • Mobile
    • IOS
  • Gaming
  • Computing
  • Tech
  • Services & Software
  • Home entertainment
No Result
View All Result
  • Home
  • App
  • Mobile
    • IOS
  • Gaming
  • Computing
  • Tech
  • Services & Software
  • Home entertainment
No Result
View All Result
T3llam
No Result
View All Result
Home Services & Software

Custom-made type order in Elasticsearch

admin by admin
May 6, 2024
in Services & Software
0
Custom-made type order in Elasticsearch
0
SHARES
0
VIEWS
Share on FacebookShare on Twitter


Whats up mates welcome once more. On this weblog, we’ll take a look at how we are able to personalized type order in Elasticsearch by offering the type order within the array format.

Elasticsearch additionally offers a number of different sorting methods by default nonetheless this sorting approach is one in every of my favorites and has a number of use circumstances additionally.

To get began we first have to create a module during which we write our performance.

After creating the module we have to create a plugin to change the Elasticsearch question. For that, we first outline our plugin within the di.xml file at path app/code/VendorName/ModuleName/and many others/di.xml

<?xml model="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/and many others/config.xsd">
    <kind identify="MagentoFrameworkSearchAdapterInterface">
        <plugin identify="customize_elasticsearch_search_results" kind="WebkulCustomizeElasticsearchPluginElasticsearch7SearchAdapterAdapter" sortOrder="1" />
    </kind>
</config>

After defining the plugin we have to create our plugin file on the location outlined within the definition of the plugin.

<?php
namespace WebkulCustomizeElasticsearchPluginElasticsearch7SearchAdapter;

use MagentoFrameworkSearchRequestInterface;
use MagentoElasticsearch7SearchAdapterMapper;
use MagentoElasticsearchSearchAdapterResponseFactory;
use MagentoElasticsearchSearchAdapterConnectionManager;
use MagentoElasticsearchSearchAdapterQueryContainerFactory;
use MagentoElasticsearchSearchAdapterAggregationBuilder as AggregationBuilder;

class Adapter
{
    /**
     * @var array
     */
    personal static $emptyRawResponse = [
        "hits" =>
        [
            "hits" => []
        ],
        "aggregations" =>
        [
            "price_bucket" => [],
            "category_bucket" =>
            [
                "buckets" => []

            ]
        ]
    ];

    /**
     * Dependency Initilization
     *
     * @param Mapper $mapper
     * @param PsrLogLoggerInterface $logger
     * @param ResponseFactory $responseFactory
     * @param ConnectionManager $connectionManager
     * @param AggregationBuilder $aggregationBuilder
     * @param QueryContainerFactory $queryContainerFactory
     * @param MagentoFrameworkMessageManagerInterface $messageManager
     * @param MagentoCatalogModelResourceModelProductCollectionFactory $collectionFactory
     */
    public operate __construct(
        personal Mapper $mapper,
        personal PsrLogLoggerInterface $logger,
        personal ResponseFactory $responseFactory,
        personal ConnectionManager $connectionManager,
        personal AggregationBuilder $aggregationBuilder,
        personal QueryContainerFactory $queryContainerFactory,
        personal MagentoFrameworkMessageManagerInterface $messageManager,
        personal MagentoCatalogModelResourceModelProductCollectionFactory $collectionFactory
    ) {
    }

    /**
     * Add vendor product to vendor assortment
     *
     * @param MagentoFrameworkSearchAdapterInterface $topic
     * @param callable $proceed
     * @param RequestInterface $request
     */
    public operate aroundQuery(
        MagentoFrameworkSearchAdapterInterface $topic,
        callable $proceed,
        RequestInterface $request
    ) {
        attempt {
            $productIds = $this->getProductCustomSortOrder();
            $consumer = $this->connectionManager->getConnection();
            $aggregationBuilder = $this->aggregationBuilder;
            $updatedQuery = $this->mapper->buildQuery($request);
            unset($updatedQuery['body']['query']['bool']['should']);
            unset($updatedQuery['body']['query']['bool']['minimum_should_match']);
            unset($updatedQuery['body']['sort']);
            $updatedQuery['body']['sort'][0]['_script']['type'] = 'quantity';
            $setSortOrderAttribute = 'params.sortOrder.indexOf(doc["_id"].worth)';
            $updatedQuery['body']['sort'][0]['_script']['script']['inline'] = $setSortOrderAttribute;
            $updatedQuery['body']['sort'][0]['_script']['script']['params']['sortOrder'] = $productIds;
            unset($updatedQuery['body']['sort'][0]['_score']);
            $updatedQuery['body']['query']['bool']['filter'] = ['ids' => ['values' => $productIds]];
            $aggregationBuilder->setQuery($this->queryContainerFactory->create(['query' => $updatedQuery]));
            attempt {
                $rawResponse = $client->question($updatedQuery);
            } catch (Exception $e) {
                $this->logger->debug(
                    "Elasticsearch7_SearchAdapter_Adapter aroundQuery " . $e->getMessage()
                );
                $rawResponse = self::$emptyRawResponse;
            }
            $rawDocuments = $rawResponse['hits']['hits'] ?? [];
            $queryResponse = $this->responseFactory->create(
                [
                    'documents' => $rawDocuments,
                    'aggregations' => $aggregationBuilder->build($request, $rawResponse),
                    'total' => $rawResponse['hits']['total']['value'] ?? 0
                ]
            );
            return $queryResponse;
        } catch (Exception $e) {
            $this->logger->debug(
                "Elasticsearch7_SearchAdapter_Adapter aroundQuery " . $e->getMessage()
            );
            $this->messageManager->addError(__($e->getMessage()));
        }
        return $proceed($request);
    }

    /**
     * Get Product Customized SortOrder
     *
     * @return array
     */
    personal operate getProductCustomSortOrder()
    {
        $merchandise = $this->collectionFactory->create()
            ->addFieldToFilter('visibility', ['in' => [3, 4]]);
        $evenProductIds = [];
        $oddProductIds = [];
        foreach ($merchandise as $product) {
            if ($product->getEntityId() % 2 == 0) {
                $evenProductIds[] = $product->getEntityId();
            } else {
                $oddProductIds[] = $product->getEntityId();
            }
        }
        return array_merge($oddProductIds, $evenProductIds);
    }
}

The code that really helps in sorting is.

$updatedQuery['body']['sort'][0]['_script']['type'] = 'quantity';
$setSortOrderAttribute = 'params.sortOrder.indexOf(doc["_id"].worth)';
$updatedQuery['body']['sort'][0]['_script']['script']['inline'] = $setSortOrderAttribute;
$updatedQuery['body']['sort'][0]['_script']['script']['params']['sortOrder'] = $productIds;

Our precise question format appears to be like one thing like this.

Array
(
    [body] => Array
    (
        [sort] => Array
        (
            [0] => Array
            (
                [_script] => Array
                (
                    [type] => quantity
                    [script] => Array
                    (
                        [inline] => params.sortOrder.indexOf(doc["_id"].worth)
                        [params] => Array
                        (
                            [sortOrder] => Array
                            (
                                [0] => 1
                                [1] => 3
                                [2] => 5
                                [3] => 7
                                [4] => 9
                                [5] => 2
                                [6] => 4
                                [7] => 6
                                [8] => 8
                                [9] => 10
                            )
                        )
                    )
                )
            )
        )
    )
)

Right here our sortOrder array incorporates all of the odd product IDs first and all of the even product IDs after them which causes our product itemizing to type merchandise in that method.

In keeping with the above code, our product itemizing might be.

That’s all guys we now have efficiently personalized type order for our product itemizing.

author-thumb

RelatedPosts

Consumer Information for Odoo POS Supply Display screen

Consumer Information for Odoo POS Supply Display screen

May 31, 2025
A deep dive into proof scores

A deep dive into proof scores

May 31, 2025
Microservices Structure: Greatest Practices & Challenges

Microservices Structure: Greatest Practices & Challenges

May 31, 2025


Whats up mates welcome once more. On this weblog, we’ll take a look at how we are able to personalized type order in Elasticsearch by offering the type order within the array format.

Elasticsearch additionally offers a number of different sorting methods by default nonetheless this sorting approach is one in every of my favorites and has a number of use circumstances additionally.

To get began we first have to create a module during which we write our performance.

After creating the module we have to create a plugin to change the Elasticsearch question. For that, we first outline our plugin within the di.xml file at path app/code/VendorName/ModuleName/and many others/di.xml

<?xml model="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/and many others/config.xsd">
    <kind identify="MagentoFrameworkSearchAdapterInterface">
        <plugin identify="customize_elasticsearch_search_results" kind="WebkulCustomizeElasticsearchPluginElasticsearch7SearchAdapterAdapter" sortOrder="1" />
    </kind>
</config>

After defining the plugin we have to create our plugin file on the location outlined within the definition of the plugin.

<?php
namespace WebkulCustomizeElasticsearchPluginElasticsearch7SearchAdapter;

use MagentoFrameworkSearchRequestInterface;
use MagentoElasticsearch7SearchAdapterMapper;
use MagentoElasticsearchSearchAdapterResponseFactory;
use MagentoElasticsearchSearchAdapterConnectionManager;
use MagentoElasticsearchSearchAdapterQueryContainerFactory;
use MagentoElasticsearchSearchAdapterAggregationBuilder as AggregationBuilder;

class Adapter
{
    /**
     * @var array
     */
    personal static $emptyRawResponse = [
        "hits" =>
        [
            "hits" => []
        ],
        "aggregations" =>
        [
            "price_bucket" => [],
            "category_bucket" =>
            [
                "buckets" => []

            ]
        ]
    ];

    /**
     * Dependency Initilization
     *
     * @param Mapper $mapper
     * @param PsrLogLoggerInterface $logger
     * @param ResponseFactory $responseFactory
     * @param ConnectionManager $connectionManager
     * @param AggregationBuilder $aggregationBuilder
     * @param QueryContainerFactory $queryContainerFactory
     * @param MagentoFrameworkMessageManagerInterface $messageManager
     * @param MagentoCatalogModelResourceModelProductCollectionFactory $collectionFactory
     */
    public operate __construct(
        personal Mapper $mapper,
        personal PsrLogLoggerInterface $logger,
        personal ResponseFactory $responseFactory,
        personal ConnectionManager $connectionManager,
        personal AggregationBuilder $aggregationBuilder,
        personal QueryContainerFactory $queryContainerFactory,
        personal MagentoFrameworkMessageManagerInterface $messageManager,
        personal MagentoCatalogModelResourceModelProductCollectionFactory $collectionFactory
    ) {
    }

    /**
     * Add vendor product to vendor assortment
     *
     * @param MagentoFrameworkSearchAdapterInterface $topic
     * @param callable $proceed
     * @param RequestInterface $request
     */
    public operate aroundQuery(
        MagentoFrameworkSearchAdapterInterface $topic,
        callable $proceed,
        RequestInterface $request
    ) {
        attempt {
            $productIds = $this->getProductCustomSortOrder();
            $consumer = $this->connectionManager->getConnection();
            $aggregationBuilder = $this->aggregationBuilder;
            $updatedQuery = $this->mapper->buildQuery($request);
            unset($updatedQuery['body']['query']['bool']['should']);
            unset($updatedQuery['body']['query']['bool']['minimum_should_match']);
            unset($updatedQuery['body']['sort']);
            $updatedQuery['body']['sort'][0]['_script']['type'] = 'quantity';
            $setSortOrderAttribute = 'params.sortOrder.indexOf(doc["_id"].worth)';
            $updatedQuery['body']['sort'][0]['_script']['script']['inline'] = $setSortOrderAttribute;
            $updatedQuery['body']['sort'][0]['_script']['script']['params']['sortOrder'] = $productIds;
            unset($updatedQuery['body']['sort'][0]['_score']);
            $updatedQuery['body']['query']['bool']['filter'] = ['ids' => ['values' => $productIds]];
            $aggregationBuilder->setQuery($this->queryContainerFactory->create(['query' => $updatedQuery]));
            attempt {
                $rawResponse = $client->question($updatedQuery);
            } catch (Exception $e) {
                $this->logger->debug(
                    "Elasticsearch7_SearchAdapter_Adapter aroundQuery " . $e->getMessage()
                );
                $rawResponse = self::$emptyRawResponse;
            }
            $rawDocuments = $rawResponse['hits']['hits'] ?? [];
            $queryResponse = $this->responseFactory->create(
                [
                    'documents' => $rawDocuments,
                    'aggregations' => $aggregationBuilder->build($request, $rawResponse),
                    'total' => $rawResponse['hits']['total']['value'] ?? 0
                ]
            );
            return $queryResponse;
        } catch (Exception $e) {
            $this->logger->debug(
                "Elasticsearch7_SearchAdapter_Adapter aroundQuery " . $e->getMessage()
            );
            $this->messageManager->addError(__($e->getMessage()));
        }
        return $proceed($request);
    }

    /**
     * Get Product Customized SortOrder
     *
     * @return array
     */
    personal operate getProductCustomSortOrder()
    {
        $merchandise = $this->collectionFactory->create()
            ->addFieldToFilter('visibility', ['in' => [3, 4]]);
        $evenProductIds = [];
        $oddProductIds = [];
        foreach ($merchandise as $product) {
            if ($product->getEntityId() % 2 == 0) {
                $evenProductIds[] = $product->getEntityId();
            } else {
                $oddProductIds[] = $product->getEntityId();
            }
        }
        return array_merge($oddProductIds, $evenProductIds);
    }
}

The code that really helps in sorting is.

$updatedQuery['body']['sort'][0]['_script']['type'] = 'quantity';
$setSortOrderAttribute = 'params.sortOrder.indexOf(doc["_id"].worth)';
$updatedQuery['body']['sort'][0]['_script']['script']['inline'] = $setSortOrderAttribute;
$updatedQuery['body']['sort'][0]['_script']['script']['params']['sortOrder'] = $productIds;

Our precise question format appears to be like one thing like this.

Array
(
    [body] => Array
    (
        [sort] => Array
        (
            [0] => Array
            (
                [_script] => Array
                (
                    [type] => quantity
                    [script] => Array
                    (
                        [inline] => params.sortOrder.indexOf(doc["_id"].worth)
                        [params] => Array
                        (
                            [sortOrder] => Array
                            (
                                [0] => 1
                                [1] => 3
                                [2] => 5
                                [3] => 7
                                [4] => 9
                                [5] => 2
                                [6] => 4
                                [7] => 6
                                [8] => 8
                                [9] => 10
                            )
                        )
                    )
                )
            )
        )
    )
)

Right here our sortOrder array incorporates all of the odd product IDs first and all of the even product IDs after them which causes our product itemizing to type merchandise in that method.

In keeping with the above code, our product itemizing might be.

That’s all guys we now have efficiently personalized type order for our product itemizing.

author-thumb

Previous Post

IOS 18 Anticipated To Deliver In AI Options Like By no means Earlier than. This is How It May Change Siri, Accessibility, Extra

Next Post

Realme GT Neo 6 will get listed for pre-order, key specs revealed

Next Post
Realme GT Neo 6 will get listed for pre-order, key specs revealed

Realme GT Neo 6 will get listed for pre-order, key specs revealed

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • App (3,061)
  • Computing (4,367)
  • Gaming (9,536)
  • Home entertainment (633)
  • IOS (9,461)
  • Mobile (11,797)
  • Services & Software (3,965)
  • Tech (5,279)
  • Uncategorized (4)

Recent Posts

  • Repairability is lastly going mainstream. Kind of.
  • The battle to play Borderlands On-line continues, as devoted archivists ask for assist in pursuit of the lengthy misplaced MMO
  • Ransomware kingpin “Stern” apparently IDed by German legislation enforcement
  • NYT Strands hints and solutions for Sunday, June 1 (recreation #455)
  • Consumer Information for Odoo POS Supply Display screen
  • App
  • Computing
  • Gaming
  • Home entertainment
  • IOS
  • Mobile
  • Services & Software
  • Tech
  • Uncategorized
  • Home
  • About Us
  • Disclaimer
  • Contact Us
  • Terms & Conditions
  • Privacy Policy

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

No Result
View All Result
  • Home
  • App
  • Mobile
    • IOS
  • Gaming
  • Computing
  • Tech
  • Services & Software
  • Home entertainment

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies. However you may visit Cookie Settings to provide a controlled consent.
Cookie settingsACCEPT
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analyticsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functionalThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessaryThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-othersThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performanceThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policyThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Save & Accept