本文整理汇总了Java中org.elasticsearch.search.aggregations.AggregationBuilders类的典型用法代码示例。如果您正苦于以下问题:Java AggregationBuilders类的具体用法?Java AggregationBuilders怎么用?Java AggregationBuilders使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
AggregationBuilders类属于org.elasticsearch.search.aggregations包,在下文中一共展示了AggregationBuilders类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: selectTermAll
import org.elasticsearch.search.aggregations.AggregationBuilders; //导入依赖的package包/类
public String selectTermAll(String indexs,String types,String field,String value){
try {
if(StringUtil.isEmpty(indexs))indexs="_all";
if(xclient==null){
init();
}
SearchSourceBuilder search = new SearchSourceBuilder();
if(!StringUtil.isEmpty(field)&&!StringUtil.isEmpty(value)&&!(field.matches(regex)||field.matches(value))){
search.query(QueryBuilders.termQuery(field, value));
}
search.aggregation(AggregationBuilders.terms("data").field(field+".keyword"));
search.explain(false);
SearchRequest request = new SearchRequest();
request.searchType(SearchType.DFS_QUERY_THEN_FETCH);
request.source(search);
request.indices(indexs.split(","));
request.types(types.split(","));
SearchResponse response = xclient.search(request);
return response.toString();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
示例2: calculateStats
import org.elasticsearch.search.aggregations.AggregationBuilders; //导入依赖的package包/类
public List<DateHistogramValue> calculateStats(String sourceUrl) {
BoolQueryBuilder filter = QueryBuilders.boolQuery()
.must(QueryBuilders.rangeQuery("created").gte("now-1M"))
.must(QueryBuilders.termQuery("source", sourceUrl));
SearchResponse response = getConnection().getClient()
.prepareSearch(getIndex())
.setTypes(getType())
.setSearchType(SearchType.DEFAULT)
.setQuery(filter)
.addAggregation(AggregationBuilders
.dateHistogram("urls_over_time")
.field("created")
.format("yyyy-MM-dd")
.dateHistogramInterval(DateHistogramInterval.DAY))
.setSize(0)
.setFetchSource(true)
.setExplain(false)
.execute()
.actionGet();
InternalDateHistogram hits = response.getAggregations().get("urls_over_time");
return hits.getBuckets().stream()
.map(b -> new DateHistogramValue(b.getKeyAsString(), b.getDocCount()))
.collect(Collectors.toList());
}
示例3: selectMatchAll
import org.elasticsearch.search.aggregations.AggregationBuilders; //导入依赖的package包/类
public String selectMatchAll(String indexs,String types,String field,String value){
try {
if(StringUtil.isEmpty(indexs))indexs="_all";
if(xclient==null){
init();
}
SearchSourceBuilder search = new SearchSourceBuilder();
if(!StringUtil.isEmpty(field)&&!StringUtil.isEmpty(value)&&!(field.matches(regex)||field.matches(value))){
search.query(QueryBuilders.matchQuery(field, value));
}
search.aggregation(AggregationBuilders.terms("data").field(field+".keyword"));
search.explain(false);
SearchRequest request = new SearchRequest();
request.searchType(SearchType.DFS_QUERY_THEN_FETCH);
request.source(search);
request.indices(indexs.split(","));
request.types(types.split(","));
SearchResponse response = xclient.search(request);
return response.toString();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
示例4: geoBounds
import org.elasticsearch.search.aggregations.AggregationBuilders; //导入依赖的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;
}
示例5: getElasticsearchGeohashAggregations
import org.elasticsearch.search.aggregations.AggregationBuilders; //导入依赖的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.AggregationBuilders; //导入依赖的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: testSingleValuedField
import org.elasticsearch.search.aggregations.AggregationBuilders; //导入依赖的package包/类
public void testSingleValuedField() {
SearchResponse rsp = client().prepareSearch("idx").addAggregation(
AggregationBuilders.ipRange("my_range")
.field("ip")
.addUnboundedTo("192.168.1.0")
.addRange("192.168.1.0", "192.168.1.10")
.addUnboundedFrom("192.168.1.10")).get();
assertSearchResponse(rsp);
Range range = rsp.getAggregations().get("my_range");
assertEquals(3, range.getBuckets().size());
Range.Bucket bucket1 = range.getBuckets().get(0);
assertNull(bucket1.getFrom());
assertEquals("192.168.1.0", bucket1.getTo());
assertEquals(0, bucket1.getDocCount());
Range.Bucket bucket2 = range.getBuckets().get(1);
assertEquals("192.168.1.0", bucket2.getFrom());
assertEquals("192.168.1.10", bucket2.getTo());
assertEquals(1, bucket2.getDocCount());
Range.Bucket bucket3 = range.getBuckets().get(2);
assertEquals("192.168.1.10", bucket3.getFrom());
assertNull(bucket3.getTo());
assertEquals(2, bucket3.getDocCount());
}
示例8: testMultiValuedField
import org.elasticsearch.search.aggregations.AggregationBuilders; //导入依赖的package包/类
public void testMultiValuedField() {
SearchResponse rsp = client().prepareSearch("idx").addAggregation(
AggregationBuilders.ipRange("my_range")
.field("ips")
.addUnboundedTo("192.168.1.0")
.addRange("192.168.1.0", "192.168.1.10")
.addUnboundedFrom("192.168.1.10")).get();
assertSearchResponse(rsp);
Range range = rsp.getAggregations().get("my_range");
assertEquals(3, range.getBuckets().size());
Range.Bucket bucket1 = range.getBuckets().get(0);
assertNull(bucket1.getFrom());
assertEquals("192.168.1.0", bucket1.getTo());
assertEquals(1, bucket1.getDocCount());
Range.Bucket bucket2 = range.getBuckets().get(1);
assertEquals("192.168.1.0", bucket2.getFrom());
assertEquals("192.168.1.10", bucket2.getTo());
assertEquals(1, bucket2.getDocCount());
Range.Bucket bucket3 = range.getBuckets().get(2);
assertEquals("192.168.1.10", bucket3.getFrom());
assertNull(bucket3.getTo());
assertEquals(2, bucket3.getDocCount());
}
示例9: testIpMask
import org.elasticsearch.search.aggregations.AggregationBuilders; //导入依赖的package包/类
public void testIpMask() {
SearchResponse rsp = client().prepareSearch("idx").addAggregation(
AggregationBuilders.ipRange("my_range")
.field("ips")
.addMaskRange("::/0")
.addMaskRange("0.0.0.0/0")
.addMaskRange("2001:db8::/64")).get();
assertSearchResponse(rsp);
Range range = rsp.getAggregations().get("my_range");
assertEquals(3, range.getBuckets().size());
Range.Bucket bucket1 = range.getBuckets().get(0);
assertEquals("::/0", bucket1.getKey());
assertEquals(3, bucket1.getDocCount());
Range.Bucket bucket2 = range.getBuckets().get(1);
assertEquals("0.0.0.0/0", bucket2.getKey());
assertEquals(2, bucket2.getDocCount());
Range.Bucket bucket3 = range.getBuckets().get(2);
assertEquals("2001:db8::/64", bucket3.getKey());
assertEquals(1, bucket3.getDocCount());
}
示例10: testPartiallyUnmapped
import org.elasticsearch.search.aggregations.AggregationBuilders; //导入依赖的package包/类
public void testPartiallyUnmapped() {
SearchResponse rsp = client().prepareSearch("idx", "idx_unmapped").addAggregation(
AggregationBuilders.ipRange("my_range")
.field("ip")
.addUnboundedTo("192.168.1.0")
.addRange("192.168.1.0", "192.168.1.10")
.addUnboundedFrom("192.168.1.10")).get();
assertSearchResponse(rsp);
Range range = rsp.getAggregations().get("my_range");
assertEquals(3, range.getBuckets().size());
Range.Bucket bucket1 = range.getBuckets().get(0);
assertNull(bucket1.getFrom());
assertEquals("192.168.1.0", bucket1.getTo());
assertEquals(0, bucket1.getDocCount());
Range.Bucket bucket2 = range.getBuckets().get(1);
assertEquals("192.168.1.0", bucket2.getFrom());
assertEquals("192.168.1.10", bucket2.getTo());
assertEquals(1, bucket2.getDocCount());
Range.Bucket bucket3 = range.getBuckets().get(2);
assertEquals("192.168.1.10", bucket3.getFrom());
assertNull(bucket3.getTo());
assertEquals(2, bucket3.getDocCount());
}
示例11: testUnmapped
import org.elasticsearch.search.aggregations.AggregationBuilders; //导入依赖的package包/类
public void testUnmapped() {
SearchResponse rsp = client().prepareSearch("idx_unmapped").addAggregation(
AggregationBuilders.ipRange("my_range")
.field("ip")
.addUnboundedTo("192.168.1.0")
.addRange("192.168.1.0", "192.168.1.10")
.addUnboundedFrom("192.168.1.10")).get();
assertSearchResponse(rsp);
Range range = rsp.getAggregations().get("my_range");
assertEquals(3, range.getBuckets().size());
Range.Bucket bucket1 = range.getBuckets().get(0);
assertNull(bucket1.getFrom());
assertEquals("192.168.1.0", bucket1.getTo());
assertEquals(0, bucket1.getDocCount());
Range.Bucket bucket2 = range.getBuckets().get(1);
assertEquals("192.168.1.0", bucket2.getFrom());
assertEquals("192.168.1.10", bucket2.getTo());
assertEquals(0, bucket2.getDocCount());
Range.Bucket bucket3 = range.getBuckets().get(2);
assertEquals("192.168.1.10", bucket3.getFrom());
assertNull(bucket3.getTo());
assertEquals(0, bucket3.getDocCount());
}
示例12: testIssue5930
import org.elasticsearch.search.aggregations.AggregationBuilders; //导入依赖的package包/类
public void testIssue5930() throws IOException {
assertAcked(client().admin().indices().prepareCreate(INDEX).addMapping(TYPE, jsonBuilder().startObject()
.startObject(TYPE).startObject("properties")
.startObject(FIELD)
.field("type", "completion")
.endObject()
.endObject().endObject()
.endObject()).get());
String string = "foo bar";
client().prepareIndex(INDEX, TYPE, "1").setSource(jsonBuilder()
.startObject()
.field(FIELD, string)
.endObject()
).setRefreshPolicy(IMMEDIATE).get();
try {
client().prepareSearch(INDEX).addAggregation(AggregationBuilders.terms("suggest_agg").field(FIELD)
.collectMode(randomFrom(SubAggCollectionMode.values()))).execute().actionGet();
// Exception must be thrown
assertFalse(true);
} catch (SearchPhaseExecutionException e) {
assertThat(e.toString(), containsString("Fielddata is not supported on field [" + FIELD + "] of type [completion]"));
}
}
示例13: testNewSearchPhaseResults
import org.elasticsearch.search.aggregations.AggregationBuilders; //导入依赖的package包/类
public void testNewSearchPhaseResults() {
for (int i = 0; i < 10; i++) {
int expectedNumResults = randomIntBetween(1, 10);
int bufferSize = randomIntBetween(2, 10);
SearchRequest request = new SearchRequest();
final boolean hasAggs;
if ((hasAggs = randomBoolean())) {
request.source(new SearchSourceBuilder().aggregation(AggregationBuilders.avg("foo")));
}
request.setBatchedReduceSize(bufferSize);
InitialSearchPhase.SearchPhaseResults<QuerySearchResultProvider> consumer
= searchPhaseController.newSearchPhaseResults(request, expectedNumResults);
if (hasAggs && expectedNumResults > bufferSize) {
assertThat("expectedNumResults: " + expectedNumResults + " bufferSize: " + bufferSize,
consumer, instanceOf(SearchPhaseController.QueryPhaseResultConsumer.class));
} else {
assertThat("expectedNumResults: " + expectedNumResults + " bufferSize: " + bufferSize,
consumer, not(instanceOf(SearchPhaseController.QueryPhaseResultConsumer.class)));
}
}
}
示例14: selectMatchAll
import org.elasticsearch.search.aggregations.AggregationBuilders; //导入依赖的package包/类
public String selectMatchAll(String indexs,String types,String field,String value){
try {
if(client==null){
init();
}
SearchRequestBuilder request = client.prepareSearch(indexs.split(",")).setTypes(types.split(","));
request.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
request.setQuery(QueryBuilders.matchQuery(field, value));
request.highlighter(new HighlightBuilder().field(field));
request.addAggregation(AggregationBuilders.terms("data").field(field+".keyword"));
request.setExplain(false);
SearchResponse response = request.get();
return response.toString();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
示例15: selectTermAll
import org.elasticsearch.search.aggregations.AggregationBuilders; //导入依赖的package包/类
public String selectTermAll(String indexs,String types,String field,String value){
try {
if(client==null){
init();
}
SearchRequestBuilder request = client.prepareSearch(indexs.split(",")).setTypes(types.split(","));
request.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
request.setQuery(QueryBuilders.termQuery(field, value));
request.highlighter(new HighlightBuilder().field(field));
request.addAggregation(AggregationBuilders.terms("data").field(field+".keyword"));
request.setExplain(false);
SearchResponse response = request.get();
return response.toString();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}