Example usage for org.springframework.data.elasticsearch.core ResultsExtractor ResultsExtractor

List of usage examples for org.springframework.data.elasticsearch.core ResultsExtractor ResultsExtractor

Introduction

In this page you can find the example usage for org.springframework.data.elasticsearch.core ResultsExtractor ResultsExtractor.

Prototype

ResultsExtractor

Source Link

Usage

From source file:com.company.project.data.elasticsearch.service.UserESServiceTest.java

@Test
public void testAggregation() {
    System.out.println("testAggregation");

    elasticsearchTemplate.deleteIndex(Car.class);
    elasticsearchTemplate.createIndex("car_idx");
    elasticsearchTemplate.putMapping(Car.class);
    elasticsearchTemplate.refresh(Car.class, true);

    boolean typeExists = elasticsearchTemplate.typeExists("car_idx", "car_type");

    assertTrue(typeExists);//from  w  w  w  . ja  v  a  2s.  co  m
    /**
      * Single index example
      */
    String id = "1";
    Car car = new Car();
    car.setId(id);
    car.setColor("red");
    car.setPrice(10000);
    car.setMake("honda");
    car.setSold("2014-10-28");
    IndexQuery userIndex = new IndexQuery();
    userIndex.setId(id);
    userIndex.setObject(car);
    userIndex.setIndexName("car_idx");
    userIndex.setType("car_type");
    elasticsearchTemplate.index(userIndex);

    /**
     * Bulk index example
     */
    id = "2";
    car = new Car();
    car.setId(id);
    car.setColor("red");
    car.setPrice(20000);
    car.setMake("honda");
    car.setSold("2014-11-05");
    IndexQuery indexQuery2 = new IndexQueryBuilder().withId(car.getId()).withIndexName("car_idx")
            .withObject(car).build();

    id = "3";
    car = new Car();
    car.setId(id);
    car.setColor("green");
    car.setPrice(30000);
    car.setMake("ford");
    car.setSold("2014-05-18");
    IndexQuery indexQuery3 = new IndexQueryBuilder().withId(car.getId()).withIndexName("car_idx")
            .withObject(car).build();

    id = "4";
    car = new Car();
    car.setId(id);
    car.setColor("blue");
    car.setPrice(15000);
    car.setMake("toyota");
    car.setSold("2014-07-02");
    IndexQuery indexQuery4 = new IndexQueryBuilder().withId(car.getId()).withIndexName("car_idx")
            .withObject(car).build();

    id = "5";
    car = new Car();
    car.setId(id);
    car.setColor("green");
    car.setPrice(12000);
    car.setMake("toyota");
    car.setSold("2014-08-19");
    IndexQuery indexQuery5 = new IndexQueryBuilder().withId(car.getId()).withIndexName("car_idx")
            .withObject(car).build();

    id = "6";
    car = new Car();
    car.setId(id);
    car.setColor("red");
    car.setPrice(20000);
    car.setMake("honda");
    car.setSold("2014-11-05");
    IndexQuery indexQuery6 = new IndexQueryBuilder().withId(car.getId()).withIndexName("car_idx")
            .withObject(car).build();

    id = "7";
    car = new Car();
    car.setId(id);
    car.setColor("red");
    car.setPrice(80000);
    car.setMake("bmw");
    car.setSold("2014-01-01");
    IndexQuery indexQuery7 = new IndexQueryBuilder().withId(car.getId()).withIndexName("car_idx")
            .withObject(car).build();

    id = "8";
    car = new Car();
    car.setId(id);
    car.setColor("blue");
    car.setPrice(25000);
    car.setMake("ford");
    car.setSold("2014-02-12");
    IndexQuery indexQuery8 = new IndexQueryBuilder().withId(car.getId()).withIndexName("car_idx")
            .withObject(car).build();
    elasticsearchTemplate.bulkIndex(Arrays.asList(indexQuery2, indexQuery3, indexQuery4, indexQuery5,
            indexQuery6, indexQuery7, indexQuery8));
    // alternative to index
    //userRepository.save(Arrays.asList(car));

    elasticsearchTemplate.refresh(Car.class, true);

    // 1   honda   red   10000   2014-10-28
    // 2   honda   red   20000   2014-11-05
    // 3   ford    green 30000   2014-05-18
    // 4   toyota  blue  15000   2014-07-02
    // 5   toyota  green 12000   2014-08-19
    // 6   honda   red   20000   2014-11-05
    // 7   bmw     red   80000   2014-01-01
    // 8   ford    blue  25000   2014-02-12
    SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery())
            .withSearchType(SearchType.COUNT).withIndices("car_idx").withTypes("car_type")
            .addAggregation(AggregationBuilders.terms("colors").field("color")
                    .subAggregation(AggregationBuilders.avg("avg_price").field("price")))
            .build();
    Aggregations aggregations = elasticsearchTemplate.query(searchQuery, new ResultsExtractor<Aggregations>() {
        @Override
        public Aggregations extract(SearchResponse response) {
            return response.getAggregations();
        }
    });
    assertThat(aggregations, is(notNullValue()));
    assertThat(aggregations.asMap().get("colors"), is(notNullValue()));
    Gson gson = new Gson();
    for (Aggregation agg : aggregations.asList()) {
        // {"order":{"id":1,"key":"_count","asc":false,"comparator":{}},"requiredSize":10,"minDocCount":1,
        // "buckets":[{"termBytes":{"bytes":[114,101,100],"offset":0,"length":3},"bucketOrd":0,"docCount":3,
        // "aggregations":{"aggregations":[{"sum":50000.0,"count":3,"name":"avg_price"}]}},{"termBytes":{"bytes":[98,108,117,101],"offset":0,"length":4},"bucketOrd":0,"docCount":2,
        //blue  // "aggregations":{"aggregations":[{"sum":40000.0,"count":2,"name":"avg_price"}]}},{"termBytes":{"bytes":[103,114,101,101,110],"offset":0,"length":5},"bucketOrd":0,"docCount":2,
        //green  // "aggregations":{"aggregations":[{"sum":42000.0,"count":2,"name":"avg_price"}]}}],"name":"colors"}
        System.out.println("Aggregation json string:" + gson.toJson(agg));

    }

}

