How to customize default query by setting up advanced expression

How to customize default query by setting up advanced expression

As we all know, we can setup the default Search Interface and setting up the Query pipelines in Experience editor will give the default search result from the updated Coveo Index.

If you want to know more about Query Pipelines & Search Interface, Please refer my older post(Search Interface & Query Pipelines).

In this article, I am going to show you how to customize the query by overriding the advanced expression in CoveoForSitecore. Before we move into the customization we need to know about what is Query Parameters ?

Query Parameters

While setting up the Query pipelines, we can choose either basic query or advanced query to execute the search query in terms of conditions. Both basic and advanced query requires the parameters or properties to fulfil the condition which is known as Query Parameters.

When we setup the basic Search Interface without having any filters gives the basic result directly from the indexed document. when we configure the facets/filter in the application then the query will modify and send the respective facet value to fetch the filtered data from the index. How this action happens in Coveo ?

This is happening because of the expression setting up in the query. when we have no expressions, then it will give the default result based on the Query Pipelines. If we setup the expression, then the result will be different.

There are number of expressions available in the Coveo for Sitecore. we have basic expression, advanced query expression, constant query expression, hidden query expression, query ranking expression etc.

Now we are going to see how to override the advanced query expression in Coveo for Sitecore. Before we move into the override options, we need to understand how to debug and check the value from the rendered DOM element in dev tool.

First access the search interface element by using below code.

var root = document.getElementById("#<search_interface_id>")        

Next step to access the Coveo element

Coveo.$$(root)        

when we put the above code in the console window after the page renders, you can see the below components rendered.

When we see inside the "CoveoResultList", there is a "queryController" section available. we are going to see what is there inside the queryController.

Under the queryController, there is aq(advanced query expression) which has only basic filter with @z95template. But we have an options to override this advanced query.

Next step to override this advanced query expression. To start with the Search Interface sitecore item, there is an option called "Initialization File Name".

As we see that I mentioned the "InitCustomFile" is the initialization cshtml file which we kept in the following Hive folder "\Coveo\Hive\init" in the application virtual directory.

Coveo for Sitecore package itself has provided the init.cshtml file. For rewriting the logic for customizing the advanced query expression, we should use the following code in the "InitCustomFile".

@using Sitecore.Mvc
@using Coveo.UI.Components
@using Coveo.UI.Components.Extensions
@using Coveo.UI.Core.ErrorReports
@model Coveo.UI.Components.Models.SearchInterfaces.ISearchInterfaceModel
<script type="text/javascript">
    document.addEventListener("CoveoSearchEndpointInitialized", function() {
        setTimeout(function() {
            var searchInterface = document.getElementById("@Model.Id");
	    Coveo.$$(searchInterface).on('buildingQuery', function(e, args) {
            args.queryBuilder.advancedExpression.addFieldExpression('@("@z95xtagname")', '==', ['American Energy']);
     		})
            @if (Html.Coveo().IsEditingInPageEditor()) {
                @: if (typeof(CoveoForSitecore) !== "undefined" && typeof(Sitecore) !== "undefined") {
                    @: CoveoForSitecore.initializeSearchInterfaceForExperienceEditor(searchInterface);
                @: }
            } else {
                @: if (typeof(CoveoForSitecore) !== "undefined") {

                    @: CoveoForSitecore.initSearchInterface(searchInterface);
                @: }
            }
        }, 0);
    });
</script>        

In the above code, the logic should be written after the initialization of Coveo search endpoint. Once we get the Coveo components rendered, we should write the customization logic under the event "buildingQuery".

buildingQuery is one of the Query events. Query events are triggered during the execution of a search request to the Coveo Search API.

To set the advanced expression value for a QueryBuilder instance, use the advancedExpression property.

For Example, I've added the "American Energy" as @z95xtagname in the advanced Expression. we can see how the query gets changed after this initialization.

With all that in place, we have implemented the search by customizing the search results using advanced query expression.

That's the end of this article.

Happy Sitecoring !

要查看或添加评论,请登录

Mohamed Sirajudeen的更多文章

  • Why Sitecore Developers trying to Integrate with AI ?

    Why Sitecore Developers trying to Integrate with AI ?

    The world is already moving towards Artificial Intelligence (AI), which offers powerful solutions to streamline tasks…

    5 条评论
  • My First View Towards Sitecore Stream

    My First View Towards Sitecore Stream

    Introduction When we think about the future of artificial intelligence and how Sitecore products are going to utilize…

  • Resolve Unable to Verify Leaf Signature Issue

    Resolve Unable to Verify Leaf Signature Issue

    During the development of Sitecore Headless (formerly known as Sitecore JSS), many encounter an issue labeled "Unable…

  • Serialization using Sitecore CLI Tool

    Serialization using Sitecore CLI Tool

    As many of you aware of Sitecore Serialization, I am writing this article to understand why this is so important and…

  • Deployment Options for Sitecore Headless Next.js Apps

    Deployment Options for Sitecore Headless Next.js Apps

    Introduction As we observe, many organizations are currently prioritizing the transition from Sitecore Traditional MVC…

  • Internationalization in the Sitecore Headless for Next Js

    Internationalization in the Sitecore Headless for Next Js

    In the Sitecore Headless application using Next.js created based on the Next.

  • Setup Local Sitecore Instance to Public using ngrok

    Setup Local Sitecore Instance to Public using ngrok

    In this article, we are going to see how to setup the local sitecore URL to be available for public using ngrok. This…

  • Rendering Contents Resolvers

    Rendering Contents Resolvers

    In this article, we are going to see how we can use Layout Service to resolve the rendering contents. So, Sitecore…

  • Routing using Next.js

    Routing using Next.js

    In this article, we are going to see how routing works in Sitecore Headless using next.js.

  • Implement Carousel using Next.js

    Implement Carousel using Next.js

    In this article, we are going to see how to setup the multiple data in the Sitecore and how to implement developers…

社区洞察

其他会员也浏览了