Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print the full elasticsearch request for debug in java

I use

ElasticSearchTemplate().queryForPage(SearchQuery, CLASS)

How can I print the full json request?

I manage to print only filter by doing :

searchQuery.getFilter().toString()

But cant manage to do the same with:

 searchQuery.getAggregations().toString();

I would like to print in console something like :

 "aggs": {
   "agg1": {
     "terms": {
       "field": "basket_id_1",
       "size": 0
     },
     "aggs": {
       "basket_id_2": {
         "terms": {
           "field": "basket_id_2",
           "size": 0
         },
         "aggs": {
           "basket_id_3": {
             "terms": {
               "field": "basket_id_3",
               "size": 0
             }
           }
         }
       }
     }
   }
 }
like image 847
Xarouma Avatar asked Dec 12 '14 09:12

Xarouma


2 Answers

This is what I've started using to do the same thing.

{
  "top_agg": {
    "terms": {
      "field": "id",
      "size": 100
    },
    "aggregations": {
      "parent": {
        "nested": {
          "path": "transactions"
        },
        "aggregations": {
          "totals": {
            "filter": {
              "terms": {
                "transactions.type": [
                  "ttype"
                ]
              }
            },
            "total_events": {
              "cardinality": {
                "field": "parent.field"
              }
            }
          }
        }
      }
    }
  }
}


NativeSearchQuery query = queryBuilder.build();

if (query.getQuery() != null) {
    log.debug(query.getQuery().toString());
}
if (query.getAggregations() != null) {
    try {
        XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
        builder.startObject();
        for (AbstractAggregationBuilder subAgg : query.getAggregations()) {
            subAgg.toXContent(builder, ToXContent.EMPTY_PARAMS);
        }
        builder.endObject();
        log.debug(builder.string());

    } catch (IOException e) {
        log.debug("Error parsing aggs");
    }
}
like image 66
BrettAHale Avatar answered Oct 06 '22 01:10

BrettAHale


Could you use the SearchResponse.getAggregations().asList() ?

like image 43
sven.kwiotek Avatar answered Oct 06 '22 00:10

sven.kwiotek