From source file:org.egov.ptis.service.es.CollectionIndexElasticSearchService.java

/**
 * Gives the consolidated collection for the dates and billing service
 * /* w w  w  .java2  s. c om*/
 * @param fromDate
 * @param toDate
 * @param billingService
 * @return BigDecimal
 */
public BigDecimal getConsolidatedCollForYears(Date fromDate, Date toDate, String billingService) {
    BoolQueryBuilder boolQuery = QueryBuilders.boolQuery()
            .must(QueryBuilders.rangeQuery(RECEIPT_DATE).gte(DATEFORMATTER_YYYY_MM_DD.format(fromDate))
                    .lte(DATEFORMATTER_YYYY_MM_DD.format(toDate)).includeUpper(false))
            .mustNot(QueryBuilders.matchQuery(STATUS, CANCELLED));

    if (COLLECION_BILLING_SERVICE_WTMS.equalsIgnoreCase(billingService))
        boolQuery = boolQuery.must(QueryBuilders.matchQuery(BILLING_SERVICE, billingService));
    else
        boolQuery = boolQuery.must(QueryBuilders.boolQuery().filter(QueryBuilders.termsQuery(BILLING_SERVICE,
                Arrays.asList(COLLECION_BILLING_SERVICE_PT, COLLECION_BILLING_SERVICE_VLT))));

    SearchQuery searchQueryColl = new NativeSearchQueryBuilder().withIndices(COLLECTION_INDEX_NAME)
            .withQuery(boolQuery).addAggregation(AggregationBuilders.sum(COLLECTIONTOTAL).field(TOTAL_AMOUNT))
            .build();

    Aggregations collAggr = elasticsearchTemplate.query(searchQueryColl, new ResultsExtractor<Aggregations>() {
        @Override
        public Aggregations extract(SearchResponse response) {
            return response.getAggregations();
        }
    });

    Sum aggr = collAggr.get(COLLECTIONTOTAL);
    return BigDecimal.valueOf(aggr.getValue()).setScale(0, BigDecimal.ROUND_HALF_UP);
}

