Why use Bing for your site?
The Bing API does not cost you a single penny to use. It’s completely free! Bing’s biggest competitor, Google Site Search on the other hand can cost you from $100 annually to a price point only known by calling a google sales representative. Looking at the their small business plan, it will cost $250 annually for 1,001 – 5000 indexed pages. If your site has a lot of pages, it will start feeling very limited. Using the Bing API, you have access to the same results as the Bing search engine and not a small subset. The one limitation to the API is that you can only request the first 1000 results of a query. If your site displays 20 results per page, that is a total of 50 pages of search results.
Bing also provides unlimited bandwidth for queries as long as you make less than 7 queries per second per IP address. In a single day, you can query 518,400 times per server. That is a magnitude of search queries that will satisfy even large web applications. Compare this with the $750 annual google plan that only allows 500,000 queries per year!
Finally, the another great benefit of using the Bing API is that you can style the results to match the look and feel of your site.
- It is to only be used for display in your Website or application.
- You must display some form of attribution to Bing compliant with the Microsoft branding rules.
- Do not filter any results you request.
- If you interleave with other sources, you must clearly differentiate those sources.
Bing in Ruby via Binged
Binged is a ruby gem I wrote that wraps the Bing API. It allows ruby developers to work with bing using all the common ruby syntax and idioms they know. Binged also has a chainable query syntax to allow easy query construction based on a large set of available filters.
[sudo] gem install binged
Get Your Bing API key
To use binged, you will require a Bing API key. Create one at http://www.bing.com/developers/createapp.aspx.
Instantiate a Client
binged = Binged::Client.new(:api_key => 'binged')
The client is used to search via different sources and holds a reference to your api key to simplify calls to the API.
Ruby on Rails configuration
Binged allows for configuration to be done once using a configure block. To use binged in your Ruby on Rails project, configure it globally in an initializer like this:
# config/initializers/binged.rb Binged.configure do |config| config.api_key = 'api_key' end
Globally configuring Binged removes the need to provide the client with any credentials or options in your code as they would have already been set. For example, defining a client would be as follows:
binged = Binged::Client.new
Web Search Example
The following example demonstrates how to search within a specific site for a keyword and provide a maximum of 30 results:
Binged::Client.new.web('ruby').from_site('adventuresincoding.com').per_page(30).each do |result| puts result.title puts result.description puts result.url end
For simple site-wide search, Bing is the optimal solution for most web applications. It is free and the usage limits are so lax, they can almost be thought of as unlimited. Be sure to check out the Binged documentation to get a grasp of all the conditions you can specify in your queries. If you find a bug or have a great feature idea, create an issue over at GitHub.
View the Binged screencast on how to integrate Binged with your Ruby on Rails application.