Problem/Motivation

Users may benefit from enhanced ability to sort results according to standards including relevance and age (oldest, newest).

Proposed resolution

Ideally, we could push a parameter (or parameters) to the search url that would return sorted results. Any other mechanism would also suffice, however.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

terracoders created an issue. See original summary.

jastraat made their first commit to this issue’s fork.

jastraat’s picture

Example custom plugin based on the new plugin from the MR to order by newest to oldest by default and optionally use the "orderby" query parameter:

<?php

namespace Drupal\custom_vertex_module\Plugin\SearchOrder;

use Drupal\Core\Entity\EntityTypeManager;
use Drupal\vertex_ai_search\Plugin\VertexSearchOrderPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;

/**
 * Custom Order.
 *
 * @VertexSearchOrderPlugin(
 *   id = "vertex_search_order_custom",
 *   title = @Translation("Custom Order"),
 * )
 */
class CustomOrder extends VertexSearchOrderPluginBase {

  /**
   * The request stack service.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected RequestStack $requestStack;

  /**
   * The entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManager
   */
  protected EntityTypeManager $entityTypeManager;

  /**
   * {@inheritdoc}
   */
  public function __construct(
    array $configuration,
    $plugin_id,
    $plugin_definition,
    RequestStack $request_stack,
    EntityTypeManager $entity_type_manager,
  ) {

    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->requestStack = $request_stack;
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {

    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('request_stack'),
      $container->get('entity_type.manager'),
    );

  }

  /**
   * {@inheritdoc}
   */
  public function getSearchOrder() {
    $current_request = $this->requestStack->getCurrentRequest();

    // Default order by date published descending.
    $order_string = 'datePublished desc';

    // Get order from query string.
    $orderby = $current_request->query->get('orderby');

    // Create an order string from query parameters.
    if (!empty($orderby)) {
      $order_string = $orderby . ' desc';
    }

    return $order_string;
  }

}

This could be enhanced by an additional query parameter for whether the sort is descending or ascending.

timozura’s picture

Version: 1.8.2 » 1.9.0
Status: Active » Fixed

Merged in MR. Marking as Fixed. Will be in release 1.9.0

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.