Google Custom Search Connector
|
The Google Custom Search Connector allows you to connect to Google Custom Search, is a platform provided by Google that allows web developers to feature specialized information in web searches, refine and categorize queries and create customized search engines, based on Google's core search technology.
The connector uses the Google Custom Search REST API version 1.1 to connect to Google Custom Search, search and filter text and images.
The following sections describe how to perform various operation with the connector.For general information on using connectors and their operations in your ESB configurations, see Using a Connector. To download the connector, go to https://github.com/wso2/esb-connectors, and then add and enable the connector in your ESB instance.
|
Connecting to Google Custom Search
To use the Google Custom Search connector, add the <googlecustomsearch.init> element in your configuration before any Google Custom Search operations.
init
|
<googlecustomsearch.init>
<apiKey>xxx</apiKey>
<cseID>xxx</cseID>
<url>https://www.googleapis.com/customsearch/v1</url>
</googlecustomsearch.init>
|
Get your API key
|
- First you have to create a google account. To do that, go to developer console : http://developers.google.com/console and create a project.
- After you have to enable APIs for your project. For this scenario, we have to enable Custom Search API.
- Then go to the credential tab, you can generate new API key for public access under Public API access. eg: AIzaSyAuTD-bYH-bqB4F3t0xWU2xzAY9Gtm8h2s
Get your Custom Search Engine ID
|
- Go to your custom search engine page https://www.google.com/cse/all, and create a custom search engine.
- you have to provide the url of the sites you want to search. If you want to search www.bbc.co.uk, then you have to provide it. You can add multiple sites also.
- Then go to control panel, then you can find search engine ID. like 08014526084702462680:cgdgefud8h
Search
|
If you are going to search about anything, You have to provide three mandatory parameters such as
apikey - API key,
cseID - custom search engine ID and
query - query string.
|
search
|
<googlecustomsearch.search>
<query>fifa</query>
</googlecustomsearch.search>
|
Sample Output
search
|
{
kind: "customsearch#search"
url: { … }
queries: {
nextPage: [ … ]
request: [
{
title: "Google Custom Search - fifa"
totalResults: "356000"
searchTerms: "fifa"
count: 10
startIndex: 1
inputEncoding: "utf8"
outputEncoding: "utf8"
safe: "off"
cx: "014869045608377880101:18pywcgrwls"
}
]
}
|
Search using Linked Custom Search Engine reference
|
You can use URL of a linked custom search engine specification for search request. In a Linked CSE the specification of the search engine is hosted on your website. In a Google Stored CSE the specification is stored at Google.
To use that , you have to create a custom search engine specification like this:
Then you can use cref instead of cscID
apikey - API key,
cref - linked custom search reference.
query - query string.
|
For this scenario, the init will be like this:
init
|
<googlecustomsearch.init>
<apiKey>xxx</apiKey>
<cref>http://www.guha.com/cref_cse.xml</cref>
<url>https://www.googleapis.com/customsearch/v1</url>
</googlecustomsearch.init>
|
Search with optional parameters
|
Here you can find optional parameter. These parameters gives a filtered results that you wanted.
For example if you want to search only images, you can search with the parameter called “searchType” and code it like
search with optional parameters
|
<googlecustomsearch.search>
<query>fifa</query>
<searchType>image</searchType>
. . . . . . . . . . . . . . . .
</googlecustomsearch.search>
|
Required parameters
Parameter name
|
Description
|
apiKey
|
The API key
|
cseID
|
Custom Search Engine ID
|
query
|
The search expression
|
Optional parameters
Parameter name
|
Description
|
alt
|
Data format for the response
|
cr
|
Restricts search results to documents originating in a particular country
|
dateRestrict
|
Restricts results to URLs based on date. Supported values include:
|
exactTerms
|
Identifies a phrase that all documents in the search results must contain
|
excludeTerms
|
Identifies a word or phrase that should not appear in any documents in the search results
|
filter
|
Controls turning on or off the duplicate content filter
Acceptable values are:
|
gl
|
Geolocation of end user
|
hl
|
Set the user interface language
|
hq
|
Appends the specified query terms to the query, as if they were combined with a logical AND operator
|
imgColorType
|
Returns black and white, grayscale, or color images: mono, gray and color
Acceptable values are:
|
imgDominantColor
|
Returns images of a specific dominant color
Acceptable values are:
|
imgSize
|
Returns images of a specified size
Acceptable values are:
|
imgType
|
Returns images of a type
Acceptable values are:
|
linkSite
|
Specifies that all search results should contain a link to a particular URL
|
lowRange
|
Specifies the starting value for a search range
|
lr
|
Restricts the search to documents written in a particular language (e.g., lr=lang_fr)
Acceptable values are:
|
num
|
Numbers of search results to return
|
orTerms
|
Provides additional search terms to check for in a document, where each document in the search results must contain at least one of the additional search terms
|
relatedSite
|
Specifies that all search results should be pages that are related to the specified URL
|
rights
|
Filters based on licensing. Supported values include: cc_publicdomain, cc_attribute, cc_sharealike, cc_noncommercial, cc_nonderived and combinations of these
|
safe
|
Search safety level
Acceptable values are:
|
searchType
|
Specifies the search type: image. If unspecified, results are limited to web pages
Acceptable values are:
|
siteSearch
|
Specifies all search results should be pages from a given site
|
siteSearchFilter
|
Controls whether to include or exclude results from the site named in the siteSearch parameter
Acceptable values are:
|
sort
|
The sort expression to apply to the results
|
start
|
The index of the first result to return
|
In creating your own Google Custom Search Engine you can prioritize or restrict search results based on specific Web sites and pages you specify.
Google Custom Search enables you to create a search engine for your website, your blog, or a collection of websites. You can configure your engine to search both web pages and images. You can fine-tune the ranking, add your own promotions and customize the look and feel of the search results.
Error Codes
|
If you didn’t provide required parameters, such as apiKey, cscID or query, This error message will appear
"domain": "usageLimits",
"reason": "keyInvalid",
"message": "Bad Request"
}
],
"code": 400,
|
If you give wrong parameter, you will get these type of error message. In most of the cases, if you give wrong parameters, it will skip that.
"domain": "global",
"reason": "invalidParameter",
"message": "Invalid string value: 'video'. Allowed values: [image]",
"locationType": "parameter",
"location": "searchType"
}
],
"code": 400,
|
And, if your daily search exceeded for a free account, This error message will appear
"domain": "usageLimits",
"reason": "dailyLimitExceeded",
"message": "Daily Limit Exceeded"
}
],
"code": 403,
|
Note: Google Custom Search API provides only 100 queries for free per day. If you want to make queries more than 100 times, you have to pay for it, or create another API key and use it.
No comments:
Post a Comment