com.github.mengxianun.elasticsearch.data.ElasticsearchQuerySummary Maven / Gradle / Ivy
package com.github.mengxianun.elasticsearch.data;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import com.github.mengxianun.core.Action;
import com.github.mengxianun.core.App;
import com.github.mengxianun.core.data.DefaultRow;
import com.github.mengxianun.core.data.Row;
import com.github.mengxianun.core.data.summary.QuerySummary;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.google.gson.reflect.TypeToken;
public class ElasticsearchQuerySummary extends QuerySummary {
private static final String NODE_TOTAL = "total";
private static final String NODE_HITS = "hits";
private static final String NODE_AGGREGATIONS = "aggregations";
private static final String NODE_GROUPBY = "groupby";
private static final String NODE_BUCKETS = "buckets";
private static final String NODE_SOURCE = "_source";
private static final String NODE_FIELDS = "fields";
private static final String NODE_KEY = "key";
private final String resultString;
public ElasticsearchQuerySummary(String resultString) {
this(null, resultString);
}
public ElasticsearchQuerySummary(Action action, String resultString) {
super(action, null);
this.resultString = resultString;
}
@Override
public List toRows() {
List rows = new ArrayList<>();
JsonObject response = App.gson().fromJson(resultString, JsonObject.class);
// hits
JsonObject hits = response.getAsJsonObject(NODE_HITS);
total = hits.get(NODE_TOTAL).getAsLong();
JsonArray items = hits.getAsJsonArray(NODE_HITS);
if (items.size() > 0) {
items.forEach(e -> rows.add(new DefaultRow(header, parseHit(e.getAsJsonObject()))));
}
// aggregations
if (response.has(NODE_AGGREGATIONS)) {
JsonObject aggregations = response.getAsJsonObject(NODE_AGGREGATIONS);
JsonObject groupby = aggregations.getAsJsonObject(NODE_GROUPBY);
JsonArray buckets = groupby.get(NODE_BUCKETS).getAsJsonArray();
buckets.forEach(e -> rows.add(new DefaultRow(header, parseBucket(e.getAsJsonObject()))));
}
return rows;
}
private Object[] parseHit(JsonObject hit) {
final Object[] values = new Object[header.size()];
// _source
if (hit.has(NODE_SOURCE)) {
JsonObject source = hit.getAsJsonObject(NODE_SOURCE);
for (Entry entry : source.entrySet()) {
String field = entry.getKey();
Object value = entry.getValue();
int i = header.indexOf(field);
if (i == -1) {
continue;
}
values[i] = value;
}
}
// fields
if (hit.has(NODE_FIELDS)) {
JsonObject fields = hit.getAsJsonObject(NODE_FIELDS);
for (Entry entry : fields.entrySet()) {
String field = entry.getKey();
JsonElement valueElement = entry.getValue();
Object value = valueElement;
int i = header.indexOf(field);
if (i == -1) {
continue;
}
if (valueElement.isJsonArray()) {
JsonElement jsonElement = valueElement.getAsJsonArray().get(0);
value = jsonElement;
if (jsonElement.isJsonPrimitive()) {
value = getRealValue(jsonElement.getAsJsonPrimitive());
}
}
values[i] = value;
}
}
return values;
}
private Map parseHitToMap(JsonObject hit) {
final Map values = new HashMap<>();
// _source
if (hit.has(NODE_SOURCE)) {
JsonObject source = hit.getAsJsonObject(NODE_SOURCE);
Type dataType = new TypeToken