Take the community feedback survey now.

dada
Jun 4, 2025
  869
(4 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

Johan Book
Johan Book Jun 4, 2025 06:45 PM

Good stuff!

Stefan Holm Olsen
Stefan Holm Olsen Jun 7, 2025 06:26 PM

Great points, Daniel.

Your example about terms filter is not even that bad. There are much worse, out in the wild.

dada
dada Jun 11, 2025 11:23 AM

Thank you, Stefan! Yes, the original chain of OR term filters was actually much larger—imagine an OR filter for every possible size you can think of. I was just trying to get the point across using as few bytes as possible. :)

Please login to comment.
Latest blogs
Running Optimizely CMS 12 on .NET 10 in Azure

Upgrade your Optimizely CMS website to .NET 10!

Tomas Hensrud Gulla | Nov 21, 2025 |

Experimentation Evolution with AI (Masterclass Recap)

If you think you are not using AI in your experimentation program you are probably wrong. Ever asked an AI to rephrase a hypothesis or brainstorm a...

Polly Walton | Nov 21, 2025

Effortlessly Configurable Custom Fields for Scheduled Jobs in Optimizely with PlugInProperty

Optimizely CMS lets developers add job parameters—such as textboxes and checkboxes—directly to the scheduled job admin UI using PlugInProperty. Wit...

Ravindra S. Rathore | Nov 21, 2025 |

Optimizely Package Explorer: Now With Extra Superpowers

If you’ve ever opened a .episerverdata file and asked “What is in here?” (guilty as charged) — then this is your moment. We’ve given our open-sourc...

Allan Thraen | Nov 20, 2025 |