org.elasticsearch.search.aggregations.bucket.nested.InternalNested Java Examples
The following examples show how to use
org.elasticsearch.search.aggregations.bucket.nested.InternalNested.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EsSearchResponse.java From mall with Apache License 2.0 | 6 votes |
/** * 获得属性的聚合 * * @param aggregation 属性的聚合 * @return 返回属性的聚合 */ private static Map<String, List<String>> getAttributes(Aggregation aggregation) { Map<String, List<String>> attributes = new HashMap<>(); if (Objects.isNull(aggregation)) { return attributes; } InternalNested nested = (InternalNested) aggregation; if (CollectionUtils.isEmpty(nested.getAggregations().asList())) { return attributes; } StringTerms stringTerms = (StringTerms) nested.getAggregations().asList().get(0); stringTerms.getBuckets().stream().forEach(bucket -> attributes.put((String) bucket.getKey(), getStringTerms(bucket.getAggregations().asList().get(0)))); return attributes; }
Example #2
Source File: AggregationTest.java From elasticsearch-sql with Apache License 2.0 | 6 votes |
@Test public void reverseAnotherNestedGroupByOnNestedFieldWithFilterTestWithReverseNestedNoPath() throws Exception { Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/nestedType GROUP BY nested(message.info),filter('myFilter',message.info = 'a'),reverse_nested(comment.data,'~comment')", TEST_INDEX_NESTED_TYPE)); InternalNested nested = result.get("message.info@NESTED"); InternalFilter filter = nested.getAggregations().get("myFilter@FILTER"); Terms infos = filter.getAggregations().get("message.info"); Assert.assertEquals(1,infos.getBuckets().size()); for(Terms.Bucket bucket : infos.getBuckets()) { InternalReverseNested reverseNested = bucket.getAggregations().get("comment.data@NESTED_REVERSED"); InternalNested innerNested = reverseNested.getAggregations().get("comment.data@NESTED"); Terms terms = innerNested.getAggregations().get("comment.data"); Terms.Bucket internalBucket = terms.getBuckets().get(0); long count = ((ValueCount) internalBucket.getAggregations().get("COUNT(*)")).getValue(); String key = internalBucket.getKey().toString(); if(key.equalsIgnoreCase("ab")) { Assert.assertEquals(2, count); } else { throw new Exception(String.format("Unexpected key. expected: only a . found: %s", key)); } } }
Example #3
Source File: AggregationTest.java From elasticsearch-sql with Apache License 2.0 | 6 votes |
@Test public void groupByOnNestedFieldTest() throws Exception { Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/nestedType GROUP BY nested(message.info)", TEST_INDEX_NESTED_TYPE)); InternalNested nested = result.get("message.info@NESTED"); Terms infos = nested.getAggregations().get("message.info"); Assert.assertEquals(3,infos.getBuckets().size()); for(Terms.Bucket bucket : infos.getBuckets()) { String key = bucket.getKey().toString(); long count = ((ValueCount) bucket.getAggregations().get("COUNT(*)")).getValue(); if(key.equalsIgnoreCase("a")) { Assert.assertEquals(2, count); } else if(key.equalsIgnoreCase("c")) { Assert.assertEquals(2, count); } else if(key.equalsIgnoreCase("b")) { Assert.assertEquals(1, count); } else { throw new Exception(String.format("Unexpected key. expected: a OR b OR c . found: %s", key)); } } }
Example #4
Source File: AggregationTest.java From elasticsearch-sql with Apache License 2.0 | 6 votes |
@Test public void groupByOnNestedFieldWithFilterTest() throws Exception { Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/nestedType GROUP BY nested(message.info),filter('myFilter',message.info = 'a')", TEST_INDEX_NESTED_TYPE)); InternalNested nested = result.get("message.info@NESTED"); InternalFilter filter = nested.getAggregations().get("myFilter@FILTER"); Terms infos = filter.getAggregations().get("message.info"); Assert.assertEquals(1,infos.getBuckets().size()); for(Terms.Bucket bucket : infos.getBuckets()) { String key = bucket.getKey().toString(); long count = ((ValueCount) bucket.getAggregations().get("COUNT(*)")).getValue(); if(key.equalsIgnoreCase("a")) { Assert.assertEquals(2, count); } else { throw new Exception(String.format("Unexpected key. expected: only a . found: %s", key)); } } }
Example #5
Source File: AggregationTest.java From elasticsearch-sql with Apache License 2.0 | 6 votes |
@Test public void histogramOnNestedField() throws Exception { Aggregations result = query(String.format("select count(*) from %s/nestedType group by histogram('field'='message.dayOfWeek','nested'='message','interval'='2' , 'alias' = 'someAlias' )", TEST_INDEX_NESTED_TYPE)); InternalNested nested = result.get("message@NESTED"); Histogram histogram = nested.getAggregations().get("someAlias"); for(Histogram.Bucket bucket : histogram.getBuckets()){ long count = ((ValueCount) bucket.getAggregations().get("COUNT(*)")).getValue(); String key = ((Double)bucket.getKey()).intValue()+""; if(key.equals("0") || key.equals("4")){ Assert.assertEquals(2,count); } else if (key.equals("2")){ Assert.assertEquals(1,count); } else{ Assert.assertTrue("only 0 2 4 keys are allowed got:" + key,false); } } }
Example #6
Source File: AggregationTest.java From elasticsearch-sql with Apache License 2.0 | 6 votes |
@Test public void reverseToRootGroupByOnNestedFieldWithFilterTestWithReverseNestedAndEmptyPath() throws Exception { Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/nestedType GROUP BY nested(message.info),filter('myFilter',message.info = 'a'),reverse_nested(someField,'')", TEST_INDEX_NESTED_TYPE)); InternalNested nested = result.get("message.info@NESTED"); InternalFilter filter = nested.getAggregations().get("myFilter@FILTER"); Terms infos = filter.getAggregations().get("message.info"); Assert.assertEquals(1,infos.getBuckets().size()); for(Terms.Bucket bucket : infos.getBuckets()) { InternalReverseNested reverseNested = bucket.getAggregations().get("someField@NESTED"); Terms terms = reverseNested.getAggregations().get("someField"); Terms.Bucket internalBucket = terms.getBuckets().get(0); long count = ((ValueCount) internalBucket.getAggregations().get("COUNT(*)")).getValue(); String key = internalBucket.getKey().toString(); if(key.equalsIgnoreCase("b")) { Assert.assertEquals(2, count); } else { throw new Exception(String.format("Unexpected key. expected: only a . found: %s", key)); } } }
Example #7
Source File: AggregationTest.java From elasticsearch-sql with Apache License 2.0 | 6 votes |
@Test public void reverseToRootGroupByOnNestedFieldWithFilterTestWithReverseNestedNoPath() throws Exception { Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/nestedType GROUP BY nested(message.info),filter('myFilter',message.info = 'a'),reverse_nested(someField)", TEST_INDEX_NESTED_TYPE)); InternalNested nested = result.get("message.info@NESTED"); InternalFilter filter = nested.getAggregations().get("myFilter@FILTER"); Terms infos = filter.getAggregations().get("message.info"); Assert.assertEquals(1,infos.getBuckets().size()); for(Terms.Bucket bucket : infos.getBuckets()) { InternalReverseNested reverseNested = bucket.getAggregations().get("someField@NESTED"); Terms terms = reverseNested.getAggregations().get("someField"); Terms.Bucket internalBucket = terms.getBuckets().get(0); long count = ((ValueCount) internalBucket.getAggregations().get("COUNT(*)")).getValue(); String key = internalBucket.getKey().toString(); if(key.equalsIgnoreCase("b")) { Assert.assertEquals(2, count); } else { throw new Exception(String.format("Unexpected key. expected: only a . found: %s", key)); } } }
Example #8
Source File: AggregationTest.java From elasticsearch-sql with Apache License 2.0 | 5 votes |
@Test public void sumOnNestedField() throws Exception { Aggregations result = query(String.format("SELECT sum(nested(message.dayOfWeek)) as sumDays FROM %s/nestedType", TEST_INDEX_NESTED_TYPE)); InternalNested nested = result.get("message.dayOfWeek@NESTED"); Sum sum = nested.getAggregations().get("sumDays"); Assert.assertEquals(13.0,sum.getValue(),0.0001); }
Example #9
Source File: AggregationTest.java From elasticsearch-sql with Apache License 2.0 | 5 votes |
@Test public void reverseAnotherNestedGroupByOnNestedFieldWithFilterAndSumOnReverseNestedField() throws Exception { Aggregations result = query(String.format("SELECT sum(reverse_nested(comment.likes,'~comment')) bla FROM %s/nestedType GROUP BY nested(message.info),filter('myFilter',message.info = 'a')", TEST_INDEX_NESTED_TYPE)); InternalNested nested = result.get("message.info@NESTED"); InternalFilter filter = nested.getAggregations().get("myFilter@FILTER"); Terms infos = filter.getAggregations().get("message.info"); Assert.assertEquals(1,infos.getBuckets().size()); for(Terms.Bucket bucket : infos.getBuckets()) { InternalReverseNested reverseNested = bucket.getAggregations().get("comment.likes@NESTED_REVERSED"); InternalNested innerNested = reverseNested.getAggregations().get("comment.likes@NESTED"); InternalSum sum = innerNested.getAggregations().get("bla"); Assert.assertEquals(4.0,sum.getValue(),0.000001); } }
Example #10
Source File: AggregationTest.java From elasticsearch-sql with Apache License 2.0 | 5 votes |
@Test public void reverseAnotherNestedGroupByOnNestedFieldWithFilterTestWithReverseNestedOnHistogram() throws Exception { Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/nestedType GROUP BY nested(message.info),filter('myFilter',message.info = 'a'),histogram('field'='comment.likes','reverse_nested'='~comment','interval'='2' , 'alias' = 'someAlias' )", TEST_INDEX_NESTED_TYPE)); InternalNested nested = result.get("message.info@NESTED"); InternalFilter filter = nested.getAggregations().get("myFilter@FILTER"); Terms infos = filter.getAggregations().get("message.info"); Assert.assertEquals(1,infos.getBuckets().size()); for(Terms.Bucket bucket : infos.getBuckets()) { InternalReverseNested reverseNested = bucket.getAggregations().get("~comment@NESTED_REVERSED"); InternalNested innerNested = reverseNested.getAggregations().get("~comment@NESTED"); InternalHistogram histogram = innerNested.getAggregations().get("someAlias"); Assert.assertEquals(2, histogram.getBuckets().size()); } }
Example #11
Source File: AggregationTest.java From elasticsearch-sql with Apache License 2.0 | 5 votes |
@Test public void reverseToRootGroupByOnNestedFieldWithFilterAndSumOnReverseNestedField() throws Exception { Aggregations result = query(String.format("SELECT sum(reverse_nested(myNum)) bla FROM %s/nestedType GROUP BY nested(message.info),filter('myFilter',message.info = 'a')", TEST_INDEX_NESTED_TYPE)); InternalNested nested = result.get("message.info@NESTED"); InternalFilter filter = nested.getAggregations().get("myFilter@FILTER"); Terms infos = filter.getAggregations().get("message.info"); Assert.assertEquals(1,infos.getBuckets().size()); for(Terms.Bucket bucket : infos.getBuckets()) { InternalReverseNested reverseNested = bucket.getAggregations().get("myNum@NESTED"); InternalSum sum = reverseNested.getAggregations().get("bla"); Assert.assertEquals(5.0,sum.getValue(),0.000001); } }
Example #12
Source File: AggregationTest.java From elasticsearch-sql with Apache License 2.0 | 5 votes |
@Test public void reverseToRootGroupByOnNestedFieldWithFilterTestWithReverseNestedOnHistogram() throws Exception { Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/nestedType GROUP BY nested(message.info),filter('myFilter',message.info = 'a'),histogram('field'='myNum','reverse_nested'='','interval'='2' , 'alias' = 'someAlias' )", TEST_INDEX_NESTED_TYPE)); InternalNested nested = result.get("message.info@NESTED"); InternalFilter filter = nested.getAggregations().get("myFilter@FILTER"); Terms infos = filter.getAggregations().get("message.info"); Assert.assertEquals(1,infos.getBuckets().size()); for(Terms.Bucket bucket : infos.getBuckets()) { InternalReverseNested reverseNested = bucket.getAggregations().get("someAlias@NESTED"); InternalHistogram histogram = reverseNested.getAggregations().get("someAlias"); Assert.assertEquals(3, histogram.getBuckets().size()); } }
Example #13
Source File: AggregationTest.java From elasticsearch-sql with Apache License 2.0 | 5 votes |
@Test public void minOnNestedField() throws Exception { Aggregations result = query(String.format("SELECT min(nested(message.dayOfWeek)) as minDays FROM %s/nestedType", TEST_INDEX_NESTED_TYPE)); InternalNested nested = result.get("message.dayOfWeek@NESTED"); Min mins = nested.getAggregations().get("minDays"); Assert.assertEquals(1.0,mins.getValue(),0.0001); }
Example #14
Source File: EsProductServiceImpl.java From BigDataPlatform with GNU General Public License v3.0 | 4 votes |
/** * 将返回结果转换为对象 */ private EsProductRelatedInfo convertProductRelatedInfo(SearchResponse response) { EsProductRelatedInfo productRelatedInfo = new EsProductRelatedInfo(); Map<String, Aggregation> aggregationMap = response.getAggregations().getAsMap(); //设置品牌 Aggregation brandNames = aggregationMap.get("brandNames"); List<String> brandNameList = new ArrayList<>(); for(int i = 0; i<((Terms) brandNames).getBuckets().size(); i++){ brandNameList.add(((Terms) brandNames).getBuckets().get(i).getKeyAsString()); } productRelatedInfo.setBrandNames(brandNameList); //设置分类 Aggregation productCategoryNames = aggregationMap.get("productCategoryNames"); List<String> productCategoryNameList = new ArrayList<>(); for(int i=0;i<((Terms) productCategoryNames).getBuckets().size();i++){ productCategoryNameList.add(((Terms) productCategoryNames).getBuckets().get(i).getKeyAsString()); } productRelatedInfo.setProductCategoryNames(productCategoryNameList); //设置参数 Aggregation productAttrs = aggregationMap.get("allAttrValues"); List<LongTerms.Bucket> attrIds = ((LongTerms) ((InternalFilter) ((InternalNested) productAttrs).getProperty("productAttrs")).getProperty("attrIds")).getBuckets(); List<EsProductRelatedInfo.ProductAttr> attrList = new ArrayList<>(); for (Terms.Bucket attrId : attrIds) { EsProductRelatedInfo.ProductAttr attr = new EsProductRelatedInfo.ProductAttr(); attr.setAttrId((Long) attrId.getKey()); List<String> attrValueList = new ArrayList<>(); List<StringTerms.Bucket> attrValues = ((StringTerms) attrId.getAggregations().get("attrValues")).getBuckets(); List<StringTerms.Bucket> attrNames = ((StringTerms) attrId.getAggregations().get("attrNames")).getBuckets(); for (Terms.Bucket attrValue : attrValues) { attrValueList.add(attrValue.getKeyAsString()); } attr.setAttrValues(attrValueList); if(!CollectionUtils.isEmpty(attrNames)){ String attrName = attrNames.get(0).getKeyAsString(); attr.setAttrName(attrName); } attrList.add(attr); } productRelatedInfo.setProductAttrs(attrList); return productRelatedInfo; }
Example #15
Source File: EsProductServiceImpl.java From mall with Apache License 2.0 | 4 votes |
/** * 将返回结果转换为对象 */ private EsProductRelatedInfo convertProductRelatedInfo(SearchResponse response) { EsProductRelatedInfo productRelatedInfo = new EsProductRelatedInfo(); Map<String, Aggregation> aggregationMap = response.getAggregations().getAsMap(); //设置品牌 Aggregation brandNames = aggregationMap.get("brandNames"); List<String> brandNameList = new ArrayList<>(); for(int i = 0; i<((Terms) brandNames).getBuckets().size(); i++){ brandNameList.add(((Terms) brandNames).getBuckets().get(i).getKeyAsString()); } productRelatedInfo.setBrandNames(brandNameList); //设置分类 Aggregation productCategoryNames = aggregationMap.get("productCategoryNames"); List<String> productCategoryNameList = new ArrayList<>(); for(int i=0;i<((Terms) productCategoryNames).getBuckets().size();i++){ productCategoryNameList.add(((Terms) productCategoryNames).getBuckets().get(i).getKeyAsString()); } productRelatedInfo.setProductCategoryNames(productCategoryNameList); //设置参数 Aggregation productAttrs = aggregationMap.get("allAttrValues"); List<LongTerms.Bucket> attrIds = ((LongTerms) ((InternalFilter) ((InternalNested) productAttrs).getProperty("productAttrs")).getProperty("attrIds")).getBuckets(); List<EsProductRelatedInfo.ProductAttr> attrList = new ArrayList<>(); for (Terms.Bucket attrId : attrIds) { EsProductRelatedInfo.ProductAttr attr = new EsProductRelatedInfo.ProductAttr(); attr.setAttrId((Long) attrId.getKey()); List<String> attrValueList = new ArrayList<>(); List<StringTerms.Bucket> attrValues = ((StringTerms) attrId.getAggregations().get("attrValues")).getBuckets(); List<StringTerms.Bucket> attrNames = ((StringTerms) attrId.getAggregations().get("attrNames")).getBuckets(); for (Terms.Bucket attrValue : attrValues) { attrValueList.add(attrValue.getKeyAsString()); } attr.setAttrValues(attrValueList); if(!CollectionUtils.isEmpty(attrNames)){ String attrName = attrNames.get(0).getKeyAsString(); attr.setAttrName(attrName); } attrList.add(attr); } productRelatedInfo.setProductAttrs(attrList); return productRelatedInfo; }
Example #16
Source File: EsProductServiceImpl.java From macrozheng with Apache License 2.0 | 4 votes |
/** * 将返回结果转换为对象 */ private EsProductRelatedInfo convertProductRelatedInfo(SearchResponse response) { EsProductRelatedInfo productRelatedInfo = new EsProductRelatedInfo(); Map<String, Aggregation> aggregationMap = response.getAggregations().getAsMap(); //设置品牌 Aggregation brandNames = aggregationMap.get("brandNames"); List<String> brandNameList = new ArrayList<>(); for(int i = 0; i<((Terms) brandNames).getBuckets().size(); i++){ brandNameList.add(((Terms) brandNames).getBuckets().get(i).getKeyAsString()); } productRelatedInfo.setBrandNames(brandNameList); //设置分类 Aggregation productCategoryNames = aggregationMap.get("productCategoryNames"); List<String> productCategoryNameList = new ArrayList<>(); for(int i=0;i<((Terms) productCategoryNames).getBuckets().size();i++){ productCategoryNameList.add(((Terms) productCategoryNames).getBuckets().get(i).getKeyAsString()); } productRelatedInfo.setProductCategoryNames(productCategoryNameList); //设置参数 Aggregation productAttrs = aggregationMap.get("allAttrValues"); List<LongTerms.Bucket> attrIds = ((LongTerms) ((InternalFilter) ((InternalNested) productAttrs).getProperty("productAttrs")).getProperty("attrIds")).getBuckets(); List<EsProductRelatedInfo.ProductAttr> attrList = new ArrayList<>(); for (Terms.Bucket attrId : attrIds) { EsProductRelatedInfo.ProductAttr attr = new EsProductRelatedInfo.ProductAttr(); attr.setAttrId((Long) attrId.getKey()); List<String> attrValueList = new ArrayList<>(); List<StringTerms.Bucket> attrValues = ((StringTerms) attrId.getAggregations().get("attrValues")).getBuckets(); List<StringTerms.Bucket> attrNames = ((StringTerms) attrId.getAggregations().get("attrNames")).getBuckets(); for (Terms.Bucket attrValue : attrValues) { attrValueList.add(attrValue.getKeyAsString()); } attr.setAttrValues(attrValueList); if(!CollectionUtils.isEmpty(attrNames)){ String attrName = attrNames.get(0).getKeyAsString(); attr.setAttrName(attrName); } attrList.add(attr); } productRelatedInfo.setProductAttrs(attrList); return productRelatedInfo; }
Example #17
Source File: EsProductServiceImpl.java From mall-swarm with Apache License 2.0 | 4 votes |
/** * 将返回结果转换为对象 */ private EsProductRelatedInfo convertProductRelatedInfo(SearchResponse response) { EsProductRelatedInfo productRelatedInfo = new EsProductRelatedInfo(); Map<String, Aggregation> aggregationMap = response.getAggregations().getAsMap(); //设置品牌 Aggregation brandNames = aggregationMap.get("brandNames"); List<String> brandNameList = new ArrayList<>(); for(int i = 0; i<((Terms) brandNames).getBuckets().size(); i++){ brandNameList.add(((Terms) brandNames).getBuckets().get(i).getKeyAsString()); } productRelatedInfo.setBrandNames(brandNameList); //设置分类 Aggregation productCategoryNames = aggregationMap.get("productCategoryNames"); List<String> productCategoryNameList = new ArrayList<>(); for(int i=0;i<((Terms) productCategoryNames).getBuckets().size();i++){ productCategoryNameList.add(((Terms) productCategoryNames).getBuckets().get(i).getKeyAsString()); } productRelatedInfo.setProductCategoryNames(productCategoryNameList); //设置参数 Aggregation productAttrs = aggregationMap.get("allAttrValues"); List<LongTerms.Bucket> attrIds = ((LongTerms) ((InternalFilter) ((InternalNested) productAttrs).getProperty("productAttrs")).getProperty("attrIds")).getBuckets(); List<EsProductRelatedInfo.ProductAttr> attrList = new ArrayList<>(); for (Terms.Bucket attrId : attrIds) { EsProductRelatedInfo.ProductAttr attr = new EsProductRelatedInfo.ProductAttr(); attr.setAttrId((Long) attrId.getKey()); List<String> attrValueList = new ArrayList<>(); List<StringTerms.Bucket> attrValues = ((StringTerms) attrId.getAggregations().get("attrValues")).getBuckets(); List<StringTerms.Bucket> attrNames = ((StringTerms) attrId.getAggregations().get("attrNames")).getBuckets(); for (Terms.Bucket attrValue : attrValues) { attrValueList.add(attrValue.getKeyAsString()); } attr.setAttrValues(attrValueList); if(!CollectionUtils.isEmpty(attrNames)){ String attrName = attrNames.get(0).getKeyAsString(); attr.setAttrName(attrName); } attrList.add(attr); } productRelatedInfo.setProductAttrs(attrList); return productRelatedInfo; }