Friday, July 4, 2014

WSO2 ESB Connector - Google Custom Search


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
  • Search queries and getting results
  • Search queries with optional parameters





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


  • 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


  • 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
  • Valid values: json, atom
  • Default value: json
cr
Restricts search results to documents originating in a particular country
dateRestrict
Restricts results to URLs based on date. Supported values include:
  • d[number]: requests results from the specified number of past days
  • w[number]: requests results from the specified number of past weeks
  • m[number]: requests results from the specified number of past months
  • y[number]: requests results from the specified number of past years
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:
  • "0": Turns off duplicate content filter
  • "1": Turns on duplicate content filter
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:
  • "color": color
  • "gray": gray
  • "mono": mono
imgDominantColor
Returns images of a specific dominant color
Acceptable values are:
  • "black": black
  • "blue": blue
  • "brown": brown
  • "gray": gray
  • "green": green
  • "pink": pink
  • "purple": purple
  • "teal": teal
  • "white": white
  • "yellow": yellow
imgSize
Returns images of a specified size
Acceptable values are:
  • "huge": huge
  • "icon": icon
  • "large": large
  • "medium": medium
  • "small": small
  • "xlarge": xlarge
  • "xxlarge": xxlarge
imgType
Returns images of a type
Acceptable values are:
  • "clipart": clipart
  • "face": face
  • "lineart": lineart
  • "news": news
  • "photo": photo
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:
  • "lang_ar": Arabic
  • "lang_bg": Bulgarian
  • "lang_ca": Catalan
  • "lang_cs": Czech
  • "lang_da": Danish
  • "lang_de": German
  • "lang_el": Greek
  • "lang_en": English
  • "lang_es": Spanish
  • "lang_et": Estonian
  • "lang_fi": Finnish
  • "lang_fr": French
  • "lang_hr": Croatian
  • "lang_hu": Hungarian
  • "lang_id": Indonesian
  • "lang_is": Icelandic
  • "lang_it": Italian
  • "lang_iw": Hebrew
  • "lang_ja": Japanese
  • "lang_ko": Korean
  • "lang_lt": Lithuanian
  • "lang_lv": Latvian
  • "lang_nl": Dutch
  • "lang_no": Norwegian
  • "lang_pl": Polish
  • "lang_pt": Portuguese
  • "lang_ro": Romanian
  • "lang_ru": Russian
  • "lang_sk": Slovak
  • "lang_sl": Slovenian
  • "lang_sr": Serbian
  • "lang_sv": Swedish
  • "lang_tr": Turkish
num
Numbers of search results to return
  • Valid values are integers between 1 and 10, inclusive
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:
  • "high": Enables highest level of SafeSearch filtering.
  • "medium": Enables moderate SafeSearch filtering.
  • "off": Disables SafeSearch filtering. (default)
searchType
Specifies the search type: image.  If unspecified, results are limited to web pages
Acceptable values are:
  • "image": custom image search
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:
  • "e": exclude
  • "i": include
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