Force verbatim mode in Google Custom Search - google-custom-search

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!

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...

Related

Flowable - concat dynamic variable and a string in http task (url)

I need to convert the base url according to the production and other environments.
I am using script task before a http task to perform this logic.
baseUrl = http://localhost:8080
baseUrl, is the output of the script task. Now I need to add this base url as a prefix in http task url
Url = ${baseUrl}/application/find (something like this).
I am getting the following issue
Unknown Property used in the expression ${baseUrl}/application/find
Script
var env = execution.getVariable("env")
if(env == "prod") {
var baseUrl = "http://localhost:8080";
execution.setVariable("baseUrl", baseUrl);
}
Please assist.
This typically means that it is unable to find a property in the expression (as the message says). The only expression you are using is baseUrl which means that the issue is around the baseUrl. The concatenation as you have done it is correct and doesn't need to have an adaption.
You should check if the variable really exists, this you can do by introducing a wait state before your HTTP task and check afterwards if the variable is created. Rather than using outputs, you can also use the Java API in your script task to create the variable:
execution.setVariable("baseUrl", "http://localhost:8080");
Assuming you are using Spring Boot, for your specific use-case it would be also an option to use the application.properties to specify your base-url and then refer to the baseUrl with the following expression:
${environment.getProperty("baseUrl")}/application/find
This will allow you to change the baseUrl independent of your process definition.

Could I get some help on changing auth tokens mid test? [duplicate]

