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".


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



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.



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".



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



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:



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



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:



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



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:
- Content Query Web Part and Content Search Web Part in SharePoint 2013

- SharePoint 2013 Online Content Search WebPart with Query String

- Microsoft Flow Step by Step Tutorial with Examples

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: 




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:



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

<

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:



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



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.