Share via


SharePoint Search 2013 - Changing the default search operator from AND to OR - Part 1

In 2013, you may want to change the default AND search operator between search terms to an OR.

Requirement: emulate an ANY() query by default.

...

 

The quickest (and dirty) way  to do it is to change it in the search results web part properties.

 

Step by Step

  1. Edit the WP properties
  2. Hit the Change Query

 

3. Wrap the {searchTerms} with ANY(...)

4. Issue a query to show the OR outcome : 

The same query with an AND operator would only give us 1 result:

 

=> There is a catch though: you won't be able to use the full KQL syntax in your search box without potentially breaking the query syntax.

 

 Let me demonstrate

Query with search results default properties: ALL (collection Assets) title:Site

 

 

 Once we implemented our change to force an ANY(...), issuing the same query gives us a bad query syntax.

 

Searching for the correlation id in the ULS logs gives us the following information. First how we transform the query based on the template we defined in our Search Results Web Part.

ProductivitySearchFlowExecutor: New request: Query template 'ANY({searchboxquery})' transformed to query text 'ANY(ALL (collection Assets) title:Site)'.

After the default query transformation, we now apply the templates of one Result Source aka Local SharePoint Results (which excludes results of type People):

QueryTemplateHelper: Query template '{?{searchTerms} -ContentClass=urn:content-class:SPSPeople}' transformed to query text 'ANY(ALL (collection Assets) title:Site) -ContentClass=urn:content-class:SPSPeople'.

As noted the Local SharePoint Search is adding a filter on the content-class.

But soon the newly built query throws a syntax error:

ExecuteFlowInternal Flow:Microsoft.SharePointSearchProviderFlow Exception: Microsoft.Office.Server.Search.Query.QueryMalformedException:

Query 'ANY(ALL (collection Assets) title:Site) -ContentClass=urn:content-class:SPSPeople' failed: syntax error at position 4, unexpected ALL, expecting UNQUOTED_STRING, or QUOTED_STRING

The QueryMalformedException is the one throwing us a "We didn't understand your search terms. Make sure you're using the proper syntax".

Conclusion

The described trick might suit some basic needs but poses some serious restrictions (query syntax).

 

Part 2 of this post will describe another method to change the default search operator from AND to OR by using Result Sources.

Stay tuned.