In my framework I have headers.js file. I am calling that js file on background of every feature file using the command * configure headers = read('headers.js'). This working as expected, some scenarios I need to change the 'client-id' value. For example headers.js has a client-id=xyz, I need to change client-id=abc for couple of scenarios rest all are using client-id as xyz (my headers have 20 different values, I don't want to write that in require feature files) Is there any way to modify one value from the headers.js file and use that as a header for the request?
The configured headers always is the last to be applied. So the best solution for you is to create a second headers-xyz.js and for only those needed scenarios do * configure headers = read('headers-xyz.js').
It is up to you to refactor the js code so that you can re-use most of it. For example, this might work:
function() {
var fun = karate.read('headers.js');
var res = fun();
res['client-id'] = 'xyz';
return res;
}

Cro::WebSocket::Client doesn't work

Created a websocket server with "cro sub".
Wrote this client:
use v6;
use Cro::WebSocket::Client;
constant WS-PORT = '20000';
constant WS-ADDRESS = 'localhost';
constant WS-PATH = 'chat';
constant WS-URL = 'ws://' ~ WS-ADDRESS ~ ':' ~ WS-PORT ~ '/' ~ WS-PATH;
constant TIMEOUT-TO-CONNECT = 5; # seconds
my $timeout;
my $connection-attempt;
await Promise.anyof(
$connection-attempt = Cro::WebSocket::Client.connect(WS-URL),
$timeout = Promise.in(TIMEOUT-TO-CONNECT));
if $timeout.status == Kept
{
say "* could not connect to server in ', TIMEOUT-TO-CONNECT, ' seconds";
exit 1;
}
if $connection-attempt.status != Kept
{
say "* error ", $connection-attempt.cause,
" when trying to connect to server";
exit 1;
}
my $connection = $connection-attempt.result;
my $peer = WS-ADDRESS ~ ':' ~ WS-PORT;
say '* connected with ', $peer;
my $counter = 0;
my $message-supplier = Supplier::Preserving.new;
my $has-message-to-send = $message-supplier.Supply;
$message-supplier.emit(1);
react
{
whenever $has-message-to-send
{
$counter++;
$connection.send($counter);
say "* ok, sent message ", $counter, " to server";
}
whenever $connection.messages -> $reply
{
say '* received reply=[' ~ $reply ~ '] from server';
$message-supplier.emit(1);
}
} # react
I see with tcpdump the response code 101 (switching protocols) from the server, but I don't see the message sent from the client to the server.
So, what am I doing wrong ?
Another question, shoudn't "$connection.send" return a Promise or something ? What if there's an error when sending ?
And another question: it seems the server only understands IPV6 addresses...how to make it understand IPV4 addresses ?
That's it, for now.
UPDATE
As per Takao's advice, changing
$connection.send($counter)
to
$connection.send($counter.Str)
solves the problem (though I tried it on another program, not this one).
Let's resolve this piece by piece.
Firstly, your code looks correct to me, except for a couple of tiny bits.
When I reproduced your code, it indeed did not work, so I tried it with cro trace . instead of cro run .. You can find info about that mode in official docs.
The alternative way is to just set CRO_TRACE=1 environment variable.
So during debug I saw this error:
[TRACE(anon 1)] Cro::HTTP::ResponseParser QUIT No applicable body serializer could be found for this message
As it says, the body you sent could not be serialized. So I looked into what are you sending: $counter. $counter in your code is Int, so we need to make it Str before, doing simple $counter.Str makes your example work.
Also, note that you are sending a message on every reply, and echo server (default one you created using cro stub) also sends a reply for every incoming message, so your example sends messages endlessly. To prevent that you may consider adding a condition under which you will no longer send things, but well, it is a test example anyway, so up to you.
As for your other questions:
Another question, shoudn't "$connection.send" return a Promise or something?
It should not, I'll write out some cro's architecture details to explain it next. As you may know from reading docs, cro pipeline is basically just a bunch of Cro::Transform-wrapped supplies. Inside of Cro::Websocket::Client::Connection, send method just sends a thing directly into Cro::Source of the whole pipeline, you cannot go wrong with a simple $supplier.emit($message)(the real implementation of this method looks very close to this line). The thing you bumped into occurred further in the pipeline. I am sure it is not a nice user experience to hide exceptions of such cases, so I'll consider making a patch to propagate the exception, so it'd be easier to catch(although you always can use debug mode).
it seems the server only understands IPV6 addresses...how to make it understand IPV4 addresses ?
I am not sure about that, please open a new question.

How to edit configured headers in karate framework

In my framework I have headers.js file. I am calling that js file on background of every feature file using the command * configure headers = read('headers.js'). This working as expected, some scenarios I need to change the 'client-id' value. For example headers.js has a client-id=xyz, I need to change client-id=abc for couple of scenarios rest all are using client-id as xyz (my headers have 20 different values, I don't want to write that in require feature files) Is there any way to modify one value from the headers.js file and use that as a header for the request?
The configured headers always is the last to be applied. So the best solution for you is to create a second headers-xyz.js and for only those needed scenarios do * configure headers = read('headers-xyz.js').
It is up to you to refactor the js code so that you can re-use most of it. For example, this might work:
function() {
var fun = karate.read('headers.js');
var res = fun();
res['client-id'] = 'xyz';
return res;
}

Sphinx enable STP_MATCH_EXTENDED for PDO connection

If my PDO connection is as below:
$sphinx = new PDO('mysql:dbname=db;host=127.0.0.1;port=9306;charset=utf8', 'user', 'pass');
$sphinx->exec('SET CHARACTER SET utf8');
$sphinx->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
$sphinx->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
And my query is:
$array = $sphinx->prepare("select * from `indexname` where MATCH #(title,body) hello world");
What is the correct way to enable SPH_MATCH_EXTENDED?
If I add:
require ("sphinxapi.php");
$sphinx = new SphinxClient();
$sphinx->SetMatchMode(SPH_MATCH_EXTENDED);
$sphinx->SetArrayResult(true);
Under the PDO connection, is this the correct way to enable stp_match_extended for PDO?
What is the correct way to enable SPH_MATCH_EXTENDED?
SphinxQL uses Extended Match Mode by default.
Don't need to do anything to use it.
You can change to use a different match mode if really want, but its NOT recommended. Internally Sphinx only (now!) implements Extended match mode, the others are all emulated.