Searching Your Blog Got a Whole Lot Easier

Published 01 February 05 09:54 AM | Scott Mitchell

If you check out the ScottOnWriting.NET homepage you'll now find that in the upper-left hand corner there's a spiffy “Search” box. Go ahead, type a query in there... you'll be redirected to http://scottonwriting.net/Search.aspx?s=ScottOnWriting.NET&count=10&first=1&query=query, which will show you the results of your search.

The new search page - which you, too, can add to your site in a matter of minutes - is possible in large part thanks to Microsoft's new MSN Search. Today Microsoft unveiled their new “built from the ground up” search and one of the cool features it that the search results can be returned as RSS. (To try it out, go to the MSN Search page and enter a query; at the bottom of the Web page you'll find that little, friendly orange RSS button.) Once I found this, I realized adding search to ScottOnWriting.NET would be a cinch if MSN Search let me search for pages on a particular domain. Thankfully, you can search by a specific site by simply adding site:siteName syntax in the search query.

Creating a search engine for a local site, then, is as easy as having a page that requests the remote RSS file based on the query entered by a user, and displaying the resulting RSS in the page. With my free RssFeed control doing exactly this is a no-brainer, requiring just two lines of code. Therefore, the code and markup for the search page for ScottOnWriting.NET basically looks like:

Source Code
// Build up the request URL for the RSS data
string requestUrl = string.Format("
http://beta.search.msn.com/results.aspx?q={0}+site%3a{1}&format=rss&first={2}&count={3}", searchQuery, “http://ScottOnWriting.NET/“, startIndex, resultsPerPage);

// Bind the results to the RssFeed control
searchResults.DataSource = requestUrl;
searchResults.DataBind();

Markup
<skm:rssfeed width="80%" id="searchResults" runat="server" Font-Names="Verdana"
Font-Size="Medium" CellPadding="5" ShowHeader="False" GridLines="None">

<ItemTemplate>
<span class="title"><a href='<%# DataBinder.Eval(Container.DataItem, "Link") %>'><%# DataBinder.Eval(Container.DataItem, "Title") %></a></span><br />
<span class="desc"><%# HighlightSearchTerm(DataBinder.Eval(Container.DataItem, "Description").ToString()) %></span><br />
<span class="url"><%# DataBinder.Eval(Container.DataItem, "Link") %></span>
</ItemTemplate>

</skm:rssfeed>

Some example results can be seen here.

This is pretty cool, but there are some gotchas.

  1. The RSS feed doesn't provide any information if there's more results. There may be 15 results for the query DataGrid, but the RSS feed, by default, will only return the first 10. There's no way of knowing for sure if there are more results unless you grab another RSS feed, this time asking for records starting at 11. What complicates things is that if there are exactly 10 records, getting the RSS feed starting at 11 won't return nothing; rather it will re-return the first 10 records. Ick. My current solution is to just display a Next button if there are 10 records on the page... it's a hack, since it won't really do what is expected for queries that return a multiple of 10 records, but it's good enough.
  2. I don't know how legal all this is. If you look at the RSS feed's <copyright> tag it says:

    Copyright © 2005 Microsoft. All rights reserved. These XML results may not be used, reproduced or transmitted in any manner or for any purpose other than rendering MSN Search results within an RSS aggregator for your personal, non-commercial use. Any other use of these results requires express written permission from Microsoft Corporation. By accessing this web page or using these results in any manner whatsoever, you agree to be bound by the foregoing restrictions.

    I doubt MS is going to care too much if I use these results for my dinky little blog, but clearly they'd care if I was a company that used this approach to provide search on my Web site in lieu of buying Index Server or Google's searching server, or whatever.

Of course, MSN Search is not the first search engine to provide a programmatic means for accessing their results. Google offers the Google API, a set of Web services that can be used to run queries on Google. In fact, I've written an article on 4Guys on how to create a search engine with the Google API: Searching Google Using the Google Web Service. (And I will likely write up an article on using the MSN Search technique...) The downside of using the Google API is that users are limited to 1,000 queries per day and creating a page to work with the API takes a bit more heavy lifting than just bringing back RSS data and displaying it (although not much more).

A future blog entry, or 4Guys article, will delve into the MSN Search techniques I used to add search to ScottOnWriting.NET, but don't let that stop you from adding this feature to your site today. Armed with RssFeed you could do it without further information in a matter of minutes.

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Archives

My Books

  • Teach Yourself ASP.NET 4 in 24 Hours
  • Teach Yourself ASP.NET 3.5 in 24 Hours
  • Teach Yourself ASP.NET 2.0 in 24 Hours
  • ASP.NET Data Web Controls Kick Start
  • ASP.NET: Tips, Tutorials, and Code
  • Designing Active Server Pages
  • Teach Yourself Active Server Pages 3.0 in 21 Days

I am a Microsoft MVP for ASP.NET.

I am an ASPInsider.