Skip to content

Product search

This example uses the product search API to search products using searchterm:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
 /**
     * Returns all products
     * @param $offset
     * @param $limit
     * @patram $locationId
     * @return \Siso\Bundle\SearchBundle\Api\Catalog\ProductSearchResult
     */
    private function getAllProducts($offset, $limit, $queryString, $locationId = 2)
    {
        $searchService = $this->get('siso_search.search_service.product');
        $searchGroup =  $this->container->get('ezpublish.config.resolver')->getParameter('groups.product_list', 'siso_search');
        $searchContextService = $this->get('siso_search.search_context_service');
        $searchContext = $searchContextService->getContext();
        $facetService = $this->get('siso_search.facet_service.simple_field');

        $query = new EshopQuery();
        if ($queryString != '' ) {
            $query->addCondition(
                new SearchTermCondition(
                    array(
                        SearchController::SEARCH_CONDITION_TERM => $queryString
                    )
                )
            );
        }
        $query->addCondition(
            new ContentTypesCondition(
                array(
                    SearchController::SEARCH_CONDITION_CONTENT_TYPES =>
                        $searchGroup[SearchController::PRODUCT_LIST_GROUP][SearchController::SEARCH_CONDITION_CONTENT_TYPES]
                )
            )
        );
        if ($locationId > 2) {
            /** @var CatalogDataProviderService $catalogService */
            $catalogService = $this->get('silver_catalog.data_provider_service');
            $catalogProvider = $catalogService->getDataProvider();
            $catalogElement = $catalogProvider->fetchElementByIdentifier($locationId);
            $query->addCondition(
                new SubtreeCondition(
                    array(
                        'path' => implode('/',$catalogElement->path )
                    )
                )
            );
        }
        $formData = array();
        // Add facets
        $productFacets = $facetService->buildFacets($formData, 'product_list');
        foreach($productFacets as $productFacet) {
            $query->addFacet($productFacet);
        }

        $query->setOffset($offset);
        $query->setLimit($limit);
        return $searchService->searchProducts($query, $searchContext);
    }

You can filter by a search engine field in PHP code in the following way:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
use Siso\Bundle\SearchBundle\Api\SearchContext;
use Siso\Bundle\SearchBundle\Api\EshopQuery;
use Siso\Bundle\SearchBundle\Controller\SearchController;
$query = new EshopQuery();

$queryString = 'content_type_id_id:2 AND ses_product_ses_datamap_ses_brand_value_s:HP';

$query->addCondition(
    new SearchQueryCondition(
        array(SearchController::SEARCH_CONDITION_QUERY => $queryString)
    )
);
$searchService = $this->getContainer()->get('siso_search.search_service.product');
$result = $searchService->searchProducts($query, new SearchContext());