Custom Query Rules | Yext Hitchhikers Platform

Query rules allows you to modify search results by adding precise rules. Check out the Query Rules unit for more details.

String of Numbers

You can target a 5-7 number string via RegEx (Regular expressions). Regular expressions allow you to search for a pattern of characters, and allows more flexibility when you’re looking for query matches. In this case, this query rule would surface a specific FAQ when a string of numbers is searched.

"rules": [
{
  "criteria": {
    "searchTermMatchesRegex": "\\b\\d{5,7}\\b"
  },
  "actions": [
    {
      "actionType": "BOOST_ENTITIES",
      "entityIds": [
        "FAQ-0"
      ],
      "verticalKey": "faqs"
    }
  ]
}


searchTermContains Multiple Search Terms

When writing query rules to prescibe search behavior, the searchTermContains criteria can be used to trigger a rule when the query contains defined terms. This will do a token match on the search term. If multiple tokens are included, it will make sure they are all present.

For example, the value ["Heart attack","911"] would trigger the rule in the case the query contains both “Heart attack” and “911”.

If, instead, you wanted to make the logic “or” (i.e., trigger the rule when at least 1 of the terms is in the query), you could use searchTermMatchesRegex, using | as an OR operator.

For example, putting the criteria below in the JSON of your query rule would trigger the query rule whenever a query contains “Heart attack” OR “911” (note: multiple terms, each separated by the | OR operater, can be added):

"criteria": {
    "searchTermMatchesRegex": [ ".*(heart attack|911).*"]
}

The result of this code is that the query rule would now be triggered on searches containing either “Heart attack” or “911”. For example, if the action associated with this rule was to boost urgent care locations, this action would be triggered for any of the following example searches:

  • “symptoms of a heart attack”
  • “when to call 911”
  • “should I call 911 if I am having a heart attack”

This can help you avoid having to write multiple query rules that trigger the same action.


Referrer URLs to Filter Location Results

To filter results to only those that pertain to the online location (ie. website) a user originates from, Query Rules can be use to detect the URL a user searched from, map it to a value of the custom field “Query Rules Location”, and filter the result set to only the entities that have that value populated in the custom field.

For example, take someone searching from a page on “location1.com”. We will see that a user searched from a page matching this subdomain, map that to the custom field value of “Location 1”, and filter the results such that only results with the field “Query Rules Location” containing “Location 1” appear in the results.

Steps:

1. Mapping URLs to the “Query Rules Locations” custom field

  • Create a custom field called “Query Rules Location” that has a multi-option selections for each location URL.

  • Within each entity type, select which Query Rule location the type falls under within the “Query Rules Location” field.

  • Once all of your locations are labeled correctly, also update the Query Rules Location custom field for all FAQs and Services and click on all the location options within the field. Note: This is so that all FAQs and Services will surface in the Search experience for all location pages.

2. Create Saved Search Filters

  • In order to filter results in Query Rules based on the location page the user is searching from, we need to create saved search filters for each location page.

  • Within the Content, create an advanced filter with the following search criteria:

    • Fields with Data includes any of Query Rules Location
    • Query Rules Location includes Choose the Location you want to filter for
  • Apply filter and save the Filter.

3. Creating Query Rule in Search Configuration

Navigate to the Search COnfiguration and add the following:

"rules": [
  {
    "criteria": {
      "referrerPageURLRegex": "^.location1.com"
    },
    "actions": [
      {
        "actionType":"ADD_FILTER",
        "verticals": [
          "faqs", 
          "jobs", 
          "locations"
        ],
        "filter":{
          "savedFilterId":{
            "$eq":"21466424"
          }
        }
      }
    ]
  }
]
book
Note
You must be on Theme v1.20 or above to use default initial search. Check out the Upgrade the Theme and SDK module for more info and the Theme Upgrade guide for step-by-step instructions on upgrading.

The typical user journey is that a user enters a search into a search bar and then lands on the search results page with exactly the answer they’re looking for. However, sometimes users land on the search results page before a query has been run (or perhaps the brand didn’t have the bandwidth to build the preferred searchbar so they only included a link to the search experience from their homepage). When this happens, they see an empty page!

Now, you can pin results to an empty query to automatically showcase specific results when this happens:

Steps:

  1. In your index.json, update the SearchBar component in componentSettings with "allowEmptySearch": true

  2. In your index.json, add pageSettings to include the default initial search. This should go outside of componentSettings (same place where it is in your verticals).

    "pageSettings": {
    "search": {
      "defaultInitialSearch": "" // Enter a default search term, should be your pinned query
    }
    },
  3. Update your Search configuration to include a pinned result. Here’s a sample:

    {
      "criteria": {
        "searchTermMatchesRegex": "^$",
        "searchTypes": "UNIVERSAL"
      },
      "actions": [
        {
          "actionType": "RETURN_PINNED_RESULT",
          "verticals": [
            {
              "savedFilterId": "[[ENTER FILTER ID]]",
              "verticalKey": "videos"
            },
            {
              "savedFilterId": "[[ENTER FILTER ID]]",
              "verticalKey": "states"
            }
          ]
        }
      ],
      "name": "Default Search"
    }
Feedback