elasticsearch中有没有办法从包含最大值的文档中获取字段? (基本上与scala中的maxBy的工作方式相同)
例如(模拟):
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import org.apache.commons.io.IOUtils;
public class MobileSample {
public static void main(String[] args) throws IOException {
URL url = new URL("https://<mobile_service_name>.azure-mobile.net/api/<api_name: example 'filestorage'>");
HttpURLConnection https = (HttpURLConnection) url.openConnection();
https.addRequestProperty("X-ZUMO-APPLICATION", "<manage_access_key>");
https.connect();
int respCode = https.getResponseCode();
System.out.println(respCode);
InputStream is = https.getInputStream();
IOUtils.copy(is, new FileOutputStream("<file_name>"));
is.close();
}
}
{
"aggregations": {
"grouped": {
"terms": {
"field": "grouping",
"order": {
"docWithMin": "asc"
}
},
"aggregations": {
"withMax": {
"max": {
"maxByField": "a",
"field": "b"
}
}
}
}
}
}
会返回(类似):{"grouping":1,"a":2,"b":5},{"grouping":1,"a":1,"b":10}
,其中最大值来自第一个对象,因为{"grouped":1,"withMax":5}
更高。
答案 0 :(得分:0)
假设您只想要回复最多的文档,您可以这样做:
{
"size": 0,
"aggs": {
"grouped": {
"terms": {
"field": "grouping"
},
"aggs": {
"maxByA": {
"top_hits": {
"sort": [
{"a": {"order": "desc"}}
],
"size": 1
}
}
}
}
}
}