7

Is it possible to force Google Custom Search to use verbatim mode be default?

For the purpose I am using it, verbatim mode will produce the most relevant results but users may not know to put their search in quotes.

I have an idea I should be able to add the quotes with Javascript before the string is submitted to Google, but I can't work out how.

Any help would be much appreciated!

Will
  • 113
  • 5

1 Answers1

0

Your best bet is to use a PHP Proxy or something similar to edit the query in order to add %22 which transforms the query into "query" before firing off the request.

header('Content-type: application/json');

# Setup Base URL and array for Parameters
$host = 'https://www.googleapis.com/customsearch/v1?';
$queries = array();
$queries['cx'] = "CSEKey";
$queries['key'] = "YourAPIKey";

# Setup possible incoming params
if (isset($_GET['search_term'])) $queries['q'] = "%22"+ $_GET['search_term']+ "%22";
if (isset($_GET['result_count'])) $queries['result_count'] = $_GET['result_count'];
if (isset($_GET['callback'])) $queries['callback'] = $_GET['callback'];

# Build query and Final URL
$queriesURL = http_build_query($queries);
$finalURL = $host.$queriesURL;
/*
  DEBUG generated URL
  echo $finalURL;
*/

$response = file_get_contents($finalURL);

echo $response; 
?>

I believe that this technique forced Verbatim mode because the new JSON response lacked the usual weird suggested spelling of name:

 "spelling": {
  "correctedQuery": "Deo Vandski",
  "htmlCorrectedQuery": "\u003cb\u003e\u003ci\u003eDeo Vandski\u003c/i\u003e\u003c/b\u003e"
 },

I also saw something about appending &tbs=li:1, but I did not see any difference when I tried on my searches...

Deovandski
  • 770
  • 1
  • 8
  • 22