本文整理汇总了Java中org.elasticsearch.search.aggregations.AggregationBuilder类的典型用法代码示例。如果您正苦于以下问题:Java AggregationBuilder类的具体用法?Java AggregationBuilder怎么用?Java AggregationBuilder使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
AggregationBuilder类属于org.elasticsearch.search.aggregations包,在下文中一共展示了AggregationBuilder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: countDomainByGatherTime
import org.elasticsearch.search.aggregations.AggregationBuilder; //导入依赖的package包/类
/**
* 统计指定网站每天抓取数量
*
* @param domain 网站域名
* @return
*/
public Map<Date, Long> countDomainByGatherTime(String domain) {
AggregationBuilder aggregation =
AggregationBuilders
.dateHistogram("agg")
.field("gatherTime")
.dateHistogramInterval(DateHistogramInterval.DAY).order(Histogram.Order.KEY_DESC);
SearchRequestBuilder searchRequestBuilder = client.prepareSearch(INDEX_NAME)
.setTypes(TYPE_NAME)
.setQuery(QueryBuilders.matchQuery("domain", domain))
.addAggregation(aggregation);
SearchResponse response = searchRequestBuilder.execute().actionGet();
Histogram agg = response.getAggregations().get("agg");
Map<Date, Long> result = Maps.newHashMap();
for (Histogram.Bucket entry : agg.getBuckets()) {
DateTime key = (DateTime) entry.getKey(); // Key
long docCount = entry.getDocCount(); // Doc count
result.put(key.toDate(), docCount);
}
return result;
}
示例2: getElasticsearchGeohashAggregations
import org.elasticsearch.search.aggregations.AggregationBuilder; //导入依赖的package包/类
protected List<AggregationBuilder> getElasticsearchGeohashAggregations(GeohashAggregation agg) {
List<AggregationBuilder> aggs = new ArrayList<>();
PropertyDefinition propertyDefinition = getPropertyDefinition(agg.getFieldName());
if (propertyDefinition == null) {
throw new VertexiumException("Unknown property " + agg.getFieldName() + " for geohash aggregation.");
}
if (propertyDefinition.getDataType() != GeoPoint.class) {
throw new VertexiumNotSupportedException("Only GeoPoint properties are valid for Geohash aggregation. Invalid property " + agg.getFieldName());
}
for (String propertyName : getPropertyNames(agg.getFieldName())) {
String visibilityHash = getSearchIndex().getPropertyVisibilityHashFromPropertyName(propertyName);
String aggName = createAggregationName(agg.getAggregationName(), visibilityHash);
GeoGridAggregationBuilder geoHashAgg = AggregationBuilders.geohashGrid(aggName);
geoHashAgg.field(propertyName + Elasticsearch5SearchIndex.GEO_POINT_PROPERTY_NAME_SUFFIX);
geoHashAgg.precision(agg.getPrecision());
aggs.add(geoHashAgg);
}
return aggs;
}
示例3: geoBounds
import org.elasticsearch.search.aggregations.AggregationBuilder; //导入依赖的package包/类
private AggregationBuilder geoBounds(MethodField field) throws SqlParseException {
String aggName = gettAggNameFromParamsOrAlias(field);
GeoBoundsAggregationBuilder boundsBuilder = AggregationBuilders.geoBounds(aggName);
String value = null;
for (KVValue kv : field.getParams()) {
value = kv.value.toString();
switch (kv.key.toLowerCase()) {
case "field":
boundsBuilder.field(value);
break;
case "wrap_longitude":
boundsBuilder.wrapLongitude(Boolean.getBoolean(value));
break;
case "alias":
case "nested":
case "reverse_nested":
case "children":
break;
default:
throw new SqlParseException("geo_bounds err or not define field " + kv.toString());
}
}
return boundsBuilder;
}
示例4: getElasticsearchAggregations
import org.elasticsearch.search.aggregations.AggregationBuilder; //导入依赖的package包/类
protected List<AggregationBuilder> getElasticsearchAggregations(Iterable<Aggregation> aggregations) {
List<AggregationBuilder> aggs = new ArrayList<>();
for (Aggregation agg : aggregations) {
if (agg instanceof HistogramAggregation) {
aggs.addAll(getElasticsearchHistogramAggregations((HistogramAggregation) agg));
} else if (agg instanceof RangeAggregation) {
aggs.addAll(getElasticsearchRangeAggregations((RangeAggregation) agg));
} else if (agg instanceof PercentilesAggregation) {
aggs.addAll(getElasticsearchPercentilesAggregations((PercentilesAggregation) agg));
} else if (agg instanceof TermsAggregation) {
aggs.addAll(getElasticsearchTermsAggregations((TermsAggregation) agg));
} else if (agg instanceof GeohashAggregation) {
aggs.addAll(getElasticsearchGeohashAggregations((GeohashAggregation) agg));
} else if (agg instanceof StatisticsAggregation) {
aggs.addAll(getElasticsearchStatisticsAggregations((StatisticsAggregation) agg));
} else if (agg instanceof CalendarFieldAggregation) {
aggs.addAll(getElasticsearchCalendarFieldAggregation((CalendarFieldAggregation) agg));
} else {
throw new MemgraphException("Could not add aggregation of type: " + agg.getClass().getName());
}
}
return aggs;
}
示例5: getElasticsearchGeohashAggregations
import org.elasticsearch.search.aggregations.AggregationBuilder; //导入依赖的package包/类
protected List<AggregationBuilder> getElasticsearchGeohashAggregations(GeohashAggregation agg) {
List<AggregationBuilder> aggs = new ArrayList<>();
PropertyDefinition propertyDefinition = getPropertyDefinition(agg.getFieldName());
if (propertyDefinition == null) {
throw new MemgraphException("Unknown property " + agg.getFieldName() + " for geohash aggregation.");
}
if (propertyDefinition.getDataType() != GeoPoint.class) {
throw new MemgraphNotSupportedException("Only GeoPoint properties are valid for Geohash aggregation. Invalid property " + agg.getFieldName());
}
for (String propertyName : getPropertyNames(agg.getFieldName())) {
String visibilityHash = getSearchIndex().getPropertyVisibilityHashFromPropertyName(propertyName);
String aggName = createAggregationName(agg.getAggregationName(), visibilityHash);
GeoGridAggregationBuilder geoHashAgg = AggregationBuilders.geohashGrid(aggName);
geoHashAgg.field(propertyName + Elasticsearch5SearchIndex.GEO_POINT_PROPERTY_NAME_SUFFIX);
geoHashAgg.precision(agg.getPrecision());
aggs.add(geoHashAgg);
}
return aggs;
}
示例6: getElasticsearchCalendarFieldAggregation
import org.elasticsearch.search.aggregations.AggregationBuilder; //导入依赖的package包/类
private Collection<? extends AggregationBuilder> getElasticsearchCalendarFieldAggregation(CalendarFieldAggregation agg) {
List<AggregationBuilder> aggs = new ArrayList<>();
PropertyDefinition propertyDefinition = getPropertyDefinition(agg.getPropertyName());
if (propertyDefinition == null) {
throw new MemgraphException("Could not find mapping for property: " + agg.getPropertyName());
}
Class propertyDataType = propertyDefinition.getDataType();
for (String propertyName : getPropertyNames(agg.getPropertyName())) {
String visibilityHash = getSearchIndex().getPropertyVisibilityHashFromPropertyName(propertyName);
String aggName = createAggregationName(agg.getAggregationName(), visibilityHash);
if (propertyDataType == Date.class) {
HistogramAggregationBuilder histAgg = AggregationBuilders.histogram(aggName);
histAgg.interval(1);
if (agg.getMinDocumentCount() != null) {
histAgg.minDocCount(agg.getMinDocumentCount());
} else {
histAgg.minDocCount(1L);
}
Script script = new Script(
ScriptType.INLINE,
"painless",
getCalendarFieldAggregationScript(agg, propertyName),
ImmutableMap.of(
"tzId", agg.getTimeZone().getID(),
"fieldName", propertyName,
"calendarField", agg.getCalendarField())
);
histAgg.script(script);
for (AggregationBuilder subAgg : getElasticsearchAggregations(agg.getNestedAggregations())) {
histAgg.subAggregation(subAgg);
}
aggs.add(histAgg);
} else {
throw new MemgraphException("Only dates are supported for hour of day aggregations");
}
}
return aggs;
}
示例7: testCase
import org.elasticsearch.search.aggregations.AggregationBuilder; //导入依赖的package包/类
private Aggregation testCase(Query query, AggregationBuilder builder) throws IOException {
Directory directory = newDirectory();
RandomIndexWriter iw = new RandomIndexWriter(random(), directory);
iw.addDocument(document("1", "a", "b"));
iw.addDocument(document("2", "c", "a"));
iw.addDocument(document("3", "b", "d"));
iw.close();
IndexReader indexReader = DirectoryReader.open(directory);
// We do not use LuceneTestCase.newSearcher because we need a DirectoryReader for "testInsideTerms"
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
Aggregation result = searchAndReduce(indexSearcher, query, builder, STRING_FIELD_TYPE);
indexReader.close();
directory.close();
return result;
}
示例8: makeRangeGroup
import org.elasticsearch.search.aggregations.AggregationBuilder; //导入依赖的package包/类
private AggregationBuilder makeRangeGroup(MethodField field) throws SqlParseException {
switch (field.getName().toLowerCase()) {
case "range":
return rangeBuilder(field);
case "date_histogram":
return dateHistogram(field);
case "date_range":
return dateRange(field);
case "month":
return dateRange(field);
case "histogram":
return histogram(field);
case "geohash_grid":
return geohashGrid(field);
case "geo_bounds":
return geoBounds(field);
case "terms":
return termsAgg(field);
default:
throw new SqlParseException("can define this method " + field);
}
}
示例9: getGroupAgg
import org.elasticsearch.search.aggregations.AggregationBuilder; //导入依赖的package包/类
private AggregationBuilder getGroupAgg(Field field, Select select2) throws SqlParseException {
boolean refrence = false;
AggregationBuilder lastAgg = null;
for (Field temp : select.getFields()) {
if (temp instanceof MethodField && temp.getName().equals("script")) {
MethodField scriptField = (MethodField) temp;
for (KVValue kv : scriptField.getParams()) {
if (kv.value.equals(field.getName())) {
lastAgg = aggMaker.makeGroupAgg(scriptField);
refrence = true;
break;
}
}
}
}
if (!refrence) lastAgg = aggMaker.makeGroupAgg(field);
return lastAgg;
}
示例10: createNestedAggregation
import org.elasticsearch.search.aggregations.AggregationBuilder; //导入依赖的package包/类
private AggregationBuilder createNestedAggregation(Field field) {
AggregationBuilder nestedBuilder;
String nestedPath = field.getNestedPath();
if (field.isReverseNested()) {
if (nestedPath == null || !nestedPath.startsWith("~")) {
ReverseNestedAggregationBuilder reverseNestedAggregationBuilder = AggregationBuilders.reverseNested(getNestedAggName(field));
if(nestedPath!=null){
reverseNestedAggregationBuilder.path(nestedPath);
}
return reverseNestedAggregationBuilder;
}
nestedPath = nestedPath.substring(1);
}
nestedBuilder = AggregationBuilders.nested(getNestedAggName(field),nestedPath);
return nestedBuilder;
}
示例11: explanFields
import org.elasticsearch.search.aggregations.AggregationBuilder; //导入依赖的package包/类
private void explanFields(SearchRequestBuilder request, List<Field> fields, AggregationBuilder groupByAgg) throws SqlParseException {
for (Field field : fields) {
if (field instanceof MethodField) {
if (field.getName().equals("script")) {
request.addStoredField(field.getAlias());
DefaultQueryAction defaultQueryAction = new DefaultQueryAction(client, select);
defaultQueryAction.intialize(request);
List<Field> tempFields = Lists.newArrayList(field);
defaultQueryAction.setFields(tempFields);
continue;
}
AggregationBuilder makeAgg = aggMaker.makeFieldAgg((MethodField) field, groupByAgg);
if (groupByAgg != null) {
groupByAgg.subAggregation(makeAgg);
} else {
request.addAggregation(makeAgg);
}
} else if (field instanceof Field) {
request.addStoredField(field.getName());
} else {
throw new SqlParseException("it did not support this field method " + field);
}
}
}
示例12: rangeQuery
import org.elasticsearch.search.aggregations.AggregationBuilder; //导入依赖的package包/类
@SuppressWarnings("rawtypes")
public static void rangeQuery(Client client ) {
SearchResponse res = null;
AggregationBuilder agg =
AggregationBuilders
.range("agg")
.field("like")
.addUnboundedTo(3)
.addRange(3, 5)
.addUnboundedFrom(5);
res = client.prepareSearch("search_test")
.setTypes("article")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.addAggregation(agg)
.setFrom(0)
.setSize(0)
.execute().actionGet();
System.out.println(res);
// on shutdown
client.close();
}
示例13: histogramQuery
import org.elasticsearch.search.aggregations.AggregationBuilder; //导入依赖的package包/类
@SuppressWarnings("rawtypes")
public static void histogramQuery(Client client ) {
SearchResponse res = null;
AggregationBuilder agg =
AggregationBuilders
.histogram("agg")
.field("like")
.interval(2);
res = client.prepareSearch("search_test")
.setTypes("article")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.addAggregation(agg)
.setFrom(0)
.setSize(0)
.execute().actionGet();
System.out.println(res);
// on shutdown
client.close();
}
示例14: dateHistogramQuery
import org.elasticsearch.search.aggregations.AggregationBuilder; //导入依赖的package包/类
@SuppressWarnings("rawtypes")
public static void dateHistogramQuery(Client client ) {
SearchResponse res = null;
AggregationBuilder agg =
AggregationBuilders
.dateHistogram("agg")
.field("publish_date")
.interval(DateHistogramInterval.YEAR)
.minDocCount(1);
res = client.prepareSearch("search_test")
.setTypes("article")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.addAggregation(agg)
.setFrom(0)
.setSize(0)
.execute().actionGet();
System.out.println(res);
// on shutdown
client.close();
}
示例15: addMetrics
import org.elasticsearch.search.aggregations.AggregationBuilder; //导入依赖的package包/类
/**
* Adds a set of 'leaf aggregations' to the provided parent metric (i.e. count, sum, max etc)
* @param parentAgg
* @param metrics
* @param addCount
*/
@SuppressWarnings("rawtypes")
private void addMetrics(AggregationBuilder parentAgg, Heading heading, boolean addCount){
for(Column metric : heading.columns()){
if(metric.getOp() == Operation.AVG)
parentAgg.subAggregation(AggregationBuilders.avg(metric.getAggName()).field(metric.getColumn()));
else if(addCount && metric.getOp() == Operation.COUNT)
parentAgg.subAggregation(AggregationBuilders.count(metric.getAggName()));
else if(metric.getOp() == Operation.MAX)
parentAgg.subAggregation(AggregationBuilders.max(metric.getAggName()).field(metric.getColumn()));
else if(metric.getOp() == Operation.MIN)
parentAgg.subAggregation(AggregationBuilders.min(metric.getAggName()).field(metric.getColumn()));
else if(metric.getOp() == Operation.SUM)
parentAgg.subAggregation(AggregationBuilders.sum(metric.getAggName()).field(metric.getColumn()));
}
}