In this article, we will see how to use the Google custom search site restricted JSON API. For people looking to skip coding and directly implement a search for their websites, go here.
Custom Search Engine offers very similar functionality to the Google Site Search but comes with a major disadvantage: ads. If you’re not a nonprofit or school then you’ll be stuck with ads. On the plus side, you can monetize the ads by connecting your Adsense account, but you’ll still likely be advertising for competitors. You can try search engines other than google that don’t track you
Custom Search Site Restricted JSON API intro
Points to remember- (if you do not adhere to the following points your API might not return the search results)
- Google Custom Search Site Restricted JSON API is similar to the JSON custom search API.
- You can search within 10 or lesser websites using this API.
- No daily Query limit.
- Make sure that the “search the entire web” is set to OFF.
Create your google custom search from https://cse.google.com
Google Custom Search Site Restricted JSON API is of the following format-
https://www.googleapis.com/customsearch/v1/siterestrict?[parameters]
Query parameters
Here parameters could be –
- q– query string
- cx– Custom search engine ID.
- fileType– bmp, gif, jpg, png, pdf etc
- num-number of search results to display
- searchType- search type.
- sort-Sort by functionality to use.
Here are the list of all parameters-
On-site search is a crucial feature on a website. When your visitors are on your website, no matter how they arrived, they might want to explore more. Of course, a popular posts widget on the sidebar or related posts listed under every blog post will help them read more content and stay on your site for a long time. But what if they are looking for something really specific on your website? Learn how to add a search bar to your website in HTML
Sample API Call
Here is a sample api call to get search results-
https://www.googleapis.com/customsearch/v1/siterestrict?key={your_api_key}&cx={custom_search_engine_id}&q={search_keyword}
https://www.googleapis.com/customsearch/v1/siterestrict?key=AIzaSyDOvbipdULbUpVm3HLjoX7bEEfDUKHELh4&cx=012278024424817052234:cimrvj4h4uk&q=linkedin
Search results in JSON format
Here are the search results in JSON format-
{ "kind": "customsearch#result", "title": "LinkedIn: Log In or Sign Up", "htmlTitle": "\u003cb\u003eLinkedIn\u003c/b\u003e: Log In or Sign Up", "link": "https://www.linkedin.com/", "displayLink": "www.linkedin.com", "snippet": "500 million+ members | Manage your professional identity. Build and engage \nwith your professional network. Access knowledge, insights and opportunities.", "htmlSnippet": "500 million+ members | Manage your professional identity. Build and engage \u003cbr\u003e\nwith your professional network. Access knowledge, insights and opportunities.", "cacheId": "AszVZRcWxZEJ", "formattedUrl": "https://www.linkedin.com/", "htmlFormattedUrl": "https://www.\u003cb\u003elinkedin\u003c/b\u003e.com/", "pagemap": { "metatags": [ { "referrer": "origin", "pageimpressionid": "b502334b-1c2a-442a-b05b-6883abbfb21b", "appname": "chrome", "pagekey": "uno-reg-guest-home", "treeid": "bjU+kvwgSRWw0apPLysAAA==", "globaltrackingurl": "//www.linkedin.com/mob/tracking", "globaltrackingappname": "chrome", "globaltrackingappid": "webTracking", "msapplication-tileimage": "https://static.licdn.com/scds/common/u/images/logos/linkedin/logo-in-win8-tile-144_v1.png", "msapplication-tilecolor": "#0077B5", "application-name": "LinkedIn", "remotenavjscontentbaseurl": "https://static.licdn.com/scds/concat/common/js?v=0.1.561", "lnkd-track-json-lib": "https://static.licdn.com/scds/concat/common/js?h=2jds9coeh4w78ed9wblscv68v-ebbt2vixcc5qz0otts5io08xv", "apphost": "seo-directory-frontend", "appversion": "0.1.343", "appinstance": "i002", "locale": "en_US", "baidu-site-verification": "bqQqxRHTnv", "detectadblock": "//platform.linkedin.com/js/px.js" }
Adding a search box to a website can quickly get tricky if not done right. Developing a search engine from scratch is never a good idea keeping in mind the cost and quality. Google’s own solution to this was GSS which got shut down and replaced by Google Custom Search. You can learn how to Google search specific website
Getting your custom search API Key-
How to get your custom search API Key-
- Go to https://developers.google.com/custom-search/json-api/v1/overview
- Click on get a key.
- Click create new project.
- This will generate your new API key.
Getting your CX / Search engine ID:
Go to your custom search / site search control panel-> edit search engine->Basics->Details->Search engine ID.
Testing with Google API explorer
You can test you google custom search api using the google api explorer
Read more on google search script
Expertrec’s Google custom search replacement API to overcome google custom search limitations
Go to https://cse.expertrec.com?platform=cse and enter your website and sitemap URLs to get your crawl done. Go and click on your demo link.
Go to https://cse.expertrec.com/csedashboard/home/Status to get your API key
A static website with a dynamic search function? Yes, Hugo provides an alternative to embeddable scripts from Google or other search engines for static websites. Check out hugo themes with search
Expertrec custom search API format
The API will be of this format-
https://searchv7.expertrec.com/v6/search/{org_id}/?q={keyword}&page={pagination}&size={number_of_search_results}
https://searchv7.expertrec.com/v6/search/79b96660-27e0-11e8-a8e3-12b6486824f4/?q=a&page=0&size=16
Expertrec site search API parameters-
- org_id- Your unique ID that identifies your website/ websites.
- q- Keyword.
- page- pagination
- size- number of search results to be displayed.
The results will be in JSON format and will be similar to this screen shot-
{ "0len":1, "1query":"a", "facet_interval":{ }, "facets":{ "type":{ "webpage":160 } }, "fq":{ }, "nf":{ }, "numeric":{ }, "res":{ "count":160, "st":0, "time":3 }, "results":[ { "date":"2018-05-09T12:35:47.110Z", "host":"blog.expertrec.com", "id":"https://blog.expertrec.com/wordpress-custom-search/", "image":[ "https://blog.expertrec.com/wp-content/uploads/2018/04/6.png" ], "popularity_score":1.0, "score":1.25, "snippet":{ "content":[ "How to <span class=highlight>create</span> <span class=highlight>a</span> wordpress custom <span class=highlight>search</span> - 2018 Expertrec   Support <span class=highlight>magento</span> <span class=highlight>search</span> engine Google site <span class=highlight>search</span> <span class=highlight>replacement</span> Expertec Toggle <span class=highlight>Navigation</span> How to <span class=highlight>create</span> <span class=highlight>a</span> wordpress custom <span class=highlight>search</span> Published by <span class=highlight>muthali</span> <span class=highlight>ganesh</span> on <span class=highlight>April</span> 8, 2018 <span class=highlight>Creating</span> wordpress custom <span class=highlight>search</span> using expertrec is <span class=highlight>as</span> <span class=highlight>easy</span> <span class=highlight>and</span> <span class=highlight>can</span> be <span class=highlight>taken</span> live in 5 minutes. " ], "metatag.description":[ ], "title":[ "How to <span class=highlight>create</span> <span class=highlight>a</span> wordpress custom <span class=highlight>search</span> - 2018 Expertrec" ] }, "title":"How to create a wordpress custom search - 2018 Expertrec", "type":"webpage", "url":"https://blog.expertrec.com/wordpress-custom-search/" } ], "sfacets":{ "type":[ { "count":160, "name":"webpage" } ] } }
Did you know that the default WordPress search engine isn’t really that efficient (and awesome) as the rest of WordPress? Check out here how you can get WordPress advanced search plugin.
Google search script for sites is an easy and effective way to add search capabilities to your site. You can learn more about Google custom search JS examples
You can try how to turn off Ads in Google search to remove ads in GCS.
If your Google site search is not working you can refer Google custom search support to find a solution.
Looking for how to add multiple websites in google custom search include sites in bulk
Sometimes you might want to see more than ten results on the Google SERP (Search Engine Results Page). Increasing the result count per page is pretty simple. You can read google custom search pagination to know how
You can also try searching multiple websites at once