Magento 2 Perform catalog search in my custom table












5















Building a custom module where I need to search on the third-party table for data.



I have executed and filtered data from the third-party table and retrieve the entity_id from it and added the entity_id the below query.



$this->queryResponse = $this->searchEngine->search($queryRequest);
$ids = [0];

$productIds = $this->crossReferenceFactory->getProductIdsBySearch($this->queryText);

if(count($productIds)>0){
foreach($productIds as $id)
$ids = $id;

}
/** @var MagentoFrameworkSearchDocument $document */
foreach ($this->queryResponse as $document) {
$ids = $document->getId();
}
parent::addFieldToFilter('entity_id', ['in' => $ids]);


When I do such filter it lists the products perfectly fine, But when I query for words which exist only in the third-party table it doesn't build the layered navigation.



After debugging I come to know that layered navigation uses below method.



public function getFacetedData($field)
{
$this->_renderFilters();
$aggregations = $this->queryResponse->getAggregations();
$bucket = $aggregations->getBucket($field . '_bucket');
if (!$bucket) {
throw new StateException(new Phrase('Bucket does not exist'));
}
$result = ;
foreach ($bucket->getValues() as $value) {

$metrics = $value->getMetrics();
$result[$metrics['value']] = $metrics;
}

return $result;
}


So, When I search for any particular word it builds the layered navigation using queryResponse data.



My concern is how can I add my table to $queryRequest so that I can have a consistent result. Any help would highly appreciate.










share|improve this question

























  • Hi, Did you find solution for this? I am facing same issue. Please answer if you found please share here.

    – Praful Rajput
    Oct 4 '16 at 12:30











  • i am also facing same issue can any one share the answer?

    – Pradeep Kumar
    Sep 11 '17 at 9:17
















5















Building a custom module where I need to search on the third-party table for data.



I have executed and filtered data from the third-party table and retrieve the entity_id from it and added the entity_id the below query.



$this->queryResponse = $this->searchEngine->search($queryRequest);
$ids = [0];

$productIds = $this->crossReferenceFactory->getProductIdsBySearch($this->queryText);

if(count($productIds)>0){
foreach($productIds as $id)
$ids = $id;

}
/** @var MagentoFrameworkSearchDocument $document */
foreach ($this->queryResponse as $document) {
$ids = $document->getId();
}
parent::addFieldToFilter('entity_id', ['in' => $ids]);


When I do such filter it lists the products perfectly fine, But when I query for words which exist only in the third-party table it doesn't build the layered navigation.



After debugging I come to know that layered navigation uses below method.



public function getFacetedData($field)
{
$this->_renderFilters();
$aggregations = $this->queryResponse->getAggregations();
$bucket = $aggregations->getBucket($field . '_bucket');
if (!$bucket) {
throw new StateException(new Phrase('Bucket does not exist'));
}
$result = ;
foreach ($bucket->getValues() as $value) {

$metrics = $value->getMetrics();
$result[$metrics['value']] = $metrics;
}

return $result;
}


So, When I search for any particular word it builds the layered navigation using queryResponse data.



My concern is how can I add my table to $queryRequest so that I can have a consistent result. Any help would highly appreciate.










share|improve this question

























  • Hi, Did you find solution for this? I am facing same issue. Please answer if you found please share here.

    – Praful Rajput
    Oct 4 '16 at 12:30











  • i am also facing same issue can any one share the answer?

    – Pradeep Kumar
    Sep 11 '17 at 9:17














5












5








5








Building a custom module where I need to search on the third-party table for data.



I have executed and filtered data from the third-party table and retrieve the entity_id from it and added the entity_id the below query.



$this->queryResponse = $this->searchEngine->search($queryRequest);
$ids = [0];

$productIds = $this->crossReferenceFactory->getProductIdsBySearch($this->queryText);

if(count($productIds)>0){
foreach($productIds as $id)
$ids = $id;

}
/** @var MagentoFrameworkSearchDocument $document */
foreach ($this->queryResponse as $document) {
$ids = $document->getId();
}
parent::addFieldToFilter('entity_id', ['in' => $ids]);


When I do such filter it lists the products perfectly fine, But when I query for words which exist only in the third-party table it doesn't build the layered navigation.



After debugging I come to know that layered navigation uses below method.



public function getFacetedData($field)
{
$this->_renderFilters();
$aggregations = $this->queryResponse->getAggregations();
$bucket = $aggregations->getBucket($field . '_bucket');
if (!$bucket) {
throw new StateException(new Phrase('Bucket does not exist'));
}
$result = ;
foreach ($bucket->getValues() as $value) {

$metrics = $value->getMetrics();
$result[$metrics['value']] = $metrics;
}

return $result;
}


So, When I search for any particular word it builds the layered navigation using queryResponse data.



My concern is how can I add my table to $queryRequest so that I can have a consistent result. Any help would highly appreciate.










share|improve this question
















Building a custom module where I need to search on the third-party table for data.



I have executed and filtered data from the third-party table and retrieve the entity_id from it and added the entity_id the below query.



$this->queryResponse = $this->searchEngine->search($queryRequest);
$ids = [0];

$productIds = $this->crossReferenceFactory->getProductIdsBySearch($this->queryText);

if(count($productIds)>0){
foreach($productIds as $id)
$ids = $id;

}
/** @var MagentoFrameworkSearchDocument $document */
foreach ($this->queryResponse as $document) {
$ids = $document->getId();
}
parent::addFieldToFilter('entity_id', ['in' => $ids]);


When I do such filter it lists the products perfectly fine, But when I query for words which exist only in the third-party table it doesn't build the layered navigation.



After debugging I come to know that layered navigation uses below method.



public function getFacetedData($field)
{
$this->_renderFilters();
$aggregations = $this->queryResponse->getAggregations();
$bucket = $aggregations->getBucket($field . '_bucket');
if (!$bucket) {
throw new StateException(new Phrase('Bucket does not exist'));
}
$result = ;
foreach ($bucket->getValues() as $value) {

$metrics = $value->getMetrics();
$result[$metrics['value']] = $metrics;
}

return $result;
}


So, When I search for any particular word it builds the layered navigation using queryResponse data.



My concern is how can I add my table to $queryRequest so that I can have a consistent result. Any help would highly appreciate.







magento2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 16 '18 at 5:20









Evince Development

1,112319




1,112319










asked Aug 12 '16 at 10:42









Codrain Technolabs Pvt LtdCodrain Technolabs Pvt Ltd

1,1541025




1,1541025













  • Hi, Did you find solution for this? I am facing same issue. Please answer if you found please share here.

    – Praful Rajput
    Oct 4 '16 at 12:30











  • i am also facing same issue can any one share the answer?

    – Pradeep Kumar
    Sep 11 '17 at 9:17



















  • Hi, Did you find solution for this? I am facing same issue. Please answer if you found please share here.

    – Praful Rajput
    Oct 4 '16 at 12:30











  • i am also facing same issue can any one share the answer?

    – Pradeep Kumar
    Sep 11 '17 at 9:17

















Hi, Did you find solution for this? I am facing same issue. Please answer if you found please share here.

– Praful Rajput
Oct 4 '16 at 12:30





Hi, Did you find solution for this? I am facing same issue. Please answer if you found please share here.

– Praful Rajput
Oct 4 '16 at 12:30













i am also facing same issue can any one share the answer?

– Pradeep Kumar
Sep 11 '17 at 9:17





i am also facing same issue can any one share the answer?

– Pradeep Kumar
Sep 11 '17 at 9:17










2 Answers
2






active

oldest

votes


















1














if you want to remove the dependency on Magento Fulltext catalog search then rewrite Magento/CatalogSearch/Model/Search/IndexBuilder.php file in your custom module and change SQL query(which will get data from your custom table) in function public function build(RequestInterface $request) then layer navigation filters also work properly.






share|improve this answer
























  • please share the code

    – Pradeep Kumar
    Dec 5 '17 at 9:08



















0














The sample code for overriding MagentoCatalogSearchModelSearchIndexBuilder is as follows:




NamespaceModulenameetcdi.xml




<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="MagentoCatalogSearchModelSearchIndexBuilder">
<plugin name="Namespace_Modulename::custom_collection" type="NamespaceModulenamePluginCatalogSearchModelSearchIndexBuilder" />
</type>
</config>



NamespaceModulenamePluginCatalogSearchModelSearchIndexBuilder.php




<?php
namespace NamespaceModulenamePluginCatalogSearchModelSearch;

use MagentoFrameworkSearchRequestInterface;

class IndexBuilder {
protected $_request;
protected $_categoryHelper;

public function __construct(
MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory,
MagentoCatalogHelperCategory $categoryHelper,
MagentoFrameworkAppRequestHttp $request
) {
$this->_productCollectionFactory = $productCollectionFactory;
$this->_categoryHelper = $categoryHelper;
$this->_request = $request;
}

public function aroundBuild($subject, callable $proceed, RequestInterface $request) {
$select = $proceed($request);

$collection = $this->_productCollectionFactory->create();
$collection->addAttributeToSelect('*');
$collection->addAttributeToFilter('necessary_attribute', ['necessary_attribute' => 'attr_value']);
$getProductAllIds = $collection->getAllIds();
$productUniqueIds = array_unique($getProductAllIds);
$select->where("search_index.entity_id IN ('" . join("','", $productUniqueIds) . "')");
return $select;
}
}


Hope this may help you guys!!





share























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "479"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f131224%2fmagento-2-perform-catalog-search-in-my-custom-table%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    if you want to remove the dependency on Magento Fulltext catalog search then rewrite Magento/CatalogSearch/Model/Search/IndexBuilder.php file in your custom module and change SQL query(which will get data from your custom table) in function public function build(RequestInterface $request) then layer navigation filters also work properly.






    share|improve this answer
























    • please share the code

      – Pradeep Kumar
      Dec 5 '17 at 9:08
















    1














    if you want to remove the dependency on Magento Fulltext catalog search then rewrite Magento/CatalogSearch/Model/Search/IndexBuilder.php file in your custom module and change SQL query(which will get data from your custom table) in function public function build(RequestInterface $request) then layer navigation filters also work properly.






    share|improve this answer
























    • please share the code

      – Pradeep Kumar
      Dec 5 '17 at 9:08














    1












    1








    1







    if you want to remove the dependency on Magento Fulltext catalog search then rewrite Magento/CatalogSearch/Model/Search/IndexBuilder.php file in your custom module and change SQL query(which will get data from your custom table) in function public function build(RequestInterface $request) then layer navigation filters also work properly.






    share|improve this answer













    if you want to remove the dependency on Magento Fulltext catalog search then rewrite Magento/CatalogSearch/Model/Search/IndexBuilder.php file in your custom module and change SQL query(which will get data from your custom table) in function public function build(RequestInterface $request) then layer navigation filters also work properly.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Sep 19 '16 at 5:11









    dharadhara

    111




    111













    • please share the code

      – Pradeep Kumar
      Dec 5 '17 at 9:08



















    • please share the code

      – Pradeep Kumar
      Dec 5 '17 at 9:08

















    please share the code

    – Pradeep Kumar
    Dec 5 '17 at 9:08





    please share the code

    – Pradeep Kumar
    Dec 5 '17 at 9:08













    0














    The sample code for overriding MagentoCatalogSearchModelSearchIndexBuilder is as follows:




    NamespaceModulenameetcdi.xml




    <?xml version="1.0"?>

    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="MagentoCatalogSearchModelSearchIndexBuilder">
    <plugin name="Namespace_Modulename::custom_collection" type="NamespaceModulenamePluginCatalogSearchModelSearchIndexBuilder" />
    </type>
    </config>



    NamespaceModulenamePluginCatalogSearchModelSearchIndexBuilder.php




    <?php
    namespace NamespaceModulenamePluginCatalogSearchModelSearch;

    use MagentoFrameworkSearchRequestInterface;

    class IndexBuilder {
    protected $_request;
    protected $_categoryHelper;

    public function __construct(
    MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory,
    MagentoCatalogHelperCategory $categoryHelper,
    MagentoFrameworkAppRequestHttp $request
    ) {
    $this->_productCollectionFactory = $productCollectionFactory;
    $this->_categoryHelper = $categoryHelper;
    $this->_request = $request;
    }

    public function aroundBuild($subject, callable $proceed, RequestInterface $request) {
    $select = $proceed($request);

    $collection = $this->_productCollectionFactory->create();
    $collection->addAttributeToSelect('*');
    $collection->addAttributeToFilter('necessary_attribute', ['necessary_attribute' => 'attr_value']);
    $getProductAllIds = $collection->getAllIds();
    $productUniqueIds = array_unique($getProductAllIds);
    $select->where("search_index.entity_id IN ('" . join("','", $productUniqueIds) . "')");
    return $select;
    }
    }


    Hope this may help you guys!!





    share




























      0














      The sample code for overriding MagentoCatalogSearchModelSearchIndexBuilder is as follows:




      NamespaceModulenameetcdi.xml




      <?xml version="1.0"?>

      <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
      <type name="MagentoCatalogSearchModelSearchIndexBuilder">
      <plugin name="Namespace_Modulename::custom_collection" type="NamespaceModulenamePluginCatalogSearchModelSearchIndexBuilder" />
      </type>
      </config>



      NamespaceModulenamePluginCatalogSearchModelSearchIndexBuilder.php




      <?php
      namespace NamespaceModulenamePluginCatalogSearchModelSearch;

      use MagentoFrameworkSearchRequestInterface;

      class IndexBuilder {
      protected $_request;
      protected $_categoryHelper;

      public function __construct(
      MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory,
      MagentoCatalogHelperCategory $categoryHelper,
      MagentoFrameworkAppRequestHttp $request
      ) {
      $this->_productCollectionFactory = $productCollectionFactory;
      $this->_categoryHelper = $categoryHelper;
      $this->_request = $request;
      }

      public function aroundBuild($subject, callable $proceed, RequestInterface $request) {
      $select = $proceed($request);

      $collection = $this->_productCollectionFactory->create();
      $collection->addAttributeToSelect('*');
      $collection->addAttributeToFilter('necessary_attribute', ['necessary_attribute' => 'attr_value']);
      $getProductAllIds = $collection->getAllIds();
      $productUniqueIds = array_unique($getProductAllIds);
      $select->where("search_index.entity_id IN ('" . join("','", $productUniqueIds) . "')");
      return $select;
      }
      }


      Hope this may help you guys!!





      share


























        0












        0








        0







        The sample code for overriding MagentoCatalogSearchModelSearchIndexBuilder is as follows:




        NamespaceModulenameetcdi.xml




        <?xml version="1.0"?>

        <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
        <type name="MagentoCatalogSearchModelSearchIndexBuilder">
        <plugin name="Namespace_Modulename::custom_collection" type="NamespaceModulenamePluginCatalogSearchModelSearchIndexBuilder" />
        </type>
        </config>



        NamespaceModulenamePluginCatalogSearchModelSearchIndexBuilder.php




        <?php
        namespace NamespaceModulenamePluginCatalogSearchModelSearch;

        use MagentoFrameworkSearchRequestInterface;

        class IndexBuilder {
        protected $_request;
        protected $_categoryHelper;

        public function __construct(
        MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory,
        MagentoCatalogHelperCategory $categoryHelper,
        MagentoFrameworkAppRequestHttp $request
        ) {
        $this->_productCollectionFactory = $productCollectionFactory;
        $this->_categoryHelper = $categoryHelper;
        $this->_request = $request;
        }

        public function aroundBuild($subject, callable $proceed, RequestInterface $request) {
        $select = $proceed($request);

        $collection = $this->_productCollectionFactory->create();
        $collection->addAttributeToSelect('*');
        $collection->addAttributeToFilter('necessary_attribute', ['necessary_attribute' => 'attr_value']);
        $getProductAllIds = $collection->getAllIds();
        $productUniqueIds = array_unique($getProductAllIds);
        $select->where("search_index.entity_id IN ('" . join("','", $productUniqueIds) . "')");
        return $select;
        }
        }


        Hope this may help you guys!!





        share













        The sample code for overriding MagentoCatalogSearchModelSearchIndexBuilder is as follows:




        NamespaceModulenameetcdi.xml




        <?xml version="1.0"?>

        <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
        <type name="MagentoCatalogSearchModelSearchIndexBuilder">
        <plugin name="Namespace_Modulename::custom_collection" type="NamespaceModulenamePluginCatalogSearchModelSearchIndexBuilder" />
        </type>
        </config>



        NamespaceModulenamePluginCatalogSearchModelSearchIndexBuilder.php




        <?php
        namespace NamespaceModulenamePluginCatalogSearchModelSearch;

        use MagentoFrameworkSearchRequestInterface;

        class IndexBuilder {
        protected $_request;
        protected $_categoryHelper;

        public function __construct(
        MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory,
        MagentoCatalogHelperCategory $categoryHelper,
        MagentoFrameworkAppRequestHttp $request
        ) {
        $this->_productCollectionFactory = $productCollectionFactory;
        $this->_categoryHelper = $categoryHelper;
        $this->_request = $request;
        }

        public function aroundBuild($subject, callable $proceed, RequestInterface $request) {
        $select = $proceed($request);

        $collection = $this->_productCollectionFactory->create();
        $collection->addAttributeToSelect('*');
        $collection->addAttributeToFilter('necessary_attribute', ['necessary_attribute' => 'attr_value']);
        $getProductAllIds = $collection->getAllIds();
        $productUniqueIds = array_unique($getProductAllIds);
        $select->where("search_index.entity_id IN ('" . join("','", $productUniqueIds) . "')");
        return $select;
        }
        }


        Hope this may help you guys!!






        share











        share


        share










        answered 4 mins ago









        Dhara BhattiDhara Bhatti

        10819




        10819






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Magento Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f131224%2fmagento-2-perform-catalog-search-in-my-custom-table%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            What other Star Trek series did the main TNG cast show up in?

            Berlina muro

            Berlina aerponto