Google Custom Search – Restricted to specific directory, and filetype…

So I’ve been having trouble getting a new Google Custom Search to look only in a specific directory on my web server, as well as to only look at specific file types.   It seemed like this should have been SO easy…  But, it took me a bit to figure it out.  So here’s the information just in case anyone else needs it.

  1. Set up a new search engine, in Google Custom Search.
  2. Make sure that you have the root directory in the “Sites” section.  I used “www.mydomain.com”.
  3. You can enter refinements, but they only seem to work if the user clicks on them; not what I wanted.
  4. Go down to “Get Code” and grab the code block that is created for you.  For me it was this:
    • <div id="cse" style="width: 100%;">Loading</div>
        <script src="http://www.google.com/jsapi" type="text/javascript"></script>
        <script type="text/javascript">
          google.load('search', '1', {language : 'en', style : google.loader.themes.MINIMALIST});
          google.setOnLoadCallback(function() {
            var customSearchOptions = {};
            var customSearchControl = new google.search.CustomSearchControl(
              'Custom Search ID', customSearchOptions);
            customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
            customSearchControl.draw('cse');
          }, true);
      </script>
  5. This will give you the basic search.  In order to add in the site restriction and the filetype restriction I changed it to:
    • <div id="cse" style="width: 100%;">Loading</div>
        <script src="http://www.google.com/jsapi" type="text/javascript"></script>
      <script type="text/javascript">
      google.load('search', '1', {language : 'en', style : google.loader.themes.MINIMALIST});
      google.setOnLoadCallback(function(){
      var customSearchOptions ={};
      /* Add Custom Search Option to restrict directory */
      customSearchOptions [google.search.Search.RESTRICT_EXTENDED_ARGS]={"as_sitesearch": "www.myDomain.com/subDirectory1/subDirectory2/"};
      var customSearchControl = new google.search.CustomSearchControl("Custom Search ID", customSearchOptions );
      customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
      customSearchControl.draw('cse');
      /* Add query addition to restrict filetype */
      customSearchControl.setSearchStartingCallback(
      this,
      function(control, searcher, query) {
      searcher.setQueryAddition("filetype:pdf OR filetype:PDF");
      }
      );
      }, true);
        </script>

    I hope that helps out anyone else who might be looking for a similar problem…

Advertisements

4 Responses to “Google Custom Search – Restricted to specific directory, and filetype…”

  1. Thank you thank you thank you! You’re the only place on the web that says how to do this (Google’s API doesn’t explain it).
    Also, if anyone needs to open a particular refinement on page load, do this…
    var customSearchOptions = {‘defaultToRefinement’:’products’};

  2. Great advice! Thanks.
    What about using variables in searcher.setQueryAddition? I would like to use query as “intitle” value. Like this:

    searcher.setQueryAddition(“intitle:[QUERY]”);

    Is it possible?

  3. always i used to read smaller content that as well clear their motive, and that is also happening with this post which I am reading at this place.

  4. I do not even know how I ended up here, but I thought this post was great.
    I don’t know who you are but definitely you’re going to
    a famous blogger if you aren’t already 😉 Cheers!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: