Showing posts with label Search Refinement. Show all posts
Showing posts with label Search Refinement. Show all posts

Thursday, October 17, 2013

SharePoint 2013 Search: Changing Refinements from 'OR' to 'AND'

By default when you implement refiners in SharePoint 2013 Search, the search experience when selecting the multi-value refinements, the filtering of the search results follows "OR" logic. If you selected SharePoint and Visio, for example, the results will display all items that either contain SharePoint or contain Visio (or both). In some cases, the user may want to combine the values such that only items that contain both SharePoint and Visio are displayed.

I started looking into this within the refinement filter code but also asked two SharePoint buddies that have been working with refinements recently if they saw a way to do this. They both came up with the same solution.

The first response and more detailed solution came from CCDW.

Here is the solution (based on modifying the system function in place) - the right way to implement this is discussed after the initial discovery:

PART I - Javascript Function

Step 1:  Locate the Search.ClientControls.js file in C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\template\layouts.

Step 2: Open the debug version of this file and locate the following line:

Srch.Refinement.submitMultiRefinement = function Srch_Refinement$submitMultiRefinement(name, control, useContains, useKQL) {

and change to:

Srch.Refinement.submitMultiRefinement = function Srch_Refinement$submitMultiRefinement(name, control, useContains, useKQL, searchBoolean) {

Here we are adding a fifth parameter named searchBoolean which we will use to pass in AND or OR

Step 3: Check  to see if the parameter was passed.

Add this line in the function:

searchBoolean = searchBoolean || 'OR';

If the parameter is not passed, we set it to the original default value such that it continues to work with legacy code.

Step 4: Change the last line to:

$v_0.updateRefinementFilters(name, $v_3, searchBoolean, useKQL, $v_4);

We've overridden the original hardwired 'OR', to our searchBoolean param!

PART 2 - Display Template

Step 1: Edit the Filter_MultiValue_body.html display template (technically you should create a copy and then set the refinement to use your custom multi-value refinement)

Step 2: Locate this section around line 90:

 <div id="SubmitValue">
                        <div id="submit">
                            <a onclick="Srch.Refinement.submitMultiRefinement('_#= $scriptEncode


Step 3: Change the onclick to:

onclick="Srch.Refinement.submitMultiRefinement('_#= $scriptEncode(propertyName) =#_', $getClientControl(this), _#= $htmlEncode(useContains) =#_, _#= $htmlEncode(useKQL) =#_,'AND');

Add a fifth string param to the function in the last line. Note I have added the 'AND'. The AND (or OR) will be passed as the searchBoolean param in the function within the mods made earlier.  If we don't pass anything the default is applied in the function.

Reset IIS to be sure and it should now work!!


The correct way to implement this is to copy the function and paste it into the refinement template. In working with Mikael Svenson (Microsoft MVP) on the same problem, he added these example steps (which I modified to flow with CCDW's answer):

What you would do in your refinement template is something like this:

Type.registerNamespace('TheMan.Search');
TheMan.Search.submitMultiRefinement = function(name,control,useContains, useKQL, searchBoolean) {
 // copy the contents from search.clientcontrols.debug.js
 // change the last line to use searchBoolean instead of "OR"  (as explained in Part I - Step #4 above)
}


Then change your onclicks to read TheMan.Search.submitMultiRefinement() instead of Srch.Refinement.submitMultiRefinement()


HOPE THIS HELPS!!! SEARCH ON!



 

Saturday, July 13, 2013

SharePoint 2013 Search: Cascading Refinements of Multi-Value Properties

My cascading refinement solution builds upon the instant refinement solution described in my previous post. The modifications update the result counts in other multi-refinement property sections as well as disables the filters that are no longer available because of the initial refinement.

This post describes a high level overview of the steps necessary for the cascading refinement functionality. You may find the code and more detailed steps in my new Enhancing the Search Experience in SharePoint 2013 guide book.


Step 1: Store the Property Selected First

Since the cascading is based on the property that is refined first, you must store the property.
 

Step 2: Evaluate the List Data and Modify Accordingly

At the top of the file, create a new array to store and process the list data and previous refiners. Add logic to set the list data to the previous refiners.

Step 3: Determine if Filter is Still Available and Update the Refinement Count

Within the !hasNoListData condition block, change the refiners.push logic.
 

Step 4: Flag Filters that Should Be Disabled


Flagging the filters that should be disabled involves two modifications. The first is to add logic to the hasFilterTokens condition. The second portion involves adding an else condition to the hasFIlterTokens if statement.

Step 5: Save Changes to the Template File


Step 6: Modify the Body Display Template

Within the for-loop, create an isDisabled variable and a fontWeight variable.
In the isSelected if statement, add fontweight=”bold”; :
Branch the else statement to check for isDisabled.

For the disabled version, just copy the input for the unselected check box but add the disabled=”disabled” property.

Finally in the label, add the a style property using the fontWeight variable.

Save the changes to the body file.

Step 7: Test the Cascade Functionality


Navigate to the search results page that contains the multi value refinements:


 
Click on one of the filters.
 
GET ALL OF THE CODE AND DETAILED STEPS IN MY NEW SEARCH EXPERIENCE GUIDE:

 

SPECIAL LOW KINDLE PRICE!!!

Monday, June 24, 2013

SharePoint 2013 Search: Adding Search Result Counts to Your Refinements

Overview
The Refinement web part has the ability to display result counts along side the refinement values for a particular managed property out-of-the-box. However, this option is not available within the web part properties. Enabling search result counts in your refinements requires a tweak within the refinement display templates.

Displaying Counts for All Refinements
The easiest configuration is to simply turn counts on for all refinements. This is achieved by modifing the Filter_Default.html and Filter_MultiValue.html display templates. Using SharePoint Designer 2013, you may access the display templates easily by following the similiar steps as outlined here.

This time instead of opening the Search sub-folder under Display Templates, open the Filters folder:

 
Locate Filter_Default.html. Right-click the file and select Edit File in Advanced Mode:


Inside the code locate the ShowCounts variable and change from false to true:

 
Save the changes. Your single value refinements now display the counts. Repeat the same process on the Filter_MultiValue.html to display counts on your multi-value refinements.
 
Displaying Counts for Selective Refinements
There may be situations or search pages where you do not wish to display the refinement counts. For this case, and for best practices sake, it is recommended to make a copy of the Filter templates and create a custom refinement display template accordingly.
 
Simply right-click the Filter_Default.html file and select Copy:


Right-click again and select Paste:

 
Rename the file accordingly:
 


Edit the new copied file and change both the Title and ShowCounts values:

 
Save the changes. Repeat the same process for the Filter_MultiValue.html if desired.
 
Now when configuring the Refinement web part, when you choose the refiners, your new display template is available in the drop-down:
 


 
You may now choose to show refinement counts on single and/or multi-value refinement properties:
 



Search On!

 

Matched Content