is it possible to specify a region/locale for google custom search engine? - google-custom-search

I want to use the Google Custom Search API (or custom search engine directly) in the following manner--
(1) I want to get the set of results for a search term for Google UK(This should return results that are returned by a regular search on Google.co.uk- Google UK)
(2) I want to get the results for a search on Google.co.in(Google India)
Is it possible to do the above, either directly while creating a custom search engine, or through the API?

Yes. It is possible. Specifiy the parameter googlehost in your query. Example: &googlehost=google.de or whatever country you want.

It is in the settings, can change from the Control Panel.

Related

Search in mutliple sites using Google Custom Search JSON

Trying to figure out how can i search in mutliple sites using Google Custom Search JSON API.
Meaning that search will be only from a specific sites list.
i was playing with the api explorer - https://developers.google.com/custom-search/v1/reference/rest/v1/cse/list?apix_params=%7B%22cx%22%3A%22011602274690322925368%3Atkz2zvvpmk0%22%2C%22siteSearch%22%3A%22www.walla.co.il%22%7D
and noticed the site search query key, but it can only accept a single string not a list of sites:
enter image description here
What is the way to search in only in specific sites?
Thanks
There's a couple things you can do.
If you know the specific sites you want to search, you can add them as refinements to your engine. Then query for that refinement by adding 'more:<REFINEMENT_LABEL>' to the query.
Or, add 'site:' operators to the query itself. For example cats site:cnn.com OR site:bbc.com

Make google custom search location-aware

When I search for "football images" on google.co.uk, it knows that I mean the sport that elsewhere might be called "soccer". If I do the same search on google.com, I get American Football.
I'm using the custom search API - how can I tell it that I'm in the UK and would like results relevant to here?
You can limit your engine to operate on sites from a particular country via "cr" param, e.g. in Custom Element it looks like this:
<gcse:search cr="gb"></gcse:search>
Google knows some synonyms on the web, but if your particular use case is not correctly recognized you can add it in Control Panel in Search Features > Synonyms
More on synonyms:
Sorry, I ended up answering this myself. I couldn't get any of the instructions under the custom search API itself to work (although the answer offered above was also mentioned there, but this just made my CSE Context XML apparently invalid), but you can make requests to a custom search engine by using the instructions here https://developers.google.com/custom-search/json-api/v1/using_rest and an API key.
This is how I did it;
<gcse:search cr="countryUK"></gcse:search>
You can even return results in a specific language, code shown below returns results only in french;
<gcse:search lr = "lang_fr"></gcse:search>
This is the reference for Google Custom Search Element Control API: https://developers.google.com/custom-search/docs/element?hl=en
Try to set the 'gl' parameter to the country you want.
For details, look into CSE:list

How to restrict Google Custom Search to specific url prefixes?

If I go to the Google CSE control panel (https://www.google.com/cse/all) I see a list of my custom search engines.
When I click on one I can see in the list the option "sites to search". There I can list
example.com/cool-path
example.com/awesome-path
etc
How do I use the API to do the same? To add multiple domains, sites, or paths to the search? I can't find any documentation specifying this behavior.
The API will connect to a search engine that is specified on your custom search panel.
So you create the search engines you need (each with it's own list of sites to search) and then you connect to whichever one you need using the API using the engine's unique code

Multiple file types search using Google Custom Search API

I need to get Google search results for particular filetypes.
For example, in browser I would directly google search for "hyperloop filetype:pdf" and it will list out PDF files for "Hyperloop".
For this, my Google Custom Search request URI will be https://www.googleapis.com/customsearch/v1?key=MY_KEY&cx=MY_UNIQUE_ID&q=hyperloop&fileType=pdf
However, currently I would like to get search results for "hyperloop" of filetypes .ppt or .doc.
In browser, I would achieve this by googling "hyperloop filetype:ppt OR filetype:doc".
What will be my Search request URI equivalent for this query?
I could not find anything related to querying using multiple values for a single parameter in Google Custom Search Documentation.
Rather than doing
q=hyerloop&filetype=pdf
you can use
q=hyperloop%20filetype:pdf%20OR%20filetype:doc
use this its work
$url='https://www.googleapis.com/customsearch/v1?key=AIzaSyCJUGIb_tevRKD-Kxxi5f4&cx=010407088:onjj7gscy2g&q='. urlencode($keywords).'&filetype=doc&filetype=docx';
for me

Google Custom Search Query Parameters and Category Parameters

So I'm trying to set the query parameters and category parameters for google custom search. Here is an example search results page url:
http://www.whiterootmedia.com/search_results/index.html?q=my%20search%20phrase
So it looks like the default query parameter is q so I put q in the box when setting up the analytics portion for my google custom search engine. Is this ok? I read it wasn't ok to use q but to instead use query? And why does it matter?
Also should I set a category parameter that looks to be optional.
Thanks,
Dusty
The Google Search API just uses q. AFAIK the API doesn't have a query parameter. Check out the custom search API documentation.
Not sure what you mean by the 'category' parameter. Haven't come across that one before.
HTML:
<gcse:search gname='google1'></gcse:search>
Javascript:
window.google.search.cse.element.getElement("google1").execute("hello world");
But you need to wait for the Google script to load before launching the javascript.
If you use searchresults-only instead of search, you also need to enable the correct layout in the CSE Control Pannel: https://www.google.com/cse/
You can hit the following: https://cse.google.com/cse?as_q=your_query_string%20must_be_urlencoded&client=google-csbe&cx=full_cse_id_goes_here:including_the_bit_after_the_colon
So a direct answer to your question - the query parameter key is 'as_q'.