I'm working in a SharePoint Online setup where https://domainname.com is being used as root site collection of the organization to host intranet content and then they created site collections for various purposes like https://domainname.com/sites/department1 and so on. Now how can I limit the search results from returning content only from https://domainname.com without bringing the results anything after https://domainname.com/*. I also want search results in the same webpart to show from specific site collections https://domainname.com/sites/department2 and https://domainname.com/sites/department4. Please let me know how can I write the query accordingly. I know it would have been much easier if the root site collection used for Intranet would have been created like https://domainname.com/sites/intranet, so that it could have been used for querying content and limiting other site collections. I also tried creating a specific result source and using it to define scope but didn't work.
You can modify the search query to get the results from specific sites.
So if you want to get the results from department2 and department4 you can append the Path property in Query Text as
And similarly you can scope the result to top level site by using Equal and Contain property while adding the property filter. I would recommend to go through these links Link 1 and Link 2 .. and play around with different options available in
Property Filter --> Path