Google 自定义搜索 - 查询 database/API?
Google Custom Search - Query database/API?
我们有兴趣在我们的项目中使用 Google 自定义搜索/Google,主要是因为它在变位和纠正拼写错误方面非常出色。
我们知道它可以 return JSON 或 XML 中的数据,我们对此没有意见。但找到问题的答案:
我们可以使用那个变位和纠错来搜索我们自己的 database/api 吗?
如果您键入 drnks with no alcohol
,它会自动更正为 drinks with no alcohol
,然后像这样搜索我们的数据库:
http://example.com?search=drinks&alcohol=0,它可以这样响应:
{
"coke": {
"alcohol": 0,
"calories": 300,
"taste": "awesome"
},
"pepsi": {
"alcohol": 0,
"calories": 300,
"taste": "meh"
}
}
然后它将以某种形式 return 这两个结果。
使用付费版本的解决方案就可以了。
如果可以的话,你能给我一个简单的例子吗?
Google为他们的自定义搜索提供了一个REST API,您可以从您的服务器查询它以确定搜索词是否有更好的拼写,然后使用它来查询您的内部数据库。
在我的代码中,我使用了 Guzzle,这是一个 REST 客户端库,可以避免 cURL 丑陋和冗长的代码,但如果确实需要,请随意使用 cURL。
// Composer's autoloader to load the REST client library
require "vendor/autoload.php";
$api_key = "..."; // Google API key, looks like random text
$search_engine = "..."; // search engine ID, looks like "<numbers>:<text>"
$query = "drnks with no alcohol"; // the original search query
// REST client object with some defaults
// avoids specifying them each time we make a request
$client = new GuzzleHttp\Client(["base_url" => "https://www.googleapis.com", "defaults" => ["query" => ["key" => $api_key, "cx" => $search_engine, "fields" => "spelling(correctedQuery)"]]]);
try {
// the actual request, with the search query
$resp = $client->get("/customsearch/v1", ["query" => ["q" => $query]])->json();
// whether Google suggests an alternative spelling
if (isset($resp["spelling"]["correctedQuery"])) {
$correctedQuery = $resp["spelling"]["correctedQuery"];
// now use that corrected spelling to query your internal DB
// or do anything else really, the query is yours now
echo $correctedQuery;
} else {
// Google doesn't have any corrections, use the original query then
echo "No corrections found";
}
} catch (GuzzleHttp\Exception\TransferException $e) {
// Something bad happened, log the exception but act as if
// nothing is wrong and process the user's original query
echo "Something bad happened";
}
这里有一些 instructions to obtain your API key, and the custom search engine ID can be obtained from the control panel。
如果你仔细看,你会发现我已经指定了 fields
查询参数来请求一个 partial response 只有最终的拼写建议,以(希望)获得更好的性能,因为我们没有不需要响应中的任何其他内容(但如果您确实需要完整的响应,请随时 change/remove 它)。
请注意,Google 不知道您的数据库中有什么,因此拼写更正将仅基于 public 数据 Google 关于您的网站,我不知道认为有一种方法可以让 Google 了解您的内部数据库,但这并不是一个好主意。
最后,确保优雅地处理速率限制和 API 失败,仍然让用户可以使用他们的原始查询进行搜索(就像没有发生任何错误一样,只记录错误以备后用)审查)。
我们有兴趣在我们的项目中使用 Google 自定义搜索/Google,主要是因为它在变位和纠正拼写错误方面非常出色。
我们知道它可以 return JSON 或 XML 中的数据,我们对此没有意见。但找到问题的答案:
我们可以使用那个变位和纠错来搜索我们自己的 database/api 吗?
如果您键入 drnks with no alcohol
,它会自动更正为 drinks with no alcohol
,然后像这样搜索我们的数据库:
http://example.com?search=drinks&alcohol=0,它可以这样响应:
{
"coke": {
"alcohol": 0,
"calories": 300,
"taste": "awesome"
},
"pepsi": {
"alcohol": 0,
"calories": 300,
"taste": "meh"
}
}
然后它将以某种形式 return 这两个结果。
使用付费版本的解决方案就可以了。
如果可以的话,你能给我一个简单的例子吗?
Google为他们的自定义搜索提供了一个REST API,您可以从您的服务器查询它以确定搜索词是否有更好的拼写,然后使用它来查询您的内部数据库。
在我的代码中,我使用了 Guzzle,这是一个 REST 客户端库,可以避免 cURL 丑陋和冗长的代码,但如果确实需要,请随意使用 cURL。
// Composer's autoloader to load the REST client library
require "vendor/autoload.php";
$api_key = "..."; // Google API key, looks like random text
$search_engine = "..."; // search engine ID, looks like "<numbers>:<text>"
$query = "drnks with no alcohol"; // the original search query
// REST client object with some defaults
// avoids specifying them each time we make a request
$client = new GuzzleHttp\Client(["base_url" => "https://www.googleapis.com", "defaults" => ["query" => ["key" => $api_key, "cx" => $search_engine, "fields" => "spelling(correctedQuery)"]]]);
try {
// the actual request, with the search query
$resp = $client->get("/customsearch/v1", ["query" => ["q" => $query]])->json();
// whether Google suggests an alternative spelling
if (isset($resp["spelling"]["correctedQuery"])) {
$correctedQuery = $resp["spelling"]["correctedQuery"];
// now use that corrected spelling to query your internal DB
// or do anything else really, the query is yours now
echo $correctedQuery;
} else {
// Google doesn't have any corrections, use the original query then
echo "No corrections found";
}
} catch (GuzzleHttp\Exception\TransferException $e) {
// Something bad happened, log the exception but act as if
// nothing is wrong and process the user's original query
echo "Something bad happened";
}
这里有一些 instructions to obtain your API key, and the custom search engine ID can be obtained from the control panel。
如果你仔细看,你会发现我已经指定了 fields
查询参数来请求一个 partial response 只有最终的拼写建议,以(希望)获得更好的性能,因为我们没有不需要响应中的任何其他内容(但如果您确实需要完整的响应,请随时 change/remove 它)。
请注意,Google 不知道您的数据库中有什么,因此拼写更正将仅基于 public 数据 Google 关于您的网站,我不知道认为有一种方法可以让 Google 了解您的内部数据库,但这并不是一个好主意。
最后,确保优雅地处理速率限制和 API 失败,仍然让用户可以使用他们的原始查询进行搜索(就像没有发生任何错误一样,只记录错误以备后用)审查)。