Search code examples
search-enginegoogle-custom-search

Create custom google search with options to search the web and my site


I want to create a custom google search for my site. My requirements are as follows:

  1. I need a text box for search and two radio buttons(one for searching my site and other for searching web).
  2. Depending on the radio button selected the results should be displayed.
  3. I have a separate search results page ('/search').

I know how to create a simple google custom search. But this requirement of providing an option to search the site or web is a little tricky for me. Please help


Solution

  • I was able to solve this issue.

    I first created a search engine in https://cse.google.com/cse/ for my site and configured it to search the web in Basics tab-> Sites to Search option. With this the search engine would search the entire web.

    The code that I placed on the search results page had something this:

    "gcse:searchresults-only as_sitesearch='site to search'"

    This is a part of the code that google gives you.

    Here the parameter 'as_sitesearch' will control which site is to be searched. When the radio button of Searching the web was selected I did not pass any value to 'as_sitesearch' and when the radio button of Searching my site was selected I passed the site url to 'as_sitesearch'.

    This worked for me