I'm new to couchdb and I want to populate my database using Git Bash. I made a text document with the following:
{
"_id" : "_design/example",
"views" : {
"foo" : {
"map" : "function(doc){ emit(doc._id, doc._rev)}"
}
}
}
I named it design.json and in Git Bash I type curl -X PUT http://<username>:<password>#localhost:5984/testdb/_design/example --data-binary ‘#design.json’
My problem is that instead of adding the file to my database I get a warning:
Warning: Couldn't read data from file "design.json", this makes an empty POST.
followed by an error:
{"error":"bad_request","reason":"invalid_json"}
The JSON you posted plus the curl call work for me – but of course only if I change the ‘...’ smart quotes from your posting to '...'. In the absence of another reason to fail, I suggest this is the problem.
Related
I am aware that there are already similar questions on StackOverflow, but none of them fixes my current problem.
I am using ElasticSearch 6.7.0, and I am currently working through its tutorial. Currently, I am stuck at this step, which requires me to download some sample data stored inside a file accounts.json. Here is a small snippet of the file:
{"index":{"_id":"988"}}
{"account_number":988,"balance":17803,"firstname":"Lucy","lastname":"Castro","age":34,"gender":"F","address":"425 Fleet Walk","employer":"Geekfarm","email":"lucycastro#geekfarm.com","city":"Mulino","state":"VA"}
{"index":{"_id":"990"}}
{"account_number":990,"balance":44456,"firstname":"Kelly","lastname":"Steele","age":35,"gender":"M","address":"809 Hoyt Street","employer":"Eschoir","email":"kellysteele#eschoir.com","city":"Stewartville","state":"ID"}
{"index":{"_id":"995"}}
{"account_number":995,"balance":21153,"firstname":"Phelps","lastname":"Parrish","age":25,"gender":"M","address":"666 Miller Place","employer":"Pearlessa","email":"phelpsparrish#pearlessa.com","city":"Brecon","state":"ME"}
The file is saved inside the path C:\Users\Me\Desktop\accounts.json.
I ran the following command, as instructed in the tutorial:
PS C:\Users\Me\Downloads\curl-7.64.1-win64-mingw\bin> .\curl.exe -H
"Content-Type: applicati lhost:9200/bank/_doc/_bulk?pretty&refresh"
--data-binary "C:\Users\Me\Desktop\accounts.json"
I received this error message:
{
"error" : {
"root_cause" : [
{
"type" : "illegal_argument_exception",
"reason" : "The bulk request must be terminated by a newline [\n]"
}
],
"type" : "illegal_argument_exception",
"reason" : "The bulk request must be terminated by a newline [\n]"
},
"status" : 400
}
To fix this, I went to the end of the file and inserted a new line by pressing Enter. However, the same error message appeared when I ran the command again.
How can I fix this issue?
I moved the accounts.json file to the same directory from which I am running curl.exe, and executed this command:
.\curl.exe -H "Content-Type: applicati
lhost:9200/bank/_doc/_bulk?pretty&refresh" --data-binary
"#accounts.json"
This time, the bulk request was successful.
For those facing similar issues, remember to insert a new line at the end of the file by pressing Enter. If that doesn't work, try moving your file into another directory. The error message "The bulk request must be terminated by a newline" can be misleading -- your file might not be malformatted at all; perhaps it simply cannot be located or accessed for various reasons.
Use "#C:\Users\Me\Desktop\accounts.json" instead of "C:\Users\Me\Desktop\accounts.json"
i.e.
\curl.exe -H "Content-Type: applicati lhost:9200/bank/_doc/_bulk?pretty&refresh" --data-binary "#C:\Users\Me\Desktop\accounts.json"
Try changing the EOL characters and Encoding of your file, as described here:
https://stackoverflow.com/a/59791215/2726844
On windows,I am try to edit my own json file
I use Enter key to get a new line. when I use git bash with command curl -XPOST 'localhost:9200/customer/external/_bulk?pretty' --data-binary #act.json
it just throw some error action_request_validation_exception ,and i use \n on the fisrt line,it still doesn't work,then I use data generated from JSON Generator my git bash can read it well.I read some resource, it is related to new line.so here comes my question,when I want to edit my own json file,how to make a new line just like the genrated file from json generator .thanks a lot!
I'm trying to POST data from a bash variable using curl, however, I'm unable to get this to work. This is the command that I'm using:
escape() { printf "%q" "$1"; }
curl -d "$(escape "$client")" -X POST -v https://$server/clients
The client variable look like this:
{"roles":["test"],"softwareName":"Some Soft","passwordSalt":"aaa","clientID":"full-client-2","contactPerson":"Test","contactPersonEmail":"a#b.org","description":"test","name":"Full Client-2","organization":"Some Org","passwordAlgorithm":"sha512","passwordHash":"bbb"}
And on the server I'm receiving the following:
{ '{"roles":': { '"test"': { '\"test\"\': '' } } }
I think its a problem with the escaping but I can't figure this out.
I've had a look at a number of other questions about this on here, but it seems most people need to insert variable into a literal that they are then trying to post. My problem is around using an entire variable as the json body. I've tried to use their answers to help me out but I haven't had any luck so far.
Don't try to quote it; use a here document:
curl -d#- -X POST -v https://"$server"/clients <<JSON
{"roles":["test"],"softwareName":"Some Soft","passwordSalt":"aaa","clientID":"full-client-2","contactPerson":"Test","contactPersonEmail":"a#b.org","description":"test","name":"Full Client-2","organization":"Some Org","passwordAlgorithm":"sha512","passwordHash":"bob"}
JSON
#- tells the -d option to look in standard input for the data, rather than using a hard-coded string.
If the text is in a variable, nothing more needs to be done; just quote the variable to prevent the shell from processing it:
curl -d "$client" -X POST -v https://"$server"/clients
when copying a database from one host to another I get the folowing error : Missing JSON list of 'docs'
Here is what I do :
source> curl -X GET http://127.0.0.1:5984/cozy/_all_docs?include_docs=true > cozy.dump
destination> curl -X PUT http://127.0.0.1:5984/cozy
{"ok":true}
destination> curl -d #cozy.dump -H "Content-type: application/json" -X POST http://localhost:5984/cozy/_bulk_docs
{"error":"bad_request","reason":"Missing JSON list of 'docs'"}
any idea ?
Thanks !
This is, indeed, a problem with versions.
Fortunately it is fairly easy to fix: just change the first line in the dump, eg.
{"total_rows": 8244, "offset": 0, "rows": [
to
{"docs": [
The dumps can now be used in the later versions.
I know this is an old question but I am still posting an answer in case some one else is looking for the solution. The bulk docs api accepts the request in a certain form.
{docs:[{},{},{}]}
The docs key must contain an array of documents to be bulk inserted. What op did with
curl -X GET http://127.0.0.1:5984/cozy/_all_docs?include_docs=true > cozy.dump
was that he simply stored the couchdb response of the format
{
total_rows: 4,
offset: 0,
rows: [....]
}
into the cozy.dump file. As we have seen above this file is not in a form that can be consumed by the bulk docs api. Hence the error
{"error":"bad_request","reason":"Missing JSON list of 'docs'"}
Couchdb needs a JSON list of docs to perform the bulk insert.
Another point to be noted here is that if you supply an _id and _rev parameter couchdb performs a bulk update rather than a bulk insert. If you just want to copy one database to another use http://wiki.apache.org/couchdb/Replication
So very basic question about elasticsearch which the docs not answer very clearly (because they seem to go into many advanced details but miss the basic ones!).
Example: range query
http://www.elasticsearch.org/guide/reference/query-dsl/range-query.html
Doesn't tell how to PERFORM the range, is it via the search endpoint?
And if it is, then how to do it via querystring? I mean, I want to do a GET, not a POST (because it's a query, not an insertion/modification). However the documention for GET requests doesn't tell how to use JSON like in the Range sample:
http://www.elasticsearch.org/guide/reference/api/search/uri-request.html
What am I missing?
Thanks
Use the Lucene query syntax:
curl -X GET 'http://localhost:9200/my_index/_search?q=my_field:[0+TO+25]&pretty'
Let's assume we have an index
curl -XPUT localhost:9200/test
And some documents
curl -XPUT localhost:9200/test/range/1 -d '{"age": 9}'
curl -XPUT localhost:9200/test/range/2 -d '{"age": 12}'
curl -XPUT localhost:9200/test/range/3 -d '{"age": 16}'
Now we can query these documents within a certain range via
curl -XGET 'http://localhost:9200/test/range/_search?pretty=true' -d '
{
"query" : {
"range" : {
"age" : {
"from" : "10",
"to" : "20",
"include_lower" : true,
"include_upper": true
}
}
}
}
'
This will return the documents 2 and 3.
I'm not sure if there is a way to perform these kind of complex queries via URI request, though.
Edit: Thanks to karmi here is the solution without JSON request:
curl -XGET --globoff 'localhost:9200/test/range/_search?q=age:["10"+TO+"20"]&pretty=true'
Replying to myself thanks to #javanna:
In the RequestBody section of the Search docs:
http://www.elasticsearch.org/guide/reference/api/search/request-body.html
At the end, it says:
The rest of the search request should be passed within the body itself. The body content can also be passed as a REST parameter named source.
So I guess that I need to use the search endpoint with the source attribute to pass json.