QueryBuilder boolQueryBuilder = query.getBoolQueryBuilder(localInfoRequest, QueryEnum.termsQuery); FunctionScoreQueryBuilder.FilterFunctionBuilder[] filterFunctionBuilders = new FunctionScoreQueryBuilder.FilterFunctionBuilder[3]; ScoreFunctionBuilder score = new FieldValueFactorFunctionBuilder("type").factor(1.0f); filterFunctionBuilders[0] = new FunctionScoreQueryBuilder.FilterFunctionBuilder(score); ScoreFunctionBuilder score1 = new FieldValueFactorFunctionBuilder("clipId").factor(1.0f); filterFunctionBuilders[1] = new FunctionScoreQueryBuilder.FilterFunctionBuilder(score1); ScoreFunctionBuilder score2 = new FieldValueFactorFunctionBuilder("partnerId").factor(1.0f); filterFunctionBuilders[2] = new FunctionScoreQueryBuilder.FilterFunctionBuilder(score2); FunctionScoreQueryBuilder functionScoreQuery = QueryBuilders.functionScoreQuery(boolQueryBuilder, filterFunctionBuilders) .scoreMode(FunctionScoreQuery.ScoreMode.SUM) .boostMode(CombineFunction.SUM); System.out.println(functionScoreQuery); CollapseBuilder collapse = new CollapseBuilder("partnerId"); InnerHitBuilder innerHit = new InnerHitBuilder(); innerHit.setName("collapse"); innerHit.setFrom(0); innerHit.setSize(1); innerHit.setTrackScores(true); innerHit.setIgnoreUnmapped(true); innerHit.addSort(SortBuilders.fieldSort("serviceId").order(SortOrder.DESC)).addSort(SortBuilders.geoDistanceSort("point",30.223,-70.334)); collapse.setInnerHits(innerHit); SearchSourceBuilder builder = new SearchSourceBuilder() .query(functionScoreQuery) .collapse(collapse) .from(0) .size(10) .sort(SortBuilders.fieldSort("type").order(SortOrder.DESC)) .sort(SortBuilders.fieldSort("partnerId").order(SortOrder.DESC)); System.out.println(builder); SearchRequest request = new SearchRequest(IndexEnum.LocalInfo.getIndex()) .source(builder) .searchType(SearchType.QUERY_THEN_FETCH); SearchResponse response = client.search(request, RequestOptions.DEFAULT); SearchHit[] hits = response.getHits().getHits(); for (SearchHit hit : hits) { System.out.println("Hits"); System.out.println(hit.getId()); for (SearchHits inner : hit.getInnerHits().values()) { System.out.println("InnerHits"); for (SearchHit innerCollapse : inner.getHits()) { System.out.println(innerCollapse.getId()); } } } Map<string, searchhits=""> innerHits = hits[0].getInnerHits(); Optional first = innerHits.values().stream().findFirst(); LocalInfo localInfo = JsonUtil.transFormationMap(first.get().getHits()[0].getSourceAsString(), LocalInfo.class); System.out.println(JsonUtil.transFormationJson(localInfo));
标签:Java,自定义,System,Elasticsearch,println,new,FunctionScoreQueryBuilder,out,innerHit From: https://www.cnblogs.com/qiezi777/p/16877321.html