SharePoint 2013 Search: Keep getting Search Results even when there is no query made

Introduction

This is a quick one which caused me some problems on a project I have been working on.

So a quick introduction to SharePoint 2013 Search. SharePoint 2010 had a feature in search called “Search Scopes” this provided a way to select the content that you wanted to return results from based on certain criteria. For example if it had a certain Content Type or was found in a particular location.

In SharePoint 2013 Scopes have been replaced by Result Sources which provide the same feature plus some much more powerful selection criteria using tokens. For example you can retrieve the user performing the search and return content where the user is the author.

Problem

The problem is that I have a search page which has both the search query web part and a search results web part.

After setting up a custom Result Source query to refine the results down I would always get results displaying, even before a query had been submitted search.

The Result Source is configured by:-

  • Browsing to the Site Collection
  • Click on the Site Settings Icon
  • Choose Site Settings->Site Collection Administration->Search Result Sources
  • Create a New Result Source
  • Part of the setup is to create the Query Transform, this brings up the window below

Here is what was setup:-ThisQueryAlwaysReturnsResults

The query always returns search results. Normally results only appear when you click on the Test tab, providing the search query and click Test Query.

This is a problem as because the search will always execute as the user goes on the page and is just plain messy.

So I spent sometime trying to work out what was going on.

Solution

I have to say the solution took sometime and it was only after re-reading the following Technet article http://technet.microsoft.com/en-us/library/dn186229.aspx#BKMK_How_does_query_transform_affect_query the section at the bottom “Narrowing search results by using a Query Transform” gave me the answer.

The key to all of this is that my query is not waiting for the search term before executing the search, it just always executes the search.

The example that the article provides is this:-

{?{searchTerms} ContentClass=urn:content-class:SPSPeople}

So that gave me the solution, the important thing is that if you want the query to wait for search terms to be filled in then wrap your query with curly brackets.

So the problem query goes from this:-

{searchTerms} (IsDocument=True AND (ContentTypeId:0x0101009CA89B0104F04692A106A1F88E847F7D*))

to this:-

{?{searchTerms} (IsDocument=True AND (ContentTypeId:0x0101009CA89B0104F04692A106A1F88E847F7D*))}

When I use the query above with the Query Transform this is the result, note the query returns no results.

ThisQueryWaitsforQueryBeforeReturnsResults

The query that I have will take any query and ensure that only search results that are documents that have a ContentTypeId which starts with a particular value.

I hope that helps someone!

13 Comments

  1. i want to restrict the search results coming from a specific site collection
    can you pls provide whats the query transform i need to use

    Reply

  2. Helpful article, thanks! Q – So if I wanted to see all the results how would I get that? The wildcard * does not seem to be working. Any thoughts?

    Reply

    1. Hi,
      So there are some limits on the number of results that you can have on one page, so generally you cannot see ‘all’ the results on one page without some custom development.

      However, you can increase the number of results displayed by the web part by editing the page.
      Choose the Core Results Search Web part and choose Edit Web Part. The section, Search Results, on the right hand side will be displayed to allow you to change the web part settings. Expand the Settings section and there is an option to set the number of items to display. This has a maximum of 50 on my environment.

      HTH

      Reply

Thoughts? Comments about this post? Please leave them here..

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s