12

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

knocte
  • 16,941
  • 11
  • 79
  • 125
  • There are [common options](https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html) to many queries – Dzmitry Lahoda Sep 08 '16 at 14:37

3 Answers3

15

Use the Lucene query syntax:

curl -X GET 'http://localhost:9200/my_index/_search?q=my_field:[0+TO+25]&pretty'
karmi
  • 14,059
  • 3
  • 33
  • 41
  • 1
    meh, I'm looking for still using the json syntax – knocte Feb 16 '13 at 03:15
  • 1
    That is obviously a better solution -- in your question, you explicitely ask how to do it without the JSON DSL definiton. – karmi Feb 16 '13 at 09:41
  • 1
    where do you read that? I say "the documention for GET requests doesn't tell how to use JSON like in the Range sample" so obviously I'm looking for the JSON solution – knocte Feb 16 '13 at 10:45
8

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'

Thorsten
  • 5,634
  • 6
  • 35
  • 33
  • I'm sorry, the title of this question clearly states "without postbody" so I'm downvoting this – knocte Feb 13 '13 at 00:42
  • you cannot send body in most GET verb APIs (i.e. .NET) – knocte Feb 13 '13 at 08:34
  • I know that the answer is not perfectly fitting the question, but like I mentioned, I'm not sure if there is another way, since elasticsearch is expecting JSON objects for querying. – Thorsten Feb 13 '13 at 10:18
  • karmi posted the right way to do it, I added it to my answer as well – Thorsten Feb 13 '13 at 10:35
  • 1
    knocte: Two things: 1) ES allows POSTs to certain endpoints that are normally a GET (such as the search endpoint), because not all clients can send a GET with a body. 2) GETs with a body are technically compatible with HTTP/1.1. Perhaps not recommended...but nothing incorrect. See this for more details: http://stackoverflow.com/questions/978061/http-get-with-request-body – Zach Feb 13 '13 at 15:18
0

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.

knocte
  • 16,941
  • 11
  • 79
  • 125
  • can you please explain how are you using source attribute with the search endpoint to get the range working? - I am having similar issue with date ranges, format yyyyMMddHHmmss within the body of the JSON – Kausty Apr 08 '15 at 17:54
  • Nevermind, I guess I found it here (+1 for question in the first place) - http://stackoverflow.com/questions/12988201/passing-json-data-in-elasticsearch-get-request-using-rest-client-ruby-gem – Kausty Apr 09 '15 at 14:59
  • sorry it's been a very big while since I don't use elasticsearch – knocte Apr 09 '15 at 15:09