From source file:org.egov.ptis.service.es.CollectionIndexElasticSearchService.java

/**
 * Provides collection and demand results
 * //from  w w w  . j av a2  s  . co m
 * @param collectionDetailsRequest
 * @param fromDate
 * @param toDate
 * @param indexName
 * @param fieldName
 * @param ulbCodeField
 * @param aggregationField
 * @return Map
 */
public Map<String, BigDecimal> getCollectionAndDemandValues(CollectionDetailsRequest collectionDetailsRequest,
        Date fromDate, Date toDate, String indexName, String fieldName, String aggregationField) {
    BoolQueryBuilder boolQuery = prepareWhereClause(collectionDetailsRequest, indexName);
    if (indexName.equals(COLLECTION_INDEX_NAME))
        boolQuery = boolQuery
                .filter(QueryBuilders.rangeQuery(RECEIPT_DATE).gte(DATEFORMATTER_YYYY_MM_DD.format(fromDate))
                        .lte(DATEFORMATTER_YYYY_MM_DD.format(toDate)).includeUpper(false))
                .mustNot(QueryBuilders.matchQuery(STATUS, CANCELLED));
    else
        boolQuery = boolQuery.filter(QueryBuilders.matchQuery(IS_ACTIVE, true))
                .filter(QueryBuilders.matchQuery(IS_EXEMPTED, false));

    AggregationBuilder aggregation = AggregationBuilders.terms(BY_CITY).field(aggregationField).size(120)
            .subAggregation(AggregationBuilders.sum("total").field(fieldName));

    SearchQuery searchQueryColl = new NativeSearchQueryBuilder().withIndices(indexName).withQuery(boolQuery)
            .addAggregation(aggregation).build();

    Aggregations collAggr = elasticsearchTemplate.query(searchQueryColl, new ResultsExtractor<Aggregations>() {
        @Override
        public Aggregations extract(SearchResponse response) {
            return response.getAggregations();
        }
    });

    StringTerms cityAggr = collAggr.get(BY_CITY);
    Map<String, BigDecimal> cytdCollMap = new HashMap<>();
    for (Terms.Bucket entry : cityAggr.getBuckets()) {
        Sum aggr = entry.getAggregations().get("total");
        cytdCollMap.put(String.valueOf(entry.getKey()),
                BigDecimal.valueOf(aggr.getValue()).setScale(0, BigDecimal.ROUND_HALF_UP));
    }
    return cytdCollMap;
}

From source file:org.egov.ptis.service.es.CollectionIndexElasticSearchService.java

/**
 * Provides demand break-up of total amount
 * @param collectionDetailsRequest/*from  w ww  .j a  v  a 2 s .  co  m*/
 * @param indexName
 * @param aggregationField
 * @return StringTerms
 */
public StringTerms getIndividualDemands(CollectionDetailsRequest collectionDetailsRequest, String indexName,
        String aggregationField, boolean isForMis) {
    AggregationBuilder aggregation;
    BoolQueryBuilder boolQuery = prepareWhereClause(collectionDetailsRequest, indexName);
    boolQuery = boolQuery.filter(QueryBuilders.matchQuery(IS_ACTIVE, true))
            .filter(QueryBuilders.matchQuery(IS_EXEMPTED, false));

    if (isForMis)
        aggregation = AggregationBuilders.terms(BY_CITY).field(aggregationField).size(120)
                .subAggregation(AggregationBuilders.sum(ARREARDMD).field("arrearDemand"))
                .subAggregation(AggregationBuilders.sum(CURR_DMD).field("annualDemand"))
                .subAggregation(AggregationBuilders.sum(ARREAR_INTEREST_DMD).field("arrearInterestDemand"))
                .subAggregation(AggregationBuilders.sum(CURR_INTEREST_DMD).field("currentInterestDemand"))
                .subAggregation(AggregationBuilders.sum("total_dmd").field(TOTAL_DEMAND))
                .subAggregation(AggregationBuilders.sum("adjustment").field("adjustment"))
                .subAggregation(AggregationBuilders.sum("arrear_coll").field("arrearCollection"))
                .subAggregation(AggregationBuilders.sum("curr_coll").field("annualCollection"))
                .subAggregation(
                        AggregationBuilders.sum("arrear_interest_coll").field("arrearInterestCollection"))
                .subAggregation(
                        AggregationBuilders.sum("curr_interest_coll").field("currentInterestCollection"))
                .subAggregation(AggregationBuilders.sum(ADVANCE).field(ADVANCE))
                .subAggregation(AggregationBuilders.sum(REBATE).field(REBATE))
                .subAggregation(AggregationBuilders.sum("total_coll").field("totalCollection"));
    else
        aggregation = AggregationBuilders.terms(BY_CITY).field(aggregationField).size(120)
                .subAggregation(AggregationBuilders.sum(ARREARDMD).field("arrearDemand"))
                .subAggregation(AggregationBuilders.sum(CURR_DMD).field("annualDemand"))
                .subAggregation(AggregationBuilders.sum(ARREAR_INTEREST_DMD).field("arrearInterestDemand"))
                .subAggregation(AggregationBuilders.sum(CURR_INTEREST_DMD).field("currentInterestDemand"));

    SearchQuery searchQueryColl = new NativeSearchQueryBuilder().withIndices(indexName).withQuery(boolQuery)
            .addAggregation(aggregation).build();

    Aggregations collAggr = elasticsearchTemplate.query(searchQueryColl, new ResultsExtractor<Aggregations>() {
        @Override
        public Aggregations extract(SearchResponse response) {
            return response.getAggregations();
        }
    });

    return collAggr.get(BY_CITY);
}

From source file:org.egov.ptis.service.es.CollectionIndexElasticSearchService.java

/**
 * Gives the total count of receipts/*w  w w.jav  a 2 s  . c o  m*/
 * 
 * @param collectionDetailsRequest
 * @param fromDate
 * @param toDate
 * @return receipt count
 */
private Long getTotalReceiptCountsForDates(CollectionDetailsRequest collectionDetailsRequest, Date fromDate,
        Date toDate) {
    BoolQueryBuilder boolQuery = prepareWhereClause(collectionDetailsRequest, COLLECTION_INDEX_NAME);
    boolQuery = boolQuery
            .filter(QueryBuilders.rangeQuery(RECEIPT_DATE).gte(DATEFORMATTER_YYYY_MM_DD.format(fromDate))
                    .lte(DATEFORMATTER_YYYY_MM_DD.format(toDate)).includeUpper(false))
            .mustNot(QueryBuilders.matchQuery(STATUS, CANCELLED));

    SearchQuery searchQueryColl = new NativeSearchQueryBuilder().withIndices(COLLECTION_INDEX_NAME)
            .withQuery(boolQuery).addAggregation(AggregationBuilders.count(RECEIPT_COUNT).field(CONSUMER_CODE))
            .build();

    Aggregations collCountAggr = elasticsearchTemplate.query(searchQueryColl,
            new ResultsExtractor<Aggregations>() {
                @Override
                public Aggregations extract(SearchResponse response) {
                    return response.getAggregations();
                }
            });

    ValueCount aggr = collCountAggr.get(RECEIPT_COUNT);
    return Long.valueOf(aggr.getValue());
}

From source file:org.egov.ptis.service.es.CollectionIndexElasticSearchService.java

/**
 * Provides month-wise receipts count for consecutive years
 * /*  ww w  . j a  v  a 2  s .  c  om*/
 * @param collectionDetailsRequest
 * @param fromDate
 * @param toDate
 * @return SearchResponse
 */
private Aggregations getReceiptsCountForConsecutiveYears(CollectionDetailsRequest collectionDetailsRequest,
        Date fromDate, Date toDate) {
    BoolQueryBuilder boolQuery = prepareWhereClause(collectionDetailsRequest, COLLECTION_INDEX_NAME);
    boolQuery = boolQuery.mustNot(QueryBuilders.matchQuery(STATUS, CANCELLED));
    AggregationBuilder monthAggregation = AggregationBuilders.dateHistogram(DATE_AGG).field(RECEIPT_DATE)
            .interval(DateHistogramInterval.MONTH)
            .subAggregation(AggregationBuilders.count(RECEIPT_COUNT).field("receiptNumber"));

    SearchQuery searchQueryColl = new NativeSearchQueryBuilder().withIndices(COLLECTION_INDEX_NAME)
            .withQuery(boolQuery.filter(
                    QueryBuilders.rangeQuery(RECEIPT_DATE).gte(DATEFORMATTER_YYYY_MM_DD.format(fromDate))
                            .lte(DATEFORMATTER_YYYY_MM_DD.format(toDate)).includeUpper(false)))
            .addAggregation(monthAggregation).build();

    return elasticsearchTemplate.query(searchQueryColl, new ResultsExtractor<Aggregations>() {
        @Override
        public Aggregations extract(SearchResponse response) {
            return response.getAggregations();
        }
    });
}

From source file:org.egov.ptis.service.es.CollectionIndexElasticSearchService.java

public Map<String, BigDecimal> getCollectionAndDemandCountResults(
        CollectionDetailsRequest collectionDetailsRequest, Date fromDate, Date toDate, String indexName,
        String fieldName, String aggregationField) {
    BoolQueryBuilder boolQuery = prepareWhereClause(collectionDetailsRequest, indexName);
    if (indexName.equals(COLLECTION_INDEX_NAME))
        boolQuery = boolQuery//from   w w w.  j  a v a 2 s .co m
                .filter(QueryBuilders.rangeQuery(RECEIPT_DATE).gte(DATEFORMATTER_YYYY_MM_DD.format(fromDate))
                        .lte(DATEFORMATTER_YYYY_MM_DD.format(toDate)).includeUpper(false))
                .mustNot(QueryBuilders.matchQuery(STATUS, CANCELLED));
    else if (indexName.equals(PROPERTY_TAX_INDEX_NAME))
        boolQuery = boolQuery.filter(QueryBuilders.matchQuery(IS_ACTIVE, true))
                .filter(QueryBuilders.matchQuery(IS_EXEMPTED, false));

    AggregationBuilder aggregation = AggregationBuilders.terms(BY_CITY).field(aggregationField).size(120)
            .subAggregation(AggregationBuilders.count("total_count").field(fieldName));

    SearchQuery searchQueryColl = new NativeSearchQueryBuilder().withIndices(indexName).withQuery(boolQuery)
            .addAggregation(aggregation).build();

    Aggregations collAggr = elasticsearchTemplate.query(searchQueryColl, new ResultsExtractor<Aggregations>() {
        @Override
        public Aggregations extract(SearchResponse response) {
            return response.getAggregations();
        }
    });

    StringTerms cityAggr = collAggr.get(BY_CITY);
    Map<String, BigDecimal> cytdCollMap = new HashMap<>();
    for (Terms.Bucket entry : cityAggr.getBuckets()) {
        ValueCount aggr = entry.getAggregations().get("total_count");
        cytdCollMap.put(String.valueOf(entry.getKey()),
                BigDecimal.valueOf(aggr.getValue()).setScale(0, BigDecimal.ROUND_HALF_UP));
    }
    return cytdCollMap;
}