Convert any Elasticsearch response to simple field

2019-07-22 06:29发布

问题:

On elastic search, when doing a simple query like:

GET miindex-*/mytype/_search
{
  "query": {
    "query_string": {
      "analyze_wildcard": true,
      "query": "*"
    }
  } 
}

It returns a format like:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "hits": {
    "total": 28,
    "max_score": 1,
    "hits": [
   ...

So I parse like response.hits.hits to get the actual records. However if you are doing another type of query e.g. aggregation, the response is totally different like:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "hits": {
    "total": 28,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "myfield": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
      ...

and I need to look actually in another json property: response.aggregations.myfield.buckets which gets even more complicated if you have more than one aggregation.

So, my question is very simple, isn't there a way that I can get Elasticsearch to response always with just the fields I want just like in SQL format:

E.g.

SELECT author, bookid FROM books

Would return:

{"author":"rogers", "bookid":099991}
{"author":"peter",  "bookid":099992}

SELECT COUNT(author) As count_author, author, count(bookid) As count_bookid, bookid FROM books GROUP BY author, bookid

Would return:

{"count_author":4, "author":"rogers", "count_bookid":9, "bookid":099991}
{"count_author":8, "author":"peter", "count_bookid":9, "bookid":099992}

Is there a way to show only the fields I want and nothing else?(not having to look within nested json objects and all that stuff). (I want this because I'm doing many reports and I want to have a simple function that parses each response easily in a uniform way.)