Spring Data Elasticsearch aggregate search actual combat

Prepare the environment

1 Install Elasticsearch 5.5.2 (omitted)

2 Use crawlers to crawl Xiamen Talent Network related recruitment information (omitted)

Through the elasticsearch-head plugin, we can see that we have crawled a lot of data

3 Integrate spring-data-elasticsearch in the project

①Introduce dependency

         <!--集成elasticSearch-->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-elasticsearch</artifactId>
            <version>3.1.1.RELEASE</version>
        </dependency>

② Configure elasticsearch host information in the .yml file

 Use spring data elasticsearch interface

Find the company in the second phase of the software park, the position is Java development company, and sort by average salary from high to low, return the top 100 records

sql:  select company,avg(lowSalary) as avg_salary group by company order by avg_salary

**
 * 描述:
 * 招聘信息检
 *
 * @author Administrator
 * @create 2018-10-26 17:45
 */
@RestController
@RequestMapping("/search")
public class JobController {

    @Autowired
    private JobRepository jobRepository;

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    @PostMapping("/salary")
    public ResponseEntity searchSalary(@RequestBody  List<QueryFilter> queryFilterList){
        BoolQueryBuilder qb = QueryBuilders.boolQuery();
        queryFilterList.forEach(queryFilter -> {
            if (queryFilter.isMust()) {
                qb.must(QueryBuilders.matchPhraseQuery(queryFilter.getName(),queryFilter.getValue()));
            } else {
                qb.should(QueryBuilders.matchPhraseQuery(queryFilter.getName(),queryFilter.getValue()));
            }
        });
        FieldSortBuilder salary = SortBuilders.fieldSort("lowSalary").order(SortOrder.DESC);
        TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("company_count").order(Terms.Order.aggregation("avg_salary",false)).field("company.keyword").size(100);
        AvgAggregationBuilder avgAggregationBuilder = AggregationBuilders.avg("avg_salary").field("lowSalary");
        SearchRequestBuilder requestBuilder = elasticsearchTemplate.getClient().prepareSearch("zhipin").setTypes("job");
        requestBuilder.addAggregation(termsAggregationBuilder.subAggregation(avgAggregationBuilder));
        requestBuilder.setQuery(qb).addSort(salary);
        SearchResponse response = requestBuilder.execute().actionGet();

        Terms company_count = response.getAggregations().get("company_count");
        for(Terms.Bucket entry:company_count.getBuckets()){
            String key = entry.getKeyAsString();
            long docCount = entry.getDocCount();
            double avg_salary = ((InternalAvg) entry.getAggregations().get("avg_salary")).getValue();
            System.out.println("key ["+key+"],doc_count ["+docCount+"],avg_salary :"+avg_salary);
        }

        return new ResponseEntity(1, HttpStatus.OK);
    }
}

The query results are as follows:

Find the company in the second phase of the software park, and the position is a Java-developed company, and return the first 20 records according to the average salary from high to low (with specific job information (in reverse order according to the company's recruitment salary))

@RestController
@RequestMapping("/search")
public class JobController {

    @Autowired
    private JobRepository jobRepository;

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    @PostMapping("/salary")
    public ResponseEntity searchSalary(@RequestBody  List<QueryFilter> queryFilterList) throws InvocationTargetException, IllegalAccessException {
        BoolQueryBuilder qb = QueryBuilders.boolQuery();
        queryFilterList.forEach(queryFilter -> {
            if (queryFilter.isMust()) {
                qb.must(QueryBuilders.matchPhraseQuery(queryFilter.getName(),queryFilter.getValue()));
            } else {
                qb.should(QueryBuilders.matchPhraseQuery(queryFilter.getName(),queryFilter.getValue()));
            }
        });
        FieldSortBuilder salary = SortBuilders.fieldSort("lowSalary").order(SortOrder.DESC);
        TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("companyName").order(Terms.Order.aggregation("avg_salary",false)).field("company.keyword").size(20);
        AvgAggregationBuilder avgAggregationBuilder = AggregationBuilders.avg("avg_salary").field("lowSalary");
        TopHitsAggregationBuilder top = AggregationBuilders.topHits("top").sort(salary).size(1).sort(salary);
        SearchRequestBuilder requestBuilder = elasticsearchTemplate.getClient().prepareSearch("zhipin").setTypes("job");
        requestBuilder.addAggregation(termsAggregationBuilder.subAggregation(top).subAggregation(avgAggregationBuilder));
        requestBuilder.setQuery(qb);
        SearchResponse response = requestBuilder.execute().actionGet();

        List<Job> jobList=new ArrayList<>();
        Terms companyName = response.getAggregations().get("companyName");
        for(Terms.Bucket entry:companyName.getBuckets()){
            String key = entry.getKeyAsString();
            long docCount = entry.getDocCount();
            TopHits topHits = entry.getAggregations().get("top");
            double avg_salary = ((InternalAvg) entry.getAggregations().get("avg_salary")).getValue();
            for (SearchHit hit:topHits.getHits().getHits()){
                System.out.println("key ["+key+"],doc_count ["+docCount+"],avg_salary :"+avg_salary+"document:"+hit.getSource());
                Job job=new Job();
                org.apache.commons.beanutils.BeanUtils.populate(job,hit.getSource());
                jobList.add(job);
            }

        }

        return new ResponseEntity(jobList, HttpStatus.OK);
    }
}

 

The query results are as follows

Use graphical visualization reports

 

 

Guess you like

Origin blog.csdn.net/Lixuanshengchao/article/details/83451068