Google Ajax Search APIで検索結果数を取得(したけどかなりあやしい)
bonlifeです。せっかくの三連休なので、いつもと違うことをやってみよう!ということでGoogle Ajax Search APIを使ってみました。興味ある方は「AJAX Search API キーへの登録 」をした後で、試してみてくださいませ。 (下記のソースの YOUR_API_KEY_HERE の部分を置き換えてくださいな。)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> <title>estimatedResultCountが非常にあやしい</title> <style type="text/css"> body { background-color: white; color: black; font-family: Arial, sans-serif; font-size : 13px; margin: 15px; } #searchcontrol .gsc-control { width : 400px; } </style> <script src="http://www.google.com/jsapi?YOUR_API_KEY_HERE" type="text/javascript"></script> <script type="text/javascript"> //<![CDATA[ google.load('search', '1.0', {"language" : "ja"}); function OnLoad() { var searchControl = new google.search.SearchControl(); searchControl.addSearcher(new google.search.WebSearch()); searchControl.setSearchCompleteCallback(searchControl,showEstResults); searchControl.setNoResultsString("そんなもんない!"); searchControl.draw(document.getElementById("searchcontrol")); searchControl.execute("bonlife"); } function showEstResults(seachControl,searcher){ if (searcher.cursor == undefined) { document.getElementById("resultcount").innerHTML = 0; } else { document.getElementById("resultcount").innerHTML = searcher.cursor.estimatedResultCount; } } google.setOnLoadCallback(OnLoad, true); //]]> </script> </head> <body> <div id="searchcontrol">Loading</div> <div id="resultcount">Loading</div> </body> </html>
これやってみると分かりますが、普通にGoogleで検索した場合と検索結果の件数がおそろしく違う!
.estimatedResultCount - 現在のクエリと一致する結果の数の推定値を指定します。この値は Google.com 検索プロパティに表示される類似の値と必ずしも一致しないことに注意してください。
こう書いてあるけど、それでも…。どっちの値があやしいのか分かりませんが、全く違ってイヤになっちゃうワン。というダメ出しは置いておいて、かなりお手軽にリッチなUIの検索窓を置けますね。ビックリ。