SharePoint Search Results WebPart Not Returning All Results

I ran into a strange issue when working with the search result web part on a client's SharePoint 2013 on-prem environment. I was utilizing the search results web part alongside my people search table item display template to show an employee directory.

The client noticed that in some searches, it wasn't returning all of the results. For example, if they ran a query for "Department: IT" it would show 15 results but there were actually 20 people in the IT department. Our first step was to examine the data to make sure it was correct. Result: Everything looked fine. Next, we looked at the user profile service and search configurations to make sure everything was kosher. So far everything was looking good.

I decided to download the Search Query Tool to do some testing. I put in my query and got the same number of results as I was getting in my search results web part. I started playing around with the settings in the Search Query Tool and decided to uncheck the "Trim Duplicates" option and re-run the query. As soon as I did that, I had 20 results instead of 15.

Given this new information, I did some Googling. I quickly found out that the Search Results Web Part has a configuration to automatically trim duplicates which you cannot override. I found some blog posts that were showing how you could export the web part and modify the .webpart file to change this setting and re-upload. I didn't want to go through all of that though.

Luckily, I happened onto this post by Elio Struyf. Elio shows a javascript approach to get around this issue. In his example, he adds a checkbox to "Show Duplicates" and when that is clicked it displays the duplicate results. I wanted to bypass that step and just automatically show the duplicates so I used this code: To implement this solution I just inserted a Script Web Part on the page containing my Search Results Web Part and pasted in the above code. And Viola! The People Search Results Query was returning the expected results!