0

can someone please advise how can I convert this JSON body into a REST URI ?

GET api/_search
{
 "age":"5",
 "aggs" : {
 "uniq_gender" : {
 "terms" : { "field" : "Gender.keyword" }
   }
  }
}
crazydev
  • 575
  • 2
  • 9
  • 30
  • what is the resource you are performing a search for? it should be a noun, e.g. customer or employee or smth – Artem Sep 11 '17 at 05:54
  • The Gender.keyword – crazydev Sep 11 '17 at 05:56
  • it is not a recource, it is a filter - search input. What kind of entity is the result of search (output)? – Artem Sep 11 '17 at 06:09
  • The output is a nested json body. Example: `{ "took": 12, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 30003, "max_score": 0, "hits": [] }, "aggregations": { "uniq_gender": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "America", "doc_count": 11570 }, { "key": "AMI", "doc_count": 8266 }` – crazydev Sep 11 '17 at 06:14
  • and I want to access the `"key"` value. – crazydev Sep 11 '17 at 06:15

1 Answers1

1

You may proceed with one of two options:

  1. Use POST with body

    POST api/_search
    {
     "age":"5",
     "aggs" : {
     "uniq_gender" : {
     "terms" : { "field" : "Gender.keyword" }
       }
      }
    }
    

It may seem like a hack, but it is simple and frankly it is widely used. Basically from REST perspective it may be considered as resource creation (filter rather than seach might be a better word here).

  1. Use query string with GET.

Something like:

GET api/_search?age=5,field=Gender.keyword

The problem with using query string is that it may be limited. In RFC there is a code for such a case. For example IE browser has such a limit - see details.

Generally speaking if there is no technical problem, readability issue may appear - it is hard to deal with 1000+ symbols string.

Artem
  • 2,084
  • 2
  • 21
  • 29