Adding Pipeline Context to a Search Page Using the Interface Editor

You can define custom pipeline contexts (in the form of key/value pairs) to help you optimize the Coveo™ search results relevance (see What’s Next?).

You must then configure each Coveo JavaScript search page to pass the appropriate custom context with each query. The easiest way to do it is with the Interface Editor (see JavaScript Search Interface Editor). You may however need developer skills when you want to configure dynamic custom contexts.

When the Interface Editor is not available for the environment in which your Coveo JavaScript search page is deployed, you must write code to pass the appropriate custom contexts (see Adding a Custom Context to Queries).

Adding a Static Pipeline Context

A static pipeline context is passing the same custom context key/value pair(s) for all queries made from a search interface.

If you want to add more than one value for a given key, in the upper right corner of the Interface Editor, click Code View, and then, at the bottom if the page code, add the following script block with the desired key and values.

<script class="CoveoPipelineContext" type="text/x-context">
&#123;&#34;MyKey&#34;&#58;&#91;&#34;MyValue1&#34;&#44;&#34;MyValue2&#34;&#93;&#125;</script>
  1. Access the Interface Editor for the search page in which you want to add pipeline context (see Accessing the Coveo Interface Editor).

  2. In the upper-left corner, click the cog icon to show the UI Settings panel.

    IntEdit2-PipelineContext1

  3. Click in the Select a settings box, and then select Pipeline context.

  4. In the Pipeline context panel:

    1. In the Options tab, click Add Pipeline Context.

    2. In the Key parameter, enter your custom context key.

    3. In the Value parameter, enter your custom context value.

    4. Repeat the previous steps when you want to add other custom context key/value pairs.

    5. Click Save.

    IntEdit2-PipelineContext2

    The current search interface, allowing to search for your product technical documentation, is used exclusively by people that are administrators of your products, not by other audiences such as product end-user, or product develops).

    In such a case, adding a custom context userRole key and a administrator value is legitimate to indicate that all queries made from this search interface are in a userRole = Administrator context.

    If you look in the Interface Editor Code View tab, you can see that the following script element has been added at the bottom of the page code.

     <script class="CoveoPipelineContext" type="text/x-context">
     &#123;&#34;userRole&#34;&#58;&#34;Administrator&#34;&#125;</script>
    

Adding a Dynamic Salesforce Pipeline Context

When using Coveo for Salesforce, you can send Salesforce values as your context.

If you want to add more than one value for a given key, in the upper right corner of the Interface Editor, click Code View, and then, at the bottom if the page code, add the following script block with the desired key and values.

<script class="CoveoPipelineContext" type="text/x-context">&#123;&#34;myKey&#34;&#58;&#91;&#34;&#123;&#33;MyValue1&#125;&#34;&#44;&#34;&#123;&#33;MyValue2&#125;&#34;&#93;&#125;</script>
  1. Access the Interface Editor for the search page in which you want to add pipeline context (see Accessing the Coveo Interface Editor).

  2. In the upper-left corner, click the cog icon.

  3. In the UI Settings panel, click in the Select a settings box, and then select Pipeline context.

  4. In the Pipeline context panel:

    1. In the Options tab, click Add Pipeline Context.

    2. In the Key parameter, enter your custom context key.

    3. In the Value parameter, enter a Salesforce value using the following syntax: {!value}.

      You want to add the user id of the user performing the search to the context. As the value, you enter {!user__userid}.

    4. Repeat the previous steps when you want to add other custom context key/value pairs.

    5. Click Save.

Adding a Dynamic Pipeline Context

When the custom context value must change depending on some variables, you must write and include JavaScript code to dynamically set the custom context value.

  1. Access the Interface Editor for the search page in which you want to add pipeline context (see Accessing the Coveo Interface Editor).

  2. In the Interface Editor upper-right corner, click Code View tab to access the search page code.

  3. At the bottom of the page code, before the last closing </div> element, adapt and paste a piece of code similar to the following snippet:

     <script>
         getCustomContext = function() {
             // Your code to return the current custom context in a JSON key/value object. 
             // {"myKey1":["myValue1","myValue2"],"myKey2":["myValue3","myValue4"]}
             ...
         };
         const customContext = getCustomContext();
         const root = Coveo.$$(document).find("#search");
         Coveo.$$(root).on("buildingQuery", function(e, args) {
             args.queryBuilder.addContext(customContext);
         });
     </script>
    

    where:

    • You write a function to dynamically build the JSON object containing the one or more custom context key/value pairs for the current context.

    • You replace, if needed, the CSS selector (#search) to get the main element of your search interface or recommendation component.

    Your search interface allows authenticated users to search for your product technical documentation. You can write a getCurrentUserRole() function to get the role (End-user, Administrator, or Developer of your products) of the current user from his or her user profile. Your function returns a JSON object with one key/value pair such as:

     {"userRole" : "End-user"}
    
  4. In the Interface editor upper-right corner, click Save.

What’s Next?

You can then take advantage of the custom context in: