Enhancing search result relevance in Drupal can be achieved by configuring Solr Query Re-Ranking and utilizing the Search API. Follow these steps to fine-tune your search results:
Prerequisites
- Drupal installed and configured.
- Apache Solr installed and running.
- Search API and Search API Solr modules installed in Drupal.
Steps
- Configure Solr Server:
- Navigate to Configuration -> Search and Metadata -> Search API.
- Add a new server and select Solr as the backend.
- Enter the required details such as the Solr endpoint URL.
- Create an Index:
- Go to Configuration -> Search API and add a new index.
- Assign the index to the Solr web server you configured earlier.
- Select the content types to be indexed.
- Save the configuration and start indexing your content.
- How to build a website with WordPress and what are the best plugins to use: Building a website with WordPress is an excellent choice due to its versatility, ease of use, and a vast array of plugins that enhance functionality. Here’s a comprehensive guide to building a WordPress website, along with recommendations for the best plugins.
- What does this property buzzword mean and how does it actually work? Gearing simply means borrowing money to buy an asset. Negative gearing can be a tax strategy used by investors and describes when the income (ie, the rent) made from an investment is less than the expenses it incurs, meaning it’s making a loss.
- How to Sell Your Ecommerce Business for the Best Value: Selling an ecommerce business can be a very profitable move. You can use the proceeds to invest in other projects, launch new ecommerce business websites, or even retire. For some startups, selling the business is the end goal. Whether you have a dropshipping website, sell with Amazon FBA, or own a large-scale ecommerce business, there’s an opportunity for you to sell.
- Comprehensive Guide to WordPress Website Development: Developing a WordPress website is a sequential process that requires careful planning, thoughtful execution, and consistent maintenance to ensure it meets the needs of users and achieves its intended goals. This involves a series of clearly defined stages, including planning, designing, content creation, optimisation, and ongoing maintenance.
- Top 10 High-Paying Jobs AI Won’t Replace in 2025: Artificial Intelligence (AI) is revolutionizing industries, automating repetitive tasks, and reshaping the global workforce. Despite its remarkable advancements, certain professions remain beyond AI’s capabilities due to their reliance on uniquely human traits like creativity, empathy, and critical thinking. This case study explores the 10 highest-paying, fastest-growing jobs in 2025 that AI won’t replace, delving into why these roles are indispensable and how they are evolving in an AI-driven world.
- Spill Your Guts: What To Wear To Olivia Rodrigo’s Australian Tour: Never afraid of screaming out all the dark, embarrassing things we’ve all thought before, Rodrigo sings about comparing herself to her boyfriend’s ex-girlfriend. If you want an edgy outfit that mimics the music…
- Top Social Media Plugins for WordPress to Increase Your Sites Reach and Engagement: If you are seeking to enhance your website’s reach and engagement on social media, you have come to the right place. In this article, we will delve into the premier social media plugins tailored for WordPress users. From Social Warfare to Jetpack, these plugins can facilitate seamless sharing of your content across diverse social platforms.Furthermore, we will provide recommendations to optimize your website’s visibility on social media. Keep an eye out for valuable insights!
- How to Change PuTTY’s Appearance: PuTTY is a widely-used SSH and telnet client for Windows and Linux hosting. While its default appearance is functional, you can customise it to improve aesthetics and usability. Below are the steps to modify PuTTY’s appearance, including changing the font, window size, colours, and cursor options.
- What programming languages does vBulletin use?: vBulletin was orginally written in perl and used a flat file database system. However, as sites grew they notice that sites could not cope with a large amounts of traffic. This problem has now been fully rectified when vBulletin was converted to php and a mysql database system.
- Edit the Solr configuration files (e.g.,
solrconfig.xml
andschema.xml
) to enable query re-ranking. - Use the
rerank
andrerankQuery
parameters to configure re-ranking behavior in your queries.
- In Drupal, configure the Search API settings to leverage Solr’s re-ranking capabilities.
- Adjust the weights of fields and other ranking criteria to improve result relevance.
- Perform search queries and analyze the results.
- Iteratively adjust the configuration to achieve the desired search relevance.
Additional Tips
- Regularly update your Solr schema and configuration to leverage new features and improvements.
- Monitor search performance and tweak the settings as your content evolves.
The path to better search results
Today, we still maintain both versions. The use of our legacy Drupal content has steadily decreased, yet it still has a substantial presence in search results. This often leads to confusion, especially when members trying to learn about features in modern Drupal find themselves on a legacy Drupal tutorial.
We have always enabled faceted searching, which allows members to narrow down the results to a specific version of Drupal after performing the initial search. Recently, based on member feedback, we decided to explore additional methods to better surface content relevant to modern Drupal, which is what the majority of our members now use.
Initially, I thought setting the “Drupal 10” facet as the default could address this issue. However, after spending an entire day exploring this, I realized this approach was impractical. Facets filter based on values present in the result set, so if a search only returns “Drupal 7” content, the “Drupal 10” facet won’t appear, and you cannot select an option that does not exist.
First attempt: boosting
I then explored using boosting. Boosting adjusts the relevance score of an item based on query time criteria. For example, you might give higher relevance to a result if the keywords in the search query appear in the title field rather than in the body. The Search API Solr module already supports this and we use it to rank courses and guides higher than tutorials.
I considered creating a Search API processor plugin similar to the existing solr_boost_more_recent
to add a Lucene expression to the document boost factors. This Lucene function runs as part of the query and uses the result as a multiplier to boost the document. For instance, you could boost the relevance of any document tagged with “Drupal 10” in the versions field by 5, regardless of the query.
$boosts = $query->getOption('solr_document_boost_factors', []);
$boosts['taxonomy_versions'] = sprintf('if(termfreq(%s,"%s"),%2F,0.0)', SolrBackendInterface::FIELD_PLACEHOLDER, $term, $boost_factor);
if ($boosts) {
$query->setOption('solr_document_boost_factors', $boosts);
}
This results in a Solr query like:
{!boost b=sum(if(termfreq(tm_X3b_und_version,"Drupal 10"),5.0,0.0))} (tm_X3b_und_body:+"testing" ...)
However, this method does not work with multi-word phrases like “Drupal 10”. While I could get it working with “10”, this solution was not ideal.
I also tried other variations, such as indexing the term ID field as an integer to see if the term ID is in the multi-value integer field:
if(itm_taxonomy_versions:1461,5.0,0.0)
or
if(exists(query({v=itm_taxonomy_versions:"1045"})),10,0)
After much experimentation and consultation in Slack, I was directed towards query re-ranking as a possible alternative.
Using query re-ranking to influence sort order in Solr search results
Query re-ranking in Solr adjusts the order of search results after the initial query execution. A re-ranking query applies a secondary scoring phase to the top N results. Unlike boosting, re-ranking modifies the order based on additional criteria post-query.
Run the query for the keyword, “views”, then take the results and run another query against those results, applying a ranking weight to any document that matches the second query.
I wrote an event subscriber that hard codes a re-ranking query to all searches on Drupalize.Me:
<?php
declare(strict_types=1);
namespace DrupaldmeEventSubscriber;
use Drupalsearch_api_solrEventPreQueryEvent;
use Drupalsearch_api_solrEventSearchApiSolrEvents;
use SymfonyComponentEventDispatcherEventSubscriberInterface;
class DmeSearchApiSolrSubscriber implements EventSubscriberInterface {
public static function getSubscribedEvents(): array {
return [
SearchApiSolrEvents::PRE_QUERY => 'preQuery',
];
}
public function preQuery(PreQueryEvent $event): void {
$solarium_query = $event->getSolariumQuery();
$rq = '{!rerank reRankQuery=$rqq reRankDocs=1000 reRankWeight=-7 reRankOperator=add}';
$solarium_query->addParam('rq', $rq);
$solarium_query->addParam('rqq', '(itm_taxonomy_versions:1045 OR itm_taxonomy_versions:1044)');
}
}
This approach allows specifying a negative reRankWeight
, which effectively demotes Drupal 7 content, and avoids the need for future adjustments when new Drupal versions are added.
While I chose a hard-coded approach to minimize custom code, I also developed a proof-of-concept showing that re-ranking could be implemented in a Search API processor plugin with a configuration form.
In the preprocessSearchQuery()
method of the plugin, you can set Solr query string parameters using:
$query->setOption('solr_param_rq', $rq);
This uses the solr_param_*
prefix, which the Search API Solr module recognizes and applies to the Solarium query.
Now, when you perform a search on our site, the results should be more relevant.
Next, I plan to explore adding support for Solr 9’s DenseVector searching feature to perform semantic searches. This will help return relevant tutorials for queries such as “How do I put fields on the page?” even if those specific phrases are not in the text.
Learn more about Solr and Drupal
If you’re interested in learning more about integrating Apache Solr and Drupal, check out our Search API and Solr in Drupal course.