Take the community feedback survey now.

dada
Jun 4, 2025
  22
(0 votes)

Optimize your queries for performance and search relevance

When working with Search & Navigation, query optimization can have a massive impact on both performance and search quality. Here are some practical tips to help you tune your queries for better speed, lower resource usage, and more relevant search results.

Use single terms query instead of multiple term OR queries

If you're filtering on the same field with multiple term OR conditions, switch to using a terms filter. It’s more efficient and easier to maintain.
In this code this translates to the following:

❌ Bad:

.Filter(x => x.ModelSize.Match("S") | x.ModelSize.Match("M") | x.ModelSize.Match("L"))

✅ Good:

.Filter(x => x.ModelSize.In(new[] { "S", "M", "L" }))


The terms filter is cached in Elasticsearch. This significantly reduces CPU and memory load, speeds up queries, and minimizes shard access.

 

Improve Relevance by Boosting Exact Matches

By default, queries may return results based on stemmed words, synonyms, or even compound terms. To improve relevance, you can explicitly boost exact matches using a custom extension method like InStandardField() below.

public static class QueryStringSearchExtensions
{
    public static IQueriedSearch<TSource, QueryStringQuery> InStandardField<TSource, TExistingQuery>(
        this IQueriedSearch<TSource, TExistingQuery> search,
        Expression<Func<TSource, string>> fieldSelector,
        double? relativeImportance = null)
        where TExistingQuery : QueryStringQuery
    {
        fieldSelector.ValidateNotNullArgument("fieldSelector");
        return search.InField(
            search.Client.Conventions.FieldNameConvention.GetFieldName((Expression)fieldSelector),
            relativeImportance);
    }
}

Usage

    .InStandardField(x => x.Name, 5)         // Boost exact match by factor of 5
    .InStandardField(x => x.SearchText, 5)   // Boost exact match by factor of 5
    .InField(x => x.Name)                    // Standard search
    .InField(x => x.SearchText);             // Standard search

Example: Searching for "vei"

Before Workaround (No boosting):
Many of the matches are compound words or unrelated stems.

"Karriereveiledningskonferansen"
"Vegstrategi"
"Vegforvaltning"
"Fylkesveg"
"Elektrisk vei"
"Vegstrategi"
"Delstrategi veg"
"Nyheter fylkesveg"
"Vegamot AS"
"Stillinger veg"

After Workaround (Boosted relevance):
Results now prioritize exact matches:

"Elektrisk vei"
"Delstrategi veg"
"Stillinger veg"
"Kontaktinformasjon veg"
"Veg-prosjekter"
"Høringssvar delstrategi veg"
"Søknad om reklame langs veg"
"Fravik fra krav i vegnormal"
"Trøndelagsmodellen - skreddersydd veg til sikker jobb"
"Stenger veg i Overhalla"

As you can see, the relevance of results is dramatically improved. Exact matches like "veg" are ranked higher, and compound or less-relevant terms are deprioritized.

Summary

  • Replace term OR logic on the same field with terms filters to improve query performance.
  • Use field boosting via extensions like InStandardField() to elevate exact matches over stemmed, synonyms or compound terms.

By following these patterns, you'll get faster queries and better results — a win-win for both backend performance and user satisfaction.

Jun 04, 2025

Comments

Please login to comment.
Latest blogs
A day in the life of an Optimizely OMVP - Opticon London 2025

This installment of a day in the life of an Optimizely OMVP gives an in-depth coverage of my trip down to London to attend Opticon London 2025 held...

Graham Carr | Oct 2, 2025

Optimizely Web Experimentation Using Real-Time Segments: A Step-by-Step Guide

  Introduction Personalization has become de facto standard for any digital channel to improve the user's engagement KPI’s.  Personalization uses...

Ratish | Oct 1, 2025 |

Trigger DXP Warmup Locally to Catch Bugs & Performance Issues Early

Here’s our documentation on warmup in DXP : 🔗 https://docs.developers.optimizely.com/digital-experience-platform/docs/warming-up-sites What I didn...

dada | Sep 29, 2025

Creating Opal Tools for Stott Robots Handler

This summer, the Netcel Development team and I took part in Optimizely’s Opal Hackathon. The challenge from Optimizely was to extend Opal’s abiliti...

Mark Stott | Sep 28, 2025

Integrating Commerce Search v3 (Vertex AI) with Optimizely Configured Commerce

Introduction This blog provides a technical guide for integrating Commerce Search v3, which leverages Google Cloud's Vertex AI Search, into an...

Vaibhav | Sep 27, 2025

A day in the life of an Optimizely MVP - Opti Graph Extensions add-on v1.0.0 released

I am pleased to announce that the official v1.0.0 of the Opti Graph Extensions add-on has now been released and is generally available. Refer to my...

Graham Carr | Sep 25, 2025