How do I submit a JSON to SOLR using Zend_Client?
Assume the JSON I am using is (It was taken from the SOLR WIKI, so I assume it is right).
$JSON ='[{"id" : "3", "title" : "test3","description":"toottoto totot ototot "}]';
I see no error in the solr error log, this is the code I use to submit
DOES NOT WORK
$url = 'http://localhost:8983/solr/update/json';
$Client = new Zend_Http_Client($url);
$Client->resetParameters();
$Client->setMethod(Zend_Http_Client::POST);
$Client->setHeaders('Content-type','application/json');
$Client->setParameterPost($JSON);//***** WRONG *****
$Client->setRawData($JSON); //* **** RIGHT FROM ANSWER BELOW, STILL NEED TO ENCODE IT!
$response = $Client->request();
THIS WORKS FROM THE COMMAND LINE!
sudo curl http://localhost:8983/solr/update/json -H 'Content-type:application/json' -d '
[{"id" : "3", "title" : "test3","description":"toottoto totot ototot "}]'
The setParameterPost() method takes two arguments, the parameter name and its value like this:
$client->setParameterPost('name', 'john'); // results in name=john
Try using setRawData() instead, this will let you set raw post data.
Related
This might be sort of a noob question, but I am using the import org.apache.commons.cli.*; to set up my command line parser application.
This is how I am setting up my option.
Option n = Option.builder().hasArg(true).option("n").build();
options.addOption(n);
In one of our scenarios, we have a particular case where the input looks something like this "-n", "2", "3". Now this is not a valid scenario, because n should fail if you provide more than one value, without the "-n" flag.
Invalid: "-n", "2", "3"
Valid: "-n", "2", "-n", "3"
I was able to get the valid scenario working, but I am unable to get the invalid scenario working. because when I use getOptionValues(), I only get back 2 and not 3. Does anyone know how I can grab 3 too, and fail the invalid scenario.
Thanks.
You can use CommandLine.getArgList() to check for such "trailing" arguments,
CommandLine cmd = parser.parse(options, new String[] {"-n", "2", "3"});
assertTrue(cmd.hasOption(n));
assertEquals("[3]", cmd.getArgList().toString());
So you can fail parsing if cmd.getArgList() is not empty to catch such cases.
I'm using the Groovy CLiBuilder with the following option :
cli.with {
b longOpt: 'build', args:4, valueSeparator: ",", argName: 'a1,a2,a3,a4', 'This is the description', required: false
}
This works well, I can correctly parse the 4 arguments with the following:
def _a1 = options.bs[0]
def _a2 = options.bs[1]
def _a3 = options.bs[2]
def _a4 = options.bs[3]
So far so good .... BUT, if one of my argument's value contains a space ... then it doesn't work anymore. When I do the following call from bash:
myScript.sh -b value_a1,value_a2,value a 3,value a 4
I end up with following String Array for my values :
[valu_a1, value_a2, value, --]
But it perfectly works if none of my argument's value contain spaces.
Did anyyone already encoutner this behavior?
You have to call it using quotes: -b "value_a1,value_a2,value a 3,value a 4"
That is how the shell works and not a problem of CliBuilder
I am trying to install java using powershell Invoke-WebRequest command on packer instance however, I am getting below error;
Cannot index into a null array.
At line:1 char:94
+ ... content | %{[regex]::matches($_, '(?:<a title="Download Java software ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
Command;
[Net.ServicePointManager]::SecurityProtocol = "tls12"
$URL = (Invoke-WebRequest -UseBasicParsing https://www.java.com/en/download/manual.jsp).Content | %{[regex]::matches($_, '(?:<a title="Download Java software for Windows .64-bit." href=")(.*)(?:">)').Groups[1].Value}
Invoke-WebRequest -UseBasicParsing -OutFile jre8.exe $URL
Start-Process .\jre8.exe '/s REBOOT=0 SPONSORS=0 AUTO_UPDATE=0' -wait
Few weeks ago I was able to run it successfully but since yesterday getting the above error.
Any advise?
Thanks.
This happens, as there is no such as string as Download Java software for Windows on the web page. Since the regex doesn't match anything, Groups member doesn't exist and you'll get an error about trying to index into a non-existing member.
Either use a web browser's View Source command, or save the content on a text file and view it with Notepad like so,
$cc = (Invoke-WebRequest -UseBasicParsing https://www.java.com/en/download/manual.jsp).Content
Set-Content -Path c:\temp\javapage.txt -Value $cc
notepad c:\temp\javapage.txt
The page loads a bunch of Javascript that generates the actual page seen on a browser.
The PhraseSuggestionBuilder in 1.6.0 of the elasticsearch java API has a collateQuery method that takes a String.
builder
.collateQuery("\"match\": {\"title\" : \"{{suggestion}}\"}")
.collatePrune(true);
Unfortunately the escaped quotes are escaped by the builder to produce JSON like this:
"collate" : {
"query" : "\"match\": {\"title\" : \"{{suggestion}}\"}",
"prune" : true
}
Anyone any ideas how I can stop this String being escaped as the JSON is generated?
Thanks.
Apache common-cli has a example on its web site for ls command:
options.addOption( "a", "all", false, "do not hide entries starting with ." );
options.addOption( "A", "almost-all", false, "do not list implied . and .." );
options.addOption( "b", "escape", false, "print octal escapes for nongraphic " + "characters" );
options.addOption( OptionBuilder.withLongOpt( "block-size" )
.withDescription( "use SIZE-byte blocks" )
.hasArg()
.withArgName("SIZE")
.create() );
This shows help like this:
-a, --all do not hide entries starting with .
-A, --almost-all do not list implied . and ..
-b, --escape print octal escapes for nongraphic characters
--block-size=SIZE use SIZE-byte blocks
When I write this code, it shows --block-size <SIZE>. I want to show something like this: -z,--block-size=SIZE(not just
long option).
what is the difference of PosixParser and GnuParser? I changed
them in the code, I didn't observed any difference.
When I provide wrong option for example h it doesn't throw any ParseException. The program starts and finishes normally.
The block size option in the example has only a long format, that's why there is no short option shown. If you add a short alias you'll get the result you expect
PosixParser and GnuParser are deprecated in the latest version of Commons CLI. A new unified parser is available as DefaultParser. The posix parser had the ability to parse concatenated short options, something like tar -zxvf foo.tar.gz.
Extra options are either handled as arguments to the application or trigger an exception, depending of the value of the stopAtNonOption parser parameter.