mirror of
https://github.com/JeffLi1993/springboot-learning-example.git
synced 2026-03-14 06:03:52 +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.domain.City;
|
||||||
import org.spring.springboot.service.CityService;
|
import org.spring.springboot.service.CityService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.data.domain.Page;
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 城市 Controller 实现 Restful HTTP 服务
|
* 城市 Controller 实现 Restful HTTP 服务
|
||||||
* <p>
|
* <p>
|
||||||
@@ -18,12 +19,12 @@ public class CityRestController {
|
|||||||
private CityService cityService;
|
private CityService cityService;
|
||||||
|
|
||||||
@RequestMapping(value = "/api/city", method = RequestMethod.POST)
|
@RequestMapping(value = "/api/city", method = RequestMethod.POST)
|
||||||
public void createCity(@RequestBody City city) {
|
public Long createCity(@RequestBody City city) {
|
||||||
cityService.saveCity(city);
|
return cityService.saveCity(city);
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(value = "/api/city/search", method = RequestMethod.GET)
|
@RequestMapping(value = "/api/city/search", method = RequestMethod.GET)
|
||||||
public Page<City> searchCity(@RequestParam(value = "pageNumber") Integer pageNumber,
|
public List<City> searchCity(@RequestParam(value = "pageNumber") Integer pageNumber,
|
||||||
@RequestParam(value = "pageSize", required = false) Integer pageSize,
|
@RequestParam(value = "pageSize", required = false) Integer pageSize,
|
||||||
@RequestParam(value = "searchContent") String searchContent) {
|
@RequestParam(value = "searchContent") String searchContent) {
|
||||||
return cityService.searchCity(pageNumber,pageSize,searchContent);
|
return cityService.searchCity(pageNumber,pageSize,searchContent);
|
||||||
|
|||||||
@@ -9,11 +9,10 @@ import java.io.Serializable;
|
|||||||
*
|
*
|
||||||
* Created by bysocket on 03/05/2017.
|
* Created by bysocket on 03/05/2017.
|
||||||
*/
|
*/
|
||||||
@Document(indexName = "cityIndex", type = "city")
|
@Document(indexName = "cityindex", type = "city")
|
||||||
public class 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;
|
package org.spring.springboot.service;
|
||||||
|
|
||||||
import org.spring.springboot.domain.City;
|
import org.spring.springboot.domain.City;
|
||||||
import org.springframework.data.domain.Page;
|
import java.util.List;
|
||||||
|
|
||||||
public interface CityService {
|
public interface CityService {
|
||||||
|
|
||||||
@@ -22,5 +22,5 @@ public interface CityService {
|
|||||||
* @param searchContent
|
* @param searchContent
|
||||||
* @return
|
* @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
|
@Override
|
||||||
public Long saveCity(City city) {
|
public Long saveCity(City city) {
|
||||||
|
|
||||||
City cityResult = cityRepository.save(city);
|
City cityResult = cityRepository.save(city);
|
||||||
return cityResult.getId();
|
return cityResult.getId();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Page<City> searchCity(Integer pageNumber,
|
public List<City> searchCity(Integer pageNumber,
|
||||||
Integer pageSize,
|
Integer pageSize,
|
||||||
String searchContent) {
|
String searchContent) {
|
||||||
// 分页参数
|
// 分页参数
|
||||||
// 按城市编号倒序
|
Pageable pageable = new PageRequest(pageNumber, pageSize);
|
||||||
Pageable pageable = new PageRequest(pageNumber, pageSize, Sort.Direction.DESC, "id");
|
|
||||||
|
|
||||||
// Function Score Query
|
// Function Score Query
|
||||||
FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery()
|
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());
|
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