πŸ‡§πŸ‡ΎBelarus @andreylugovtsov

Account created on 21 October 2014, about 10 years ago
#

Recent comments

πŸ‡§πŸ‡ΎBelarus andreylugovtsov

mime_type.guesser service not found, Drupal 10.3.5

πŸ‡§πŸ‡ΎBelarus andreylugovtsov

In case someone else facet the problem.
Solution is to register an event subscriber and modify Solr query according to documentation.

/**
 * Add tags to fq fields and facet.field fields to exclude each facet's
 * filtering constraints from influencing its own facet item's result counts,
 * to always keep the result counts of each facet item independent of that
 * facet's filtering.
 *
 * @see https://solr.apache.org/guide/7_6/faceting.html#tagging-and-excluding-filters
 */
class ProcessFacetFilterQuery implements EventSubscriberInterface {

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents(): array {
    return [
      Events::POST_CREATE_REQUEST => 'processFacets',
    ];
  }

  /**
   * Process facets fields.
   *
   * @param \Solarium\Core\Event\PostCreateRequest $event
   *   The event.
   */
  public function processFacets(PostCreateRequest $event): void {
    $request = $event->getRequest();
    $params = $request->getParams();

    foreach ($params['facet.field'] ?? [] as $facetField) {
      /*
       * Spotting an "ex=facet:" tag inside facet field value means
       * that facet is configured in OR mode. That works right.
       * Problem occurs, when that facet is also used in a query condition.
       * That makes it lose its values.
       * A modification to fq param should be applied in that case.
       */
      $fieldName = preg_match('/ex=facet:(.*)}/', $facetField, $matches) ? $matches[1] : NULL;
      if ($fieldName) {
        $this->modifyFilterQueryParam($params, $fieldName);
      }
    }

    $request->setParams($params);
  }

  /**
   * Modify fq filter param.
   *
   * @param array $params
   *   Request params.
   * @param string $facetField
   *   Facet field name.
   *
   *   If facet filter is being applied as a query condition,
   *   fq param should be modified to exclude facet filter
   *   from losing its values.
   *
   * @see https://solr.apache.org/guide/7_6/faceting.html#changing-the-output-key
   * @see \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::setFacets()
   */
  private function modifyFilterQueryParam(array &$params, string $facetField): void {
    foreach ($params['fq'] as $key => $field) {
      if (str_contains($field, $facetField)) {
        $params['fq'][$key] = "{!tag=facet:{$facetField}}" . $field;
      }
    }
  }

}
πŸ‡§πŸ‡ΎBelarus andreylugovtsov

Were you able to find a solution?
Facing the same problem

πŸ‡§πŸ‡ΎBelarus andreylugovtsov

andreylugovtsov β†’ made their first commit to this issue’s fork.

πŸ‡§πŸ‡ΎBelarus andreylugovtsov

andreylugovtsov β†’ made their first commit to this issue’s fork.

πŸ‡§πŸ‡ΎBelarus andreylugovtsov

andreylugovtsov β†’ made their first commit to this issue’s fork.

πŸ‡§πŸ‡ΎBelarus andreylugovtsov

andreylugovtsov β†’ made their first commit to this issue’s fork.

πŸ‡§πŸ‡ΎBelarus andreylugovtsov

andreylugovtsov β†’ made their first commit to this issue’s fork.

πŸ‡§πŸ‡ΎBelarus andreylugovtsov

andreylugovtsov β†’ made their first commit to this issue’s fork.

Production build 0.71.5 2024