×

    Get a free Consultation

    Search for:

    Last Updated | November 30, 2021

    What is the Search Criteria?

    Before you start worrying and diving in Magento development deep sea after knowing about this term please keep it simple. This is nothing but literally it just a “Search Criteria” to search/filter the desired result from a repository.

    What is Search Criteria Builder?

    As its name describe it is a search criteria builder that helps you to build your search criteria. The builder concept is new in Magento 2. Basically, Search Criteria Builder is a class instance of what you need to search for. In order to use it, you need Search Criteria Interface. For that add its dependency in your class

    <?php
    
    namespace Folio3\SearchBuilderExample\Controller\Index;
    
    use Magento\Framework\Api\SearchCriteriaBuilder;
    
    class SearchProduct extends Action
    {
    
    protected $searchCriteriaBuilder;
    
    public function __construct(
    SearchCriteriaBuilder $searchCriteriaBuilder
    )
    {
    $this->searchCriteriaBuilder = $searchCriteriaBuilder;
    
    }
    
    }
    

    Filter

    Filters help to create search criteria by filters you can set field, its condition and its value on the basis of which you want to filter/search repository. To use builder you need to add its dependency in class.

    <?php
    
    namespace Folio3\SearchBuilderExample\Controller\Index;
    
    use Magento\Framework\Api\SearchCriteriaBuilder;
    use Magento\Framework\Api\FilterBuilder;
    
    class SearchProduct extends Action
    {
    
    protected $searchCriteriaBuilder;
    protected $filterBuilder;
    
    public function __construct(
    SearchCriteriaBuilder $searchCriteriaBuilder,
    FilterBuilder $filterBuilder
    
    )
    {
    $this->searchCriteriaBuilder = $searchCriteriaBuilder;
    $this->filterBuilder = $filterBuilder;
    
    }
    
    }
    

    Now, let’s see an example how to pass search criteria directly to the repository. We take product repository to filter it with our search criteria that is to search product with SKU

    <?php
    
    namespace Folio3\SearchBuilderExample\Controller\Index;
    
    use Magento\Framework\Api\SearchCriteriaBuilder;
    use Magento\Framework\Api\FilterBuilder;
    use Magento\Catalog\Model\ProductRepository;
    
    class SearchProduct extends Action
    {
    
    protected $searchCriteriaBuilder;
    protected $filterBuilder;
    protected $productRepository;
    
    public function __construct(
    SearchCriteriaBuilder $searchCriteriaBuilder,
    FilterBuilder $filterBuilder,
    ProductRepository $productRepository
    )
    {
    $this->searchCriteriaBuilder = $searchCriteriaBuilder;
    $this->filterBuilder = $filterBuilder;
    $this->productRepository = $productRepository;
    
    }
    
    public function getProducts()
    {
    $filters[] = $this->filterBuilder
    ->setField('sku')
    ->setConditionType('eq')
    ->setValue('something')
    ->create();
    $this->searchCriteriaBuilder->addFilters($filters);
    
    $searchCriteria = $this->searchCriteriaBuilder->create();
    $searchResults = $this->productRepository->getList($searchCriteria);
    return $searchResults->getItems();
    }
    
    }
    

    How to use AND/OR condition in search criteria

    Time to add some more spices. Assume there is a case where you want to filter using AND/OR conditions. Lets take an example where you want to search the product by name or by sku and store.

    <?php
    
    namespace Folio3\SearchBuilderExample\Controller\Index;
    
    use Magento\Framework\Api\SearchCriteriaBuilder;
    use Magento\Framework\Api\FilterBuilder;
    use Magento\Catalog\Model\ProductRepository;
    use Magento\Framework\Api\Search\FilterGroupBuilder;
    
    class SearchProduct extends Action
    {
    
    protected $searchCriteriaBuilder;
    protected $filterBuilder;
    protected $productRepository;
    protected $filterGroupBuilder;
    
    public function __construct(
    SearchCriteriaBuilder $searchCriteriaBuilder,
    FilterBuilder $filterBuilder,
    ProductRepository $productRepository,
    FilterGroupBuilder $filterGroupBuilder
    )
    {
    $this->searchCriteriaBuilder = $searchCriteriaBuilder;
    $this->filterBuilder = $filterBuilder;
    $this->productRepository = $productRepository;
    $this->filterGroupBuilder = $filterGroupBuilder;
    
    }
    
    public function getProducts()
    {
    $filter_sku = $this->filterBuilder
    ->setField('sku')
    ->setConditionType('like')
    ->setValue('%test%')
    ->create();
    
    $filter_name = $this->filterBuilder
    ->setField('name')
    ->setConditionType('like')
    ->setValue('%test%')
    ->create();
    
    $filter_store = $this->filterBuilder
    ->setField("store")
    ->setValue('5')
    ->setConditionType("eq")
    ->create();
    
    $filter_group_1 = $this->filterGroupBuilder
    ->addFilter($filter_sku)
    ->addFilter($filter_name)
    ->create();
    
    $filter_group_2 = $this->filterGroupBuilder
    ->addFilter($filter_store)
    ->create();
    
    $search_criteria = $this->searchCriteriaBuilder
    ->setFilterGroups([$filter_group_1, $filter_group_2])
    ->create();
    
    $searchResults = $this->productRepository->getList($searchCriteria);
    
    $products = $searchResults->getItems();
    
    }
    
    }

    In the above example of magento devlopment, there is the addition of FilterGroupBuilder. Now, FilterGroupBuilder is used when you have multiple AND or OR conditions. Filters are combined with “OR” inside one filter group, and each group is combined with “AND” on the level of search criteria. In our case

    $filter_group_1 = $this->filterGroupBuilder
    ->addFilter($filter_sku)
    ->addFilter($filter_name)
    ->create();
    
    $filter_group_2 = $this->filterGroupBuilder
    ->addFilter($filter_store)
    ->create();
    

    Here OR condition is applied in $filter_group_1 which means product SKU or Name can be like “test”. While $filter_group_2 is used as AND condition. Now search criteria is like “Search * from some table where sku like ‘%test%’ or a name like ‘%test%’ AND store = 5”.

    Sorting

    Field and direction make up the two parameters that define a Sort Order object. The field is the name of the field to sort. The direction is the method of sorting whose value can be ASC or DESC.

    $sortOrder
    ->setField("name")
    ->setDirection("ASC");
    
    $searchCriteria->setSortOrders([$sortOrder]);

    Pagination

    You can limit your result by defining page limit in your search criteria for this we use setPageSize function.

    $searchCriteria->setPageSize(10); //retrieve 10 or less entities

    The setCurrentPage function sets the current page:

    $searchCriteria
    ->setPageSize(10)
    ->setCurrentPage(2); //show the 11th to 20th entity.

    Now, let us take a look to our complete code

    <?php
    
    namespace Folio3\SearchBuilderExample\Controller\Index;
    
    use Magento\Framework\Api\SearchCriteriaBuilder;
    use Magento\Framework\Api\FilterBuilder;
    use Magento\Catalog\Model\ProductRepository;
    use Magento\Framework\Api\Search\FilterGroupBuilder;
    
    class SearchProduct extends Action
    {
    
    protected $searchCriteriaBuilder;
    protected $filterBuilder;
    protected $productRepository;
    protected $filterGroupBuilder;
    
    public function __construct(
    SearchCriteriaBuilder $searchCriteriaBuilder,
    FilterBuilder $filterBuilder,
    ProductRepository $productRepository,
    FilterGroupBuilder $filterGroupBuilder
    )
    {
    $this->searchCriteriaBuilder = $searchCriteriaBuilder;
    $this->filterBuilder = $filterBuilder;
    $this->productRepository = $productRepository;
    $this->filterGroupBuilder = $filterGroupBuilder;
    
    }
    
    public function getProducts()
    {
    $filter_sku = $this->filterBuilder
    ->setField('sku')
    ->setConditionType('like')
    ->setValue('%test%')
    ->create();
    
    $filter_name = $this->filterBuilder
    ->setField('name')
    ->setConditionType('like')
    ->setValue('%test%')
    ->create();
    
    $filter_store = $this->filterBuilder
    ->setField("store")
    ->setValue('5')
    ->setConditionType("eq")
    ->create();
    
    $filter_group_1 = $this->filterGroupBuilder
    ->addFilter($filter_sku)
    ->addFilter($filter_name)
    ->create();
    
    $filter_group_2 = $this->filterGroupBuilder
    ->addFilter($filter_store)
    ->create();
    
    $search_criteria = $this->searchCriteriaBuilder
    ->setFilterGroups([$filter_group_1, $filter_group_2])
    ->create();
    
    $sortOrder
    ->setField("name")
    ->setDirection("ASC");
    
    $searchCriteria->setSortOrders([$sortOrder]);
    
    $searchCriteria->setPageSize(10);
    
    $searchResults = $this->productRepository->getList($searchCriteria);
    
    $products = $searchResults->getItems();
    
    }
    
    }

    That’s all! hope you guys find it easier to understand Search Criteria.

    FAQs:

    Shopify wholesale app development company vs. Magento 2 Development Company: How to compare with each other by Price and extra costs?

    When it comes down to the app development of Magento and Shopify, there are various factors to consider that influence the final cost of wholesale app development. The Shopify app development considers the platform fee, credit card processing fees, hosting, development, and customization costs. On the other hand, Magento 2 cost for development is decided according to the annual revenue of the store, platform fee, hosting, and development factors.


    folio-social-logo
    About

    Ibad is a Senior Software Engineer at Folio3 and has been in relationship with Magento since 5 years. Whenever he has a breakup (that is rare), he spends time reading blogs and trying different types of food.