如何在java中打印完整的elasticsearch请求

时间:2014-12-12 09:19:43

标签: java spring debugging elasticsearch

我用

ElasticSearchTemplate().queryForPage(SearchQuery, CLASS)

如何打印完整的json请求?

我设法通过执行以下操作仅打印过滤器:

searchQuery.getFilter().toString()

但是不能设法做同样的事情:

 searchQuery.getAggregations().toString();

我想在控制台中打印如下:

 "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
             }
           }
         }
       }
     }
   }
 }

2 个答案:

答案 0 :(得分:2)

这是我开始用来做同样的事情。

{
  "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");
    }
}

答案 1 :(得分:0)

你能使用SearchResponse.getAggregations()。asList()吗?