Setting Up Single Language Results in Coveo Atomic Facet Search

Easily control the language of Coveo Atomic facet search's results by understanding what dataset it's looking at and modifying the request object.

January 23, 2024

By Kelly Parks

Configuring Coveo Atomic’s Facet Search

If you have more than one language in your Coveo index, you might have noticed that the Atomic facet search results show multiple languages. You might not want this behavour. Luckily the solution is simple and involves knowing what data object the facet search is looking at, and using Coveo’s handy preprocessRequest method.

The Problem: Multiple Languages in Coveo Atomic Facet Results

In this example, we are viewing an English version of a search page’s facet. The facet initializes with English options. However as we type a value in the Atomic Facet’s search box, both English and French options appear.

Demonstration of a facet search returning results in different languages in Coveo Atomic 

In our example, we are setting the language in our request body’s aq property. But there’s also a searchContext object.

Language code is set on the body of the payload. SearchContext object is also shown in Coveo Atomic 

If we take a closer look, searchContext has some of the required fields used to execute a Coveo search, but it doesn’t contain our language query.

A closer look at the SearchContext object shows no language code is set on it in Coveo Atomic

The Fix: Set the Language in the SearchContext Object

We can set our language query on the searchContext object by using the engine’s preprocessRequest method. We want to set this only for facet searches, so our code looks like the following:

preprocessRequest: (request, clientOrigin, metadata) => {
    if (clientOrigin === 'searchApiFetch') {
        const body = JSON.parse(request.body.toString()) as any;

        var qloc = body;

        if (request.url.includes('/facet')) {
            qloc = body.searchContext;
        }

        qloc.aq = '(@languagecode=="' + props.language + '")';

        if (request.url.includes('/facet')) {
            request.body = JSON.stringify(body);
            return request;
        }
        request.body = JSON.stringify(qloc);
    }
    return request;
},

Now if we try again, our facet search is now only returning results in the selected language:

Demonstration of the code change affecting the facet search so that it only shows results in one language in Coveo Atomic

Looking at our request payload in a browser’s dev tools shows that the searchContext object now knows which language to fetch results in.

The searchContext object in the request payload now has the language code as a query property in Coveo Atomic

Coveo’s preprocessRequest is a very powerful method that gives so many options from modifying default beahaviour, debugging, to creating new fields on the fly.



Kelly Parks

Kelly Parks

Front-End Developer

Kelly is a Front-end Developer at Fishtank. She enjoys digging into details and working with clients to create solutions. In her free time, she enjoys learning about and growing plants, collecting rocks and minerals, and playing Dungeons & Dragons with her friends.