SharePoint Online: How to create a result source and use custom result source in content search web part

Introduction:

 Here we will discuss how we can create a custom result source and then how we can use the custom result source inside a content search web part in SharePoint online.

Result Sources:

 SharePoint provides result sources by using which we can limit search results to specific content. When a user performs a search, the system matches the query with a result source to provide results. SharePoint comes with many preconfigured result sources for you to use, including Conversations, Documents, and Pictures etc.

We can also create custom result sources at various scopes like:
- Service Application Level / SharePoint Admin Center for Office 365: When we create result scope at search service application level, the result source will be available to any site collection that consumes the Search Service Application.
- Site Collection: When we create result scope at the site collection level then the result source can be used by any site within the site collection.
- Site: When we create result scope at the site level then the result source can be used by that site only.

Create Custom Result Sources:

 We can create a custom result sources at site collection level as well as site level. To create custom result sources at the site collection level go to the Site Settings and then click on "Search Result Sources" which is under "Site Collection Administration".

https://www.enjoysharepoint.com/wp-content/uploads/2018/09/custom_result_sources_sharepoint_online.png

And if you want to create a custom result sources at a site level then click on "Result Sources" which is under "Search".

https://www.enjoysharepoint.com/wp-content/uploads/2018/09/custom_result_sources_sharepoint_online_office_365.png

Here we will create a custom result source for Site level. So click on Site settings and then click on "Result Sources" which is under "Search".
This will open the Manage Result Sources page, here click on "New Result Source" like below:
Here on this page also you can see lots of default result sources as well as custom result sources.

https://www.enjoysharepoint.com/wp-content/uploads/2018/09/custom_result_sources_office_365_sharepoint_online.png

Here in General Information section give a Name, Description of the custom result source. Then Protocol as Local SharePoint. And Type as "SharePoint Search Results".

https://www.enjoysharepoint.com/wp-content/uploads/2018/09/create_custom_result_source_sharepoint_online.png

Now in the Query Transform click on "Launch Query Builder" like below:

https://www.enjoysharepoint.com/wp-content/uploads/2018/09/create_custom_result_source_sharepoint_online_office_365.png

This will open the Query Builder where we can build the search query. Here we will modify the Query text like below:

When we are writing the query like below:
path: "https://onlysharepoint2013.sharepoint.com/sites/Bhawana/MySearchTestDocLib/"

We are telling that we only want results from the specified path and anything under it like folders, sub folders or sub site etc. 

But we will modify the query a bit to search only a particular type of file like a docx file.

path: "https://onlysharepoint2013.sharepoint.com/sites/Bhawana/MySearchTestDocLib/" FileType:docx

The FileType property instructs the query to only return files that have a .docx extension. The full query looks like below:

https://www.enjoysharepoint.com/wp-content/uploads/2018/09/result_source_to_get_only_word_document_sharepoint_2013.png

You can also click on the Test Query button to see the result like below, where it will display only docx files.

https://www.enjoysharepoint.com/wp-content/uploads/2018/09/result_source_to_get_only_word_document_sharepoint_2016.png

Similarly if you want to add multiple filters then you can add like below:

path: "https://onlysharepoint2013.sharepoint.com/sites/Bhawana/MySearchTestDocLib/" FileType:docx FileType:txt FileType:pdf

Only you save the query then now Query Transform and Credentials Information will look like below:

https://www.enjoysharepoint.com/wp-content/uploads/2018/09/result_source_to_get_only_word_document_sharepoint_online.png

Then click on Save which will create our custom result source which we can see under Manage Result Sources page like below:

https://www.enjoysharepoint.com/wp-content/uploads/2018/09/result_source_to_get_only_word_document_sharepoint_online_1.png

Now our result source is ready and in the next step, we will see how we can use it in a content search web part.

How to use Custom Result Source inside a Content Search Web Part?

Now we will add a content search web part to a web part page. If you are new to the content search web part, then you can see a few articles below:

You can create a web part page in SharePoint 2013. And then edit the page and then from web part categories Content Rollup choose Content Search like below: 

https://www.enjoysharepoint.com/wp-content/uploads/2018/09/custom_result_source_content_search_web_part.png

Once the web part added, the web part will display recently changes items. Now edit the web part properties. In the Content Search web part properties, click on Change query like below:

https://www.enjoysharepoint.com/wp-content/uploads/2018/09/custom_result_source_content_search_web_part_sharepoint_online.png

This will open the Build Your Query window and here click on Switch to Advanced Mode:

<https://www.enjoysharepoint.com/wp-content/uploads/2018/09/custom_result_source_content_search_web_part_sharepoint_2013.png

In the Advanced Mode, in the Select a query, you will be able to see the custom result source which we have created. Choose it and if you will click on Test Query you will be able to see the results also:

https://www.enjoysharepoint.com/wp-content/uploads/2018/09/custom_result_source_content_search_web_part_sharepoint_2016.png

Click on OK and Save the Page, you can see the search results like below where it will only show word documents.

https://www.enjoysharepoint.com/wp-content/uploads/2018/09/SharePoint_2013_custom_result_source_content_search_web_part_sharepoint.png

Conclusion:

 Here we have seen how we can create a custom result source as well as how we can use it in a content search web part.