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.
Related
I'm using curl to send some json data. Part of the data is a request counter that needs to be incremented after each call.
I would like to reduce the code below by incrementing it right after evaluating it. I'm not sure how to format the variable within the json string though.
Thank you in advance!
#!/bin/bash
reqcnt=0
curl http://myurl.com --data-binary '{"requestCounter":'${reqcnt}'}'
((reqcnt++))
Expected:
#!/bin/bash
reqcnt=0
curl http://myurl.com --data-binary '{"requestCounter":'${((reqcnt++)}'}'
Edit
Taking into account the great answer by Inian, I noticed there are cases where I need to save the output of curl. For some reason the arithmetic operation is not performed on the variable in that case:
res=$(curl http://myurl.com --data-binary {"requestCounter":'"$((reqcnt++))"'}')
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.
I am using a REST API to move a user into a group, but need to append the group name to the URL when I run the command.
So the path to my REST API is:
http://server:8080/rest/api/2/group/user?groupname
And it's expecting "groupname" to be passed as "groupname="Name%20Of%20Group"
i.e.
http://server:8080/rest/api/2/group/user?groupname="Name%20Of%20Group"
The full command I'm running on Windows is
curl -u name:pass -X POST --data #add_user.txt -H "Content-Type: application/json" http://server:8080/rest/api/2/group/user?groupname
add_user.txt is structured like this
{
"name": "tim",
"groupname": "MY%20TEST%20GROUP"
},
{
"name": "carol",
"groupname": "MY%20TEST%20GROUP"
}
It's looping through the names I believe, but I want CURL to pick up on each "groupname" defined in the file.
Any ideas on how I can do this?
each curl call makes a single request.
looks like you need to change from POST to GET and use the -G, --get option to add the values as parameters after the ?
See How do I use cURL to perform multiple simultaneous requests?
HTH
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
I'm stuck with a little elasticsearch problem. I'm new to elasticsearch and don't know why this doesn't work.
curl -XPOST 'http://myhost.nl:9200/my_index/test/_search?pretty=true' -d '{ "fields": ["message"] }'
I don't get any field back. The field "message" does exist and realy looks like the example on the elasticsearch site. http://www.elasticsearch.org/guide/reference/api/search/fields.html
Can anybody see what I'm missing?
Your query would have worked if this field was stored. But since it's not stored and only available as part of source, you need to specify full path to it. Try:
curl -XPOST 'http://myhost.nl:9200/my_index/test/_search?pretty=true' -d '{ "fields": ["tweet.message"] }'