12
votes

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

3
There are common options to many queriesDzmitry Lahoda

3 Answers

15
votes

Use the Lucene query syntax:

curl -X GET 'http://localhost:9200/my_index/_search?q=my_field:[0+TO+25]&pretty'
8
votes

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'

0
votes

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.