ElasticSeacher 基本查詢 方法一
阿新 • • 發佈:2018-11-25
分組後排序
QueryBuilder bool = QueryBuilders.boolQuery().must(QueryBuilders.termQuery("IsPresent",0));
//true升序 false是降序
TermsAggregationBuilder teamAgg = AggregationBuilders.terms("pos_count").field("NewsId").order(Terms.Order.count(true)).size(Integer.MAX_VALUE);
SearchRequestBuilder builder = client.prepareSearch (esIndexConfig.getIndex()).setTypes(esIndexConfig.getType());
SearchResponse response = builder.setQuery(bool).addAggregation(teamAgg).execute().actionGet();
// SearchResponse response= builder.setQuery(bool).setSize(0).execute().actionGet();
System.out.println(response.getHits().totalHits );
Terms terms = response.getAggregations().get("pos_count");
/**得到的是分組數*/
System.out.println(terms.getBuckets().size());
Iterator<?> classBucketIt = terms.getBuckets().iterator();
SearchHits searchHit=response.getHits();
while(classBucketIt.hasNext ())
{
Terms.Bucket classBucket =(Terms.Bucket) classBucketIt.next();
System.out.println( "NewId: " + classBucket.getDocCount() +"調結果。");
}
多重組合排序並取得排序後的資料集
先經過VendorId欄位排序 後經過NewsId排序 top是用來取得結果集,沒有top只能得到
這種結果
17088791年級17088791班有30個學生。
17088816年級17088816班有10個學生。
17086707年級17086707班有9個學生。
17089274年級17089274班有9個學生。
17089275年級17089275班有8個學生。
17089270年級17089270班有4個學生。
17089198年級17089198班有2個學生。
17089199年級17089199班有2個學生。
17089200年級17089200班有2個學生。
17089265年級17089265班有2個學生。
17089267年級17089267班有2個學生。
17088796年級17088796班有1個學生。
17088798年級17088798班有1個學生。
17088799年級17088799班有1個學生。
17089266年級17089266班有1個學生。
QueryBuilder bool = QueryBuilders.boolQuery().must(QueryBuilders.termQuery("IsPresent", 0));
TermsAggregationBuilder teamAgg = AggregationBuilders.terms("VendorId").field("VendorId").order(Terms.Order.count(false)).size(Integer.MAX_VALUE);
TermsAggregationBuilder team = AggregationBuilders.terms("NewsId").field("NewsId").order(Terms.Order.count(false)).size(Integer.MAX_VALUE);
team.subAggregation(AggregationBuilders.topHits("top").size(Integer.MAX_VALUE));
teamAgg.subAggregation(team);
SearchRequestBuilder builder = client.prepareSearch(esIndexConfig.getIndex()).setTypes(esIndexConfig.getType());
SearchResponse response = builder.setQuery(bool).addAggregation(teamAgg).setSize(100).execute().actionGet();
// SearchResponse response= builder.setQuery(bool).setSize(0).execute().actionGet();
System.out.println(response.getHits().totalHits);
Terms terms = response.getAggregations().get("VendorId");
/**得到的是分組數*/
System.out.println("分組 +VendorId"+terms.getBuckets().size());
Iterator<?> classBucketIt = terms.getBuckets().iterator();
SearchHits searchHit = response.getHits();
for(Terms.Bucket entry:terms.getBuckets()){
Terms terTwo=entry.getAggregations().get("NewsId");
System.out.println("分組 +NewsId"+terms.getBuckets().size());
for(Terms.Bucket bucket:terTwo.getBuckets()){
System.out.println(entry.getKeyAsString()+"____"+bucket.getKeyAsString()+"有"+bucket.getDocCount());
System.out.println(bucket.getAggregations().asList().size());
TopHits topHits = bucket.getAggregations().get("top");
for(SearchHit hit:topHits.getHits()){
System.out.println(hit.getSourceAsString());
}
}
}
}
結果如下
{"VendorId":50002218,"NewsId":17086707,"CarId":103302,"CarImage":"http://img1.bitautoimg.com/autoalbum/files/20150810/534/21252753412596_4198300_3.jpg","IsPresent":0,"PresentPrice":0.00,"DealerId":50002218,"DealerName":"易車互聯公司","BusinessModelId":2,"VendorTel":"18001353123$010-65432123$010-65432124","PId":2,"PName":"北京","CityId":201,"CityName":"北京","SaleRegionType":9999,"Discount":6.00,"FavorablePrice":1.00,"CarReferPrice":16.9800,"SalePrice":15.9800,"NId":17086707,"VendorPrice":15.5000,"StoreState":1,"NewCarID":103302,"CarName":"2.0L 手動 兩驅 時尚型","CarTransmissionType":"手動","CarYearType":2013,"CsId":1574,"CsShowName":"全新途勝","CsLevelId":64,"CpCountry":1,"CbName":"北京現代","CbId":10062,"CMBName":"現代","CMBID":13,"CarColor":"","AreaId":110108,"AreaName":"海淀區","JiangFu":0.0588928150765606595995288575,"MobileDimWeightSort":60001,"PCDimWeightSort":64001,"MediaIds":"1,2,3,4,5,11,16,17,18,20,22","MediaIdsArr":[1,2,3,4,5,11,16,17,18,20,22],"CityNameList":"","ProvinceSpell":"beijing","CitySpell":"beijing","NewsPubTime":"2018-02-11T10:26:00+08:00","StartDateTime":"2018-02-11T00:00:00+08:00","EndDateTime":"2018-03-12T00:00:00+08:00","LastModifyTime":"2018-02-11T10:26:00+08:00","DealerLastmodifyTime":"2017-04-25T18:03:00+08:00","TimeStamp":"2018-08-16T14:39:07.8177881+08:00"}
{"VendorId":50002218,"NewsId":17086707,"CarId":116436,"CarImage":"http://img1.bitautoimg.com/autoalbum/files/20150810/534/21252753412596_4198300_3.jpg","IsPresent":0,"PresentPrice":0.00,"DealerId":50002218,"DealerName":"易車互聯公司","BusinessModelId":2,"VendorTel":"18001353123$010-65432123$010-65432124","PId":2,"PName":"北京","CityId":201,"CityName":"北京","SaleRegionType":9999,"Discount":5.00,"FavorablePrice":1.00,"CarReferPrice":19.6900,"SalePrice":18.6900,"NId":17086707,"VendorPrice":20.0000,"StoreState":1,"NewCarID":116436,"CarName":"1.6T 雙離合 領先型 兩驅","CarTransmissionType":"雙離合","CarYearType":2015,"CsId":1574,"CsShowName":"全新途勝","CsLevelId":64,"CpCountry":1,"CbName":"北京現代","CbId":10062,"CMBName":"現代","CMBID":13,"CarColor":"","AreaId":110108,"AreaName":"海淀區","JiangFu":0.0507872016251904520060944642,"MobileDimWeightSort":60001,"PCDimWeightSort":64001,"MediaIds":"1,2,3,4,5,11,16,17,18,20,22","MediaIdsArr":[1,2,3,4,5,11,16,17,18,20,22],"CityNameList":"","ProvinceSpell":"beijing","CitySpell":"beijing","NewsPubTime":"2018-02-11T10:26:00+08:00","StartDateTime":"2018-02-11T00:00:00+08:00","EndDateTime":"2018-03-12T00:00:00+08:00","LastModifyTime":"2018-02-11T10:26:00+08:00","DealerLastmodifyTime":"2017-04-25T18:03:00+08:00","TimeStamp":"2018-08-16T14:39:07.8177881+08:00"}
{"VendorId":50002218,"NewsId":17086707,"CarId":116446,"CarImage":"http://img1.bitautoimg.com/autoalbum/files/20150810/534/21252753412596_4198300_3.jpg","IsPresent":0,"PresentPrice":0.00,"DealerId":50002218,"DealerName":"易車互聯公司","BusinessModelId":2,"VendorTel":"18001353123$010-65432123$010-65432124","PId":2,"PName":"北京","CityId":201,"CityName":"北京","SaleRegionType":9999,"Discount":6.00,"FavorablePrice":1.00,"CarReferPrice":17.9900,"SalePrice":16.9900,"NId":17086707,"VendorPrice":18.0000,"StoreState":1,"NewCarID":116446,"CarName":"1.6T 雙離合 智慧型 兩驅","CarTransmissionType":"雙離合","CarYearType":2015,"CsId":1574,"CsShowName":"全新途勝","CsLevelId":64,"CpCountry":1,"CbName":"北京現代","CbId":10062,"CMBName":"現代","CMBID":13,"CarColor":"","AreaId":110108,"AreaName":"海淀區","JiangFu":0.0555864369093941078376876042,"MobileDimWeightSort":60001,"PCDimWeightSort":64001,"MediaIds":"1,2,3,4,5,11,16,17,18,20,22","MediaIdsArr":[1,2,3,4,5,11,16,17,18,20,22],"CityNameList":"","ProvinceSpell":"beijing","CitySpell":"beijing","NewsPubTime":"2018-02-11T10:26:00+08:00","StartDateTime":"2018-02-11T00:00:00+08:00","EndDateTime":"2018-03-12T00:00:00+08:00","LastModifyTime":"2018-02-11T10:26:00+08:00","DealerLastmodifyTime":"2017-04-25T18:03:00+08:00","TimeStamp":"2018-08-16T14:39:07.8177881+08:00"}
{"VendorId":50002218,"NewsId":17086707,"CarId":116451,"CarImage":"http://img1.bitautoimg.com/autoalbum/files/20150810/534/21252753412596_4198300_3.jpg","IsPresent":0,"PresentPrice":0.00,"DealerId":50002218,"DealerName":"易車互聯公司","BusinessModelId":2,"VendorTel":"18001353123$010-65432123$010-65432124","PId":2,"PName":"北京","CityId":201,"CityName":"北京","SaleRegionType":9999,"Discount":6.00,"FavorablePrice":1.00,"CarReferPrice":16.4900,"SalePrice":15.4900,"NId":17086707,"VendorPrice":16.0000,"StoreState":1,"NewCarID":116451,"CarName":"2.0L 自動 舒適型 兩驅","CarTransmissionType":"手自一體","CarYearType":2015,"CsId":1574,"CsShowName":"全新途勝","CsLevelId":64,"CpCountry":1,"CbName":"北京現代","CbId":10062,"CMBName":"現代","CMBID":13,"CarColor":"","AreaId":110108,"AreaName":"海淀區","JiangFu":0.06064281382656155245603396,"MobileDimWeightSort":60001,"PCDimWeightSort":64001,"MediaIds":"1,2,3,4,5,11,16,17,18,20,22","MediaIdsArr":[1,2,3,4,5,11,16,17,18,20,22],"CityNameList":"","ProvinceSpell":"beijing","CitySpell":"beijing","NewsPubTime":"2018-02-11T10:26:00+08:00","StartDateTime":"2018-02-11T00:00:00+08:00","EndDateTime":"2018-03-12T00:00:00+08:00","LastModifyTime":"2018-02-11T10:26:00+08:00","DealerLastmodifyTime":"2017-04-25T18:03:00+08:00","TimeStamp":"2018-08-16T14:39:07.8177881+08:00"}
{"VendorId":50002218,"NewsId":17086707,"CarId":116452,"CarImage":"http://img1.bitautoimg.com/autoalbum/files/20150810/534/21252753412596_4198300_3.jpg","IsPresent":0,"PresentPrice":0.00,"DealerId":50002218,"DealerName":"易車互聯公司","BusinessModelId":2,"VendorTel":"18001353123$010-65432123$010-65432124","PId":2,"PName":"北京","CityId":201,"CityName":"北京","SaleRegionType":9999,"Discount":6.00,"FavorablePrice":1.00,"CarReferPrice":15.9900,"SalePrice":14.9900,"NId":17086707,"VendorPrice":15.9900,"StoreState":1,"NewCarID":116452,"CarName":"2.0L 手動 舒適型 兩驅","CarTransmissionType":"手動","CarYearType":2015,"CsId":1574,"CsShowName":"全新途勝","CsLevelId":64,"CpCountry":1,"CbName":"北京現代","CbId":10062,"CMBName":"現代","CMBID":13,"CarColor":"","AreaId":110108,"AreaName":"海淀區","JiangFu":0.0625390869293308317698561601,"MobileDimWeightSort":60001,"PCDimWeightSort":64001,"MediaIds":"1,2,3,4,5,11,16,17,18,20,22","MediaIdsArr":[1,2,3,4,5,11,16,17,18,20,22],"CityNameList":"","ProvinceSpell":"beijing","CitySpell":"beijing","NewsPubTime":"2018-02-11T10:26:00+08:00","StartDateTime":"2018-02-11T00:00:00+08:00","EndDateTime":"2018-03-12T00:00:00+08:00","LastModifyTime":"2018-02-11T10:26:00+08:00","DealerLastmodifyTime":"2017-04-25T18:03:00+08:00","TimeStamp":"2018-08-16T14:39:07.8177881+08:00"}
{"VendorId":50002218,"NewsId":17086707,"CarId":116091,"CarImage":"http://img1.bitautoimg.com/autoalbum/files/20150810/534/21252753412596_4198300_3.jpg","IsPresent":0,"PresentPrice":0.00,"DealerId":50002218,"DealerName":"易車互聯公司","BusinessModelId":2,"VendorTel":"18001353123$010-65432123$010-65432124","PId":2,"PName":"北京","CityId":201,"CityName":"北京","SaleRegionType":9999,"Discount":4.00,"FavorablePrice":1.00,"CarReferPrice":23.9900,"SalePrice":22.9900,"NId":17086707,"VendorPrice":24.0000,"StoreState":1,"NewCarID":116091,"CarName":"1.6T 雙離合 旗艦型 四驅","CarTransmissionType":"雙離合","CarYearType":2015,"CsId":1574,"CsShowName":"全新途勝","CsLevelId":64,"CpCountry":1,"CbName":"北京現代","CbId":10062,"CMBName":"現代","CMBID":13,"CarColor":"","AreaId":110108,"AreaName":"海淀區","JiangFu":0.0416840350145894122551062943,"MobileDimWeightSort":60001,"PCDimWeightSort":64001,"MediaIds":"1,2,3,4,5,11,16,17,18,20,22","MediaIdsArr":[1,2,3,4,5,11,16,17,18,20,22],"CityNameList":"","ProvinceSpell":"beijing","CitySpell":"beijing","NewsPubTime":"2018-02-11T10:26:00+08:00","StartDateTime":"2018-02-11T00:00:00+08:00","EndDateTime":"2018-03-12T00:00:00+08:00","LastModifyTime":"2018-02-11T10:26:00+08:00","DealerLastmodifyTime":"2017-04-25T18:03:00+08:00","TimeStamp":"2018-08-16T14:39:07.8177881+08:00"}
{"VendorId":50002218,"NewsId":17086707,"CarId":116449,"CarImage":"http://img1.bitautoimg.com/autoalbum/files/20150810/534/21252753412596_4198300_3.jpg","IsPresent":0,"PresentPrice":0.00,"DealerId":50002218,"DealerName":"易車互聯公司","BusinessModelId":2,"VendorTel":"18001353123$010-65432123$010-65432124","PId":2,"PName":"北京","CityId":201,"CityName":"北京","SaleRegionType":9999,"Discount":6.00,"FavorablePrice":1.00,"CarReferPrice":17.4900,"SalePrice":16.4900,"NId":17086707,"VendorPrice":17.4900,"StoreState":1,"NewCarID":116449,"CarName":"2.0L 自動 智慧型 兩驅","CarTransmissionType":"手自一體","CarYearType":2015,"CsId":1574,"CsShowName":"全新途勝","CsLevelId":64,"CpCountry":1,"CbName":"北京現代","CbId":10062,"CMBName":"現代","CMBID":13,"CarColor":"","AreaId":110108,"AreaName":"海淀區","JiangFu":0.0571755288736420811892510006,"MobileDimWeightSort":60001,"PCDimWeightSort":64001,"MediaIds":"1,2,3,4,5,11,16,17,18,20,22","MediaIdsArr":[1,2,3,4,5,11,16,17,18,20,22],"CityNameList":"","ProvinceSpell":"beijing","CitySpell":"beijing","NewsPubTime":"2018-02-11T10:26:00+08:00","StartDateTime":"2018-02-11T00:00:00+08:00","EndDateTime":"2018-03-12T00:00:00+08:00","LastModifyTime":"2018-02-11T10:26:00+08:00","DealerLastmodifyTime":"2017-04-25T18:03:00+08:00","TimeStamp":"2018-08-16T14:39:07.8177881+08:00"}
{"VendorId":50002218,"NewsId":17086707,"CarId":116433,"CarImage":"http://img1.bitautoimg.com/autoalbum/files/20150810/534/21252753412596_4198300_3.jpg","IsPresent":0,"PresentPrice":0.00,"DealerId":50002218,"DealerName":"易車互聯公司","BusinessModelId":2,"VendorTel":"18001353123$010-65432123$010-65432124","PId":2,"PName":"北京","CityId":201,"CityName":"北京","SaleRegionType":9999,"Discount":5.00,"FavorablePrice":1.00,"CarReferPrice":21.5900,"SalePrice":20.5900,"NId":17086707,"VendorPrice":22.0000,"StoreState":1,"NewCarID":116433,"CarName":"1.6T 雙離合 尊貴型 四驅","CarTransmissionType":"雙離合","CarYearType":2015,"CsId":1574,"CsShowName":"全新途勝","CsLevelId":64,"CpCountry":1,"CbName":"北京現代","CbId":10062,"CMBName":"現代","CMBID":13,"CarColor":"","AreaId":110108,"AreaName":"海淀區","JiangFu":0.0463177396943029180176007411,"MobileDimWeightSort":60001,"PCDimWeightSort":64001,"MediaIds":"1,2,3,4,5,11,16,17,18,20,22","MediaIdsArr":[1,2,3,4,5,11,16,17,18,20,22],"CityNameList":"","ProvinceSpell":"beijing","CitySpell":"beijing","NewsPubTime":"2018-02-11T10:26:00+08:00","StartDateTime":"2018-02-11T00:00:00+08:00","EndDateTime":"2018-03-12T00:00:00+08:00","LastModifyTime":"2018-02-11T10:26:00+08:00","DealerLastmodifyTime":"2017-04-25T18:03:00+08:00","TimeStamp":"2018-08-16T14:39:07.8177881+08:00"}
50002218____17089274有9
1
{"VendorId":50002218,"NewsId":17089274,"CarId":115717,"CarImage":"https://img1.bitautoimg.com/autoalbum/files/20130109/519/17291451996479_2435044_3.jpg","IsPresent":0,"PresentPrice":0.00,"DealerId":50002218,"DealerName":"易車互聯公司","BusinessModelId":2,"VendorTel":"18001353123$010-65432123$010-65432124","PId":2,"PName":"北京","CityId":201,"CityName":"北京","SaleRegionType":9999,"Discount":0.00,"FavorablePrice":0.00,"CarReferPrice":24.9800,"SalePrice":24.9800,"NId":17089274,"VendorPrice":25.5000,"StoreState":1,"NewCarID":115717,"CarName":"2.0T 四驅 自動 智慧型 5座","CarTransmissionType":"手自一體","CarYearType":2015,"CsId":3821,"CsShowName":"全新勝達","CsLevelId":64,"CpCountry":1,"CbName":"北京現代","CbId":10062,"CMBName":"現代","CMBID":13,"CarColor":"","AreaId":110108,"AreaName":"海淀區","JiangFu":0.0,"MobileDimWeightSort":60001,"PCDimWeightSort":64001,"MediaIds":"1,2,3,4,5,11,16,17,18,20,22","MediaIdsArr":[1,2,3,4,5,11,16,17,18,20,22],"CityNameList":"","ProvinceSpell":"beijing","CitySpell":"beijing","NewsPubTime":"2018-08-15T10:22:00+08:00","StartDateTime":"2018-08-15T00:00:00+08:00","EndDateTime":"2018-09-13T00:00:00+08:00","LastModifyTime":"2018-08-15T10:22:00+08:00","DealerLastmodifyTime":"2017-04-25T18:03:00+08:00","TimeStamp":"2018-08-16T14:39:07.8237884+08:00"}
{"VendorId":50002218,"NewsId":17089274,"CarId":115718,"CarImage":"https://img1.bitautoimg.com/autoalbum/files/20130109/519/17291451996479_2435044_3.jpg","IsPresent":0,"PresentPrice":0.00,"DealerId":50002218,"DealerName":"易車互聯公司","BusinessModelId":2,"VendorTel":"18001353123$010-65432123$010-65432124","PId":2,"PName":"北京","CityId":201,"CityName":"北京","SaleRegionType":9999,"Discount":0.00,"FavorablePrice":0.00,"CarReferPrice":23.9800,"SalePrice":23.9800,"NId":17089274,"VendorPrice":23.7700,"StoreState":1,"NewCarID":115718,"CarName":"2.0T 兩驅 自動 智慧型 5座","CarTransmissionType":"手自一體","CarYearType":2015,"CsId":3821,"CsShowName":"全新勝達","CsLevelId":64,"CpCountry":1,"CbName":"北京現代","CbId":10062,"CMBName":"現代","CMBID":13,"CarColor":"","AreaId":110108,"AreaName":"海淀區","JiangFu":0.0,"MobileDimWeightSort":60001,"PCDimWeightSort":64001,"MediaIds":"1,2,3,4,5,11,16,17,18,20,22","MediaIdsArr":[1,2,3,4,5,11,16,17,18,20,22],"CityNameList":"","ProvinceSpell":"beijing","CitySpell":"beijing","NewsPubTime":"2018-08-15T10:22:00+08:00","StartDateTime":"2018-08-15T00:00:00+08:00","EndDateTime":"2018-09-13T00:00:00+08:00","LastModifyTime":"2018-08-15T10:22:00+08:00","DealerLastmodifyTime":"2017-04-25T18:03:00+08:00","TimeStamp":"2018-08-16T14:39:07.8237884+08:00"}
{"VendorId":50002218,"NewsId":17089274,"CarId":114034,"CarImage":"https://img1.bitautoimg.com/autoalbum/files/20130109/519/17291451996479_2435044_3.jpg","IsPresent":0,"PresentPrice":0.00,"DealerId":50002218,"DealerName":"易車互聯公司","BusinessModelId":2,"VendorTel":"18001353123$010-65432123$010-65432124","PId":2,"PName":"北京","CityId":201,