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 22, 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;
    <span class="hljs-keyword">var</span> qloc = body;

    <span class="hljs-keyword">if</span> (request.url.includes(<span class="hljs-string">'/facet'</span>)) {
        qloc = body.searchContext;
    }

    qloc.aq = <span class="hljs-string">'(@languagecode=="'</span> + props.language + <span class="hljs-string">'")'</span>;

    <span class="hljs-keyword">if</span> (request.url.includes(<span class="hljs-string">'/facet'</span>)) {
        request.body = <span class="hljs-built_in">JSON</span>.stringify(body);
        <span class="hljs-keyword">return</span> request;
    }
    request.body = <span class="hljs-built_in">JSON</span>.stringify(qloc);
}
<span class="hljs-keyword">return</span> 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.

Second CTA Ogilvy's Legacy

Today, David Ogilvy's influence can still be felt in the world of advertising.

Ogilvy's Influence Example
Emphasis on research Market research is a crucial part of any successful advertising campaign
Focus on headlines A strong headline can make the difference between an ad that is noticed and one that is ignored
Use of visuals Compelling images and graphics are essential for capturing audience attention