Search code examples
google-custom-search

Google Custom Search - Add/remove sites to search dynamically


Google Custom Search has a feature to specify the sites you want the search engine to search - "Sites to search" feature. I have a requirement to add/remove these sites on the fly. Is there any api or any other way provided by Google with which I can achieve this?


Solution

  • Here you can find the relevant information:

    https://developers.google.com/custom-search/docs/tutorial/creatingcse

    To create a custom search engine:

    Sign into Control Panel using your Google Account (get an account if you don't have one). In the Sites to search section, add the pages you want to include in your search engine. You can include any sites you want, not just the sites you own. You can include whole site URLs or individual pages URLs. You can also use URL patterns.

    https://support.google.com/customsearch/answer/71826?hl=en

    URL patterns

    URL patterns are used to specify what pages you want included in your custom search engine. When you use the control panel or the Google Marker to add sites, you're generating URL patterns. Most URL patterns are very simple and simply specify a whole site. However, by using more advanced patterns, you can more precisely pick out portions of sites.

    For example, the pattern 'www.foo.com/bar' will only match the single page 'www.foo.com/bar'. To cover all the pages where the URL starts with ' www.foo.com/bar', you must explicitly add a '' at the end. In the form-based interfaces for adding sites, 'foo.com' defaults to '.foo.com/*'. If this is not what you want, you can change it back in the control panel. No such defaulting occurs for patterns that you upload. Also note that URLs are case sensitive - if your site URLs include capital letters, you'll need to make sure your patterns do as well.

    In addition, the use of wildcards in URL patterns allows you to include or exclude multiple pages or portions of a site all at once.

    So, basically you've to navigate to the "Sites to search section" and enter the needed sites there. If you want to change these site on the fly, you've to manipulate your URL pattern.

    There's also an option to use the XML configuration files. You just have to add (or remove) your sites there:

    https://developers.google.com/custom-search/docs/annotations

    Annotations: The annotations XML file lists the webpages or websites you want your search engine to cover, and indicates any preferences you have about how these sites should be ranked in your search results. Each site and its associated information is called an annotation. More information about the annotations XML file.

    An example for an annotation:

    <Annotation about="http://www.solarenergy.org/*">
       <Label name="_cse_abcdefghijk"/>
    </Annotation>