mirror of
https://github.com/JeffLi1993/springboot-learning-example.git
synced 2026-03-13 21:43:45 +08:00
Spring Boot 整合 Elasticsearch,实现 function score query 权重分查询
This commit is contained in:
@@ -3,9 +3,10 @@ package org.spring.springboot.controller;
|
||||
import org.spring.springboot.domain.City;
|
||||
import org.spring.springboot.service.CityService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 城市 Controller 实现 Restful HTTP 服务
|
||||
* <p>
|
||||
@@ -18,14 +19,14 @@ public class CityRestController {
|
||||
private CityService cityService;
|
||||
|
||||
@RequestMapping(value = "/api/city", method = RequestMethod.POST)
|
||||
public void createCity(@RequestBody City city) {
|
||||
cityService.saveCity(city);
|
||||
public Long createCity(@RequestBody City city) {
|
||||
return cityService.saveCity(city);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/api/city/search", method = RequestMethod.GET)
|
||||
public Page<City> searchCity(@RequestParam(value = "pageNumber") Integer pageNumber,
|
||||
@RequestParam(value = "pageSize", required = false) Integer pageSize,
|
||||
@RequestParam(value = "searchContent") String searchContent) {
|
||||
public List<City> searchCity(@RequestParam(value = "pageNumber") Integer pageNumber,
|
||||
@RequestParam(value = "pageSize", required = false) Integer pageSize,
|
||||
@RequestParam(value = "searchContent") String searchContent) {
|
||||
return cityService.searchCity(pageNumber,pageSize,searchContent);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,11 +9,10 @@ import java.io.Serializable;
|
||||
*
|
||||
* Created by bysocket on 03/05/2017.
|
||||
*/
|
||||
@Document(indexName = "cityIndex", type = "city")
|
||||
public class City {
|
||||
@Document(indexName = "cityindex", type = "city")
|
||||
public class City implements Serializable{
|
||||
|
||||
// implements Serializable
|
||||
// private static final long serialVersionUID = -1L;
|
||||
private static final long serialVersionUID = -1L;
|
||||
|
||||
/**
|
||||
* 城市编号
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package org.spring.springboot.repository;
|
||||
|
||||
import org.spring.springboot.domain.City;
|
||||
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
/**
|
||||
* Created by bysocket on 17/05/2017.
|
||||
*/
|
||||
@Repository
|
||||
public interface CityRepository extends ElasticsearchRepository<City,Long> {
|
||||
|
||||
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
package org.spring.springboot.service;
|
||||
|
||||
import org.spring.springboot.domain.City;
|
||||
import org.springframework.data.domain.Page;
|
||||
import java.util.List;
|
||||
|
||||
public interface CityService {
|
||||
|
||||
@@ -22,5 +22,5 @@ public interface CityService {
|
||||
* @param searchContent
|
||||
* @return
|
||||
*/
|
||||
Page<City> searchCity(Integer pageNumber, Integer pageSize, String searchContent);
|
||||
List<City> searchCity(Integer pageNumber, Integer pageSize, String searchContent);
|
||||
}
|
||||
@@ -34,17 +34,17 @@ public class CityServiceImpl implements CityService {
|
||||
|
||||
@Override
|
||||
public Long saveCity(City city) {
|
||||
|
||||
City cityResult = cityRepository.save(city);
|
||||
return cityResult.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<City> searchCity(Integer pageNumber,
|
||||
public List<City> searchCity(Integer pageNumber,
|
||||
Integer pageSize,
|
||||
String searchContent) {
|
||||
// 分页参数
|
||||
// 按城市编号倒序
|
||||
Pageable pageable = new PageRequest(pageNumber, pageSize, Sort.Direction.DESC, "id");
|
||||
Pageable pageable = new PageRequest(pageNumber, pageSize);
|
||||
|
||||
// Function Score Query
|
||||
FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery()
|
||||
@@ -60,7 +60,8 @@ public class CityServiceImpl implements CityService {
|
||||
|
||||
LOGGER.info("\n searchCity(): searchContent [" + searchContent + "] \n DSL = \n " + searchQuery.getQuery().toString());
|
||||
|
||||
return cityRepository.search(searchQuery);
|
||||
Page<City> searchPageResults = cityRepository.search(searchQuery);
|
||||
return searchPageResults.getContent();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user