Share via


SharePoint 2013: Simple search customizations using regular expressions

Introduction

This posting collates notes on implementing simple Search customizations. This posting is updated as new and simple customizations are identified and documented.

A new customer requirement involves the following: whenever a user is at a particular web and performs a search (using the standard search query text box), the search results page should first list the results of the query as applied against a particular list in that web, followed by the general search results. Additionally, this custom search result should appear only when the user searches on a query term that corresponds to a number in a custom column in that list. The numbers that appear in this list range from 5001 to 11945. The solution to this requirement involves the implementation of a promoted results block based upon a new query rule and a regular expression to filter the query term. Let's review this requirement as it applies to Search:

  • Requirement
    • Conditionally promote important results from a custom list
  • Method
    • New Query Rule
  • Scope
    • Web
  • Context
    • Local SharePoint Results
  • Conditions
    • Query term is a number
    • Number is within a specific range {5001..11945}
  • Actions
    • Promote conditional results as a block
    • Block appears before other results
    • Filter results based upon query term equaling a number in the custom column

The solution implemented here doesn't require farm administrator involvement but can be implemented by a site collection administrator or someone having Full Control over the target web.

  1. Navigate to: Settings > Site Settings > Search > Query Rules.

  2. Look for the dropdown that shows "Select a Result Source..." Open this dropdown, and then select Local SharePoint Results (System).

  3. Click on New Query Rule.

  4. Enter a name for this query rule. This name is not seen by users but is the name that will be shown on the Query Rules page.

  5. In the Query Conditions group, from the first dropdown, select Advanced Query Text Match. After you do this, a new set of configuration fields will appear below it.

  6. Select Query matches this regular expression since a regular expression is needed to filter for the range of numbers.

  7. Now enter the regular expression:

    The range of numbers for which the query rule should filter is 5001 - 11945. It is convenient to look at this range is comprising two strings: one of 4 digits and a second one of 5 digits. A regular expression developed for each string engages the range of each digit in that string. Putting these two regex rules together, gives us two regular expressions:

    "[5-9][0-9][0-9][1-9]" and "1[0-1][0-9][0-4][0-5]"

    where individual digit ranges are made known to the regex engine by brackets "[ ]". The first expression filters for the range 5001 - 9999 and the second expression filters for the range 10000 - 11945. Combining these expressions into a single regular expression requires the alternation operator "|", like so:

    [5-9][0-9][0-9][1-9]|1[0-1][0-9][0-4][0-5]

    This is what must be entered into the regular expression box.

  8. Enable the Entire query matches exactly option.

  9. In the Actions group, click Add Results Block. The Add Results Block dialog appears.

  10. Enter a title for the results block. This title will be displayed for the block when it appears on the results page.

  11. In the Query section, click Launch Query Builder. The Build Your Query dialog appears.

  12. Delete any text currently in the Query text box. By default, this will display {subjectTerms}.

  13. From the Property filter dropdown, select the name of the custom column containing the numbers. If you don't find the property listed, select --Show all managed properties-- and try again.

    Note: the property names that you see displayed here are not necessarily the actual current names of list columns. These are the managed properties that are currently mapped to that crawled properties configured for the list columns. If a new column was created at the site collection level, a crawled property is automatically created for it. If its created locally at the list level, a crawled property must be created for it manually. If you are uncertain as to what the managed property is for a column, see the Notes section below for guidance on how to find this.

  14. Just below the Property filter dropdown is a smaller dropdown that by default has Contains selected. Leave this selection as default.

  15. Next to this small dropdown is another dropdown, the Select value dropdown. From this dropdown, select subjectTerms - the entire query.

  16. Lastly, click the Add property filter button. The property filter is then added to the Query text box.

  17. Click OK.

  18. Leave the Search this Source dropdown as default.

  19. Select the number of items you want to have listed in the promoted results block. For the example discussed here, only one result will be of value and that is the result where the custom list column value is equal to the query term value.

  20. Click Save. The new query rule goes into affect immediately.

References

Notes

  • SharePoint hierarchy: Farm > Site Collection > Web
  • Discovering the Crawled Property name associated with a column:
    1. Navigate to the list. Make sure that you are viewing a list view that features the target column. The URL should only display the path to the list, without any parameters.
    2. Perform a sort of the list by that column (hover the cursor over the column title, etc).
    3. Once the sort completes, look up in the URL address bar again. There will now be a hash code and other parameters appended to the core view URL. Look for the SortField parameter, and then note down the value following this parameter (just past the "%3D" and extended to the hyphen "-"). This will be the crawled property name associated with the column you sorted on. Copy this string.
    4. Now go: Settings > Site Settings > Search > Schema.
    5. Click on the Crawled Properties hyperlink.
    6. Paste the string from step 3 into the Crawled properties text box, and then click the green arrow box below. A single result should be displayed. The first column lists the crawled property you searched for and the second column lists the property it is mapped to. This mapped property is what is listed in the Property dropdown in the Query Builder dialog and is the property that you use when creating a custom query.