Jeudi 27 juillet 2023
Search API Drupal : guide complet pour les développeurs

On sous-estime souvent l’importance d’un bon moteur de recherche sur un site web ou une application. Pourtant, c’est une fonction clé pour les utilisateurs notamment lorsqu’ils sont perdus et peinent à identifier ce qu’ils souhaitent. Cette fonctionnalité devient alors le dernier recours avant… le churn ! Si vous utilisez déjà Drupal, ne manquez pas ce plugin indispensable.

    A plugin you definitely don't want to miss!

    Search API Drupal is an open-source module for the CMS (Content Management System) Drupal that optimizes a website's search functionality. It was created in 2011 by German developer Thomas Seidl. Since then, it has undergone constant evolution and regular improvements.

    Over time, it has become one of the modules most popular search engine for Drupal, with over 200,000 installations to date. It is used by many high-profile websites, including government sites, media sites, e-commerce sites and travel sites.

     

    What is the Drupal Search API?

    Search API Drupal is an essential module for web developers and administrators, in fact, it provides a complete infrastructure for search, enabling indexing content efficientlyand searching it quickly. This plugin uses an architecture that allows a high level of customizationthrough a variety of configuration options and settings.

     

    Why use the Drupal Search API?

    The Drupal Search API has several particularly interesting advantages:

    • Efficient indexing : it enables rapid indexing of your website's content and data,
    • A highly customizable solution: it can be configured to meet your specific needs. Developers can thus define search fields, weights for the various fields, and criteria to better meet users' needs.
    • Advanced filters: based on criteria such as categories, publication dates and so on.
    • Features offered in real time: allowing users to see results as they write their query. This feature greatly enhances the experience.
    • Compatibility with other Drupal plugins: such as Facet API, Views, Search Autocomplete, or even Search API Solr and Search API Location.

     

    Requirements for using the Drupal Search API

    Reassure yourself, nothing too complicated at this stage. If you've managed to install Drupal, you'll manage to complete all these steps without any problems:

    Drupal Core

    The Drupal Search API is a module that is integrated into the Drupal core since the version 8. So it's already present in the core and doesn't need to be downloaded or installed separately. To use it, you simply need to activate the module.

    Database Search

    To use the Drupal Search API, you need a Drupal-compatible database management system, such as MySQL, PostgreSQL or SQLite. It uses this database management system to index your website and deliver fast, relevant results.

    Search Facets

    Search Facets is an add-on module to the Drupal Search API. It allows you to add advanced filters. To use it, simply install and activate them.

    Search Views

    Search Views is also complementary to the Drupal Search API. It allows developers to create search views and customize "results". To use Search Views, again, simply install and activate them. It's that easy!

     

    Setting up the Drupal Search API

    Once these steps have been completed, it's time to get started and discover the possibilities offered by this "all-in-one"

     

    Quickly installing the module

    Installation is relatively straightforward. First, you need to download and install the plugin on your site. Then you need to activate it in the section of the same name.

    How to create an index

    To use it, you need to start by creating an index of your site's content. To do this, select the fields to be indexed (the "title", "text", tags, description, etc.)

    You can also add additional filters to improve the relevance of the results.

     

    How to define fields

    In the administration interface, click on Configuration, then on Search and metadata, and finally on Search API. You'll see a list of current indexes and you can click "Add Index" to create a new one.

    Next, you can add fields by clicking on the "Add field" button in the "Fields" tab of your index configuration. You can select the fields you wish to index by checking the corresponding boxes.

    You can also customize the way fields are indexed by modifying the settings for each.

    For example, you can adjust their weights to influence the relevance of resultsor add additional filters to restrict them.

     

    Add additional conditions

    In addition to the basic fields, you can add additional conditions. They allow you to customize indexing to meet the specific needs of your website.

     

    Condition on a boolean field

    Conditions on boolean fields allow you to filter "results" based on the value of a specific boolean field.

    To add such a condition, you need to select the Boolean field from the list of those indexed, then add the condition by clicking on the "Add" button in the "Filters" tab of your index configuration.

    You can then specify whether you want to filter "results" that have the value "True" or "Null".

     

    Condition on a "varchar"

    Conditions on "varchar" fields allow you to filter results according to the value of a specific text field. To add such a condition, you need to select "varchar" from the list of indexed fields, then add the condition by clicking on the "Add" button in the "Filters" tab of your index configuration.

    You can then specify the value you wish to filter, as well as the type of match (e.g. exact match, partial match, etc.).

     

    Condition on a date field

    Conditions on date fields allow you to filter results according to its value. To add such a condition, you need to select the date field in the list of indexed fields, then aadd the condition by clicking on the "Add" button in the "Filters" tab of your index configuration.

    You can then specifystart and end dates to filter results according to the desired date range. You can also specify the date format used in the field to ensure that the "results" match the filtering criteria.

     

    Manage pagination

    To manage pagination, you need to use Views. As its name suggests, it lets you create custom viewswith built-in pagination. To add pagination, you must first create a view in the administration interface: add a "Pager" field. You can then define the number of items to be displayed per page and customize the pagination appearance.

    It's also important to note that pagination management can have an impact on your website's performance, especially if you have many items. To avoid long response times, you can limit the number displayed per page.

     

    Configuring the server correctly: a few tips

    Search server configuration is crucial to ensuring good performance from your website. Here are some common pitfalls to avoid:

    • Don't use a dedicated server;
    • Do not use an optimized index ;
    • Do not monitor error logs ;
    • Don't use a redundant infrastructure : if your server suffers a network failure, this can lead to extended downtime and a poor user experience;

     

    Using the Drupal Search API

    How to initialize a request

    Query initialization is the processing step during which parameters are configured before being sent to the search index. In the Drupal Search API, developers can use hooks and filters before the query is sent to the search index.

    For example, the hook_search_api_query_alter hook lets you modify query parameters before the query is executed, while the hook_search_api_index_alter hook lets you modify search index parameters to optimize performance.

    It's important to note that this step can have a significant impact on performance and user experience. By configuring the parameters correctly, it is possible to improve the relevance of results, reduce response times and offer a smoother, more satisfying user experience.

     

    How to define a query

    Developers can use query objects. Here's an example:

    use Drupal\search_api\Query\SearchApiQueryInterface;

    // Get the Search API Drupal query object.

    $query = \Drupal::service('search_api.query')->create('my_index');

    // Add keywords to the search query.

    $query->keys('search');

    // Execute the search query.

    $results = $query->execute();

    Search API Drupal is a reliable, simple and fast tool for offering quality service to your customers and users. By following best practices and avoiding a few pitfalls, you can facilitate access to your services and products!