作为 Google 创建索引过程的有机组成部分,Google 要检索被建立索引的页面副本,在搜索结果中为用户提供到缓存页面的链接。主要的缺点是用户看不到更新后的内容,但一些优点也是存在的。
检索缓存页面通常要比检索真实页面要快些,这归功于 Google 的带宽和处理能力,而且如果出于某种原因没有实际的服务器,仍能检索缓存页面。可最重要的还是搜索项在缓存页面中突出显示,更容易看出页面的相关程度。
Google API 使您有可能检索缓存页面的文本:
import com.google.soap.search.GoogleSearch;
import com.google.soap.search.GoogleSearchFault;
public class GoogleCacheTutorial {
public static void main (String[] args) {
try {
GoogleSearch search = new GoogleSearch();
search.setKey("00000000000000000000000000000000");
byte[] pageText = search.doGetCachedPage( "http://www-106.ibm.com/developerworks/xml/library/x-tiphdln.html"); System.out.println(new String(pageText));
} catch (GoogleSearchFault gsf) {
System.out.println("Google Search Fault: "+gsf.getMessage());
}
}
}
上面使用的不是 doSearch() 而是 doGetCachedPage()。结果为 byte 数组,您可以和其它任何 Java byte 数组一样使用。
Google 搜索引擎意识到用户通常并不知道如何拼写要找的主题,所以内置一个拼写检查器,它可以分析查询、提出建议。不论什么样的搜索,都会有这样的拼写建议。
import com.google.soap.search.GoogleSearch;
import com.google.soap.search.GoogleSearchFault;
public class GoogleSpellingTutorial {
public static void main (String[] args) {
String spellingRequest = args0];
try {
GoogleSearch search = new GoogleSearch();
search.setKey("00000000000000000000000000000000");
String suggestion = search.doSpellingSuggestion(spellingRequest);
if (suggestion == null) {
System.out.println("There is no suggestion in the database.");
} else {
System.out.println(suggestion);
}
} catch (GoogleSearchFault gsf) {
System.out.println("Google Search Fault: "+gsf.getMessage());
}
}
}
可以检查任何词或词组。如果没有拼写错误,或者如果引擎根本不认识某个单词,那么它将返回 null。不然的话,就会返回所建议的词或词组。