Ejemplo n.º 1
0
 /**
  * @return \im\search\components\query\FieldQueryInterface
  */
 protected function getValueSearchQuery()
 {
     /** @var FacetValueInterface $value */
     $value = $this;
     return SearchQueryHelper::getQueryInstanceFromFacetValue($value);
 }
 /**
  * Return search query.
  *
  * @param string $searchableType
  * @param Category $category
  * @param SearchQueryInterface|string $searchQuery
  * @param array $params
  * @return \im\search\components\query\QueryInterface
  */
 public function getQuery($searchableType, Category $category, $searchQuery = null, $params = [])
 {
     $searchComponent = $this->getSearchManager()->getSearchComponent();
     $searchableType = $this->normalizeSearchableType($searchableType);
     // Parse search query
     if ($searchQuery && is_string($searchQuery)) {
         if ($searchableType instanceof QueryParserContextInterface) {
             $searchQuery = $searchComponent->parseQuery($searchQuery, $searchableType);
         } else {
             $searchQuery = $searchComponent->parseQuery($searchQuery);
         }
     }
     // Add category query to search query
     $categoryQuery = $this->getCategoryFieldQuery($category, $searchableType);
     if ($categoryQuery) {
         $searchQuery = $searchQuery ? SearchQueryHelper::includeQuery(clone $searchQuery, $categoryQuery) : $categoryQuery;
     }
     $query = $searchComponent->getQuery($searchableType, $searchQuery);
     $facets = $this->getFacets($category);
     $facetsFilter = null;
     // Exclude category query from search query, merge search query with category parents query and add it to categories facet as filter.
     // This will allow to calculate categories facet not for current category but also for children categories.
     if ($categoryQuery && array_filter($facets, function ($facet) {
         return $facet instanceof CategoriesFacet;
     })) {
         $facetsFilter = SearchQueryHelper::excludeQuery(clone $query->getSearchQuery(), $categoryQuery);
         // Search query by category parents
         $categoryParentsQuery = $this->getCategoryParentsQuery($category, $searchableType);
         if ($categoryParentsQuery) {
             $facetsFilter = SearchQueryHelper::includeQuery($facetsFilter, $categoryParentsQuery);
         }
     }
     foreach ($facets as $facet) {
         $facet->setContext($category);
         if ($facet instanceof CategoriesFacet && $facetsFilter) {
             $facet->setFilter($facetsFilter);
             if (isset($params['categoriesFacetValueRouteParams'])) {
                 $facet->setValueRouteParams($params['categoriesFacetValueRouteParams']);
             }
         }
     }
     $query->setFacets($facets);
     // Exclude category query from search query as we will add category to facet route parameters
     if ($categoryQuery) {
         /** @var QueryInterface|Component $query */
         $query->on(QueryInterface::EVENT_AFTER_RESULT, function (QueryResultEvent $event) use($categoryQuery) {
             $searchQuery = $event->queryResult->getQuery()->getSearchQuery();
             if ($searchQuery) {
                 $event->queryResult->getQuery()->setSearchQuery(SearchQueryHelper::excludeQuery($searchQuery, $categoryQuery));
             }
         });
     }
     return $query;
 }