From 931cd7dbc0b208f8db29e9ef855f0da082c6d989 Mon Sep 17 00:00:00 2001 From: JeffLi1993 Date: Wed, 17 May 2017 14:52:43 +0800 Subject: [PATCH] =?UTF-8?q?Spring=20Boot=20=E6=95=B4=E5=90=88=20Elasticsea?= =?UTF-8?q?rch=EF=BC=8C=E5=AE=9E=E7=8E=B0=20function=20score=20query=20?= =?UTF-8?q?=E6=9D=83=E9=87=8D=E5=88=86=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 + springboot-elasticsearch/pom.xml | 39 +++++++++++ .../org/spring/springboot/Application.java | 20 ++++++ .../controller/CityRestController.java | 31 +++++++++ .../org/spring/springboot/domain/City.java | 69 +++++++++++++++++++ .../springboot/service/CityService.java | 26 +++++++ .../service/impl/CityServiceImpl.java | 66 ++++++++++++++++++ .../src/main/resources/application.properties | 3 + 8 files changed, 256 insertions(+) create mode 100755 springboot-elasticsearch/pom.xml create mode 100644 springboot-elasticsearch/src/main/java/org/spring/springboot/Application.java create mode 100644 springboot-elasticsearch/src/main/java/org/spring/springboot/controller/CityRestController.java create mode 100644 springboot-elasticsearch/src/main/java/org/spring/springboot/domain/City.java create mode 100644 springboot-elasticsearch/src/main/java/org/spring/springboot/service/CityService.java create mode 100644 springboot-elasticsearch/src/main/java/org/spring/springboot/service/impl/CityServiceImpl.java create mode 100644 springboot-elasticsearch/src/main/resources/application.properties diff --git a/pom.xml b/pom.xml index 08c923e..91c7e5f 100644 --- a/pom.xml +++ b/pom.xml @@ -39,6 +39,8 @@ springboot-dubbo-server springboot-dubbo-client + + springboot-elasticsearch diff --git a/springboot-elasticsearch/pom.xml b/springboot-elasticsearch/pom.xml new file mode 100755 index 0000000..20b46e8 --- /dev/null +++ b/springboot-elasticsearch/pom.xml @@ -0,0 +1,39 @@ + + + 4.0.0 + + springboot + springboot-elasticsearch + 0.0.1-SNAPSHOT + springboot-elasticsearch :: 整合 Elasticsearch + + + + org.springframework.boot + spring-boot-starter-parent + 1.5.1.RELEASE + + + + + + + org.springframework.boot + spring-boot-starter-data-elasticsearch + + + + + org.springframework.boot + spring-boot-starter-web + + + + + junit + junit + 4.12 + + + diff --git a/springboot-elasticsearch/src/main/java/org/spring/springboot/Application.java b/springboot-elasticsearch/src/main/java/org/spring/springboot/Application.java new file mode 100644 index 0000000..5070937 --- /dev/null +++ b/springboot-elasticsearch/src/main/java/org/spring/springboot/Application.java @@ -0,0 +1,20 @@ +package org.spring.springboot; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * Spring Boot 应用启动类 + * + * Created by bysocket on 16/4/26. + */ +// Spring Boot 应用的标识 +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + // 程序启动入口 + // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件 + SpringApplication.run(Application.class,args); + } +} diff --git a/springboot-elasticsearch/src/main/java/org/spring/springboot/controller/CityRestController.java b/springboot-elasticsearch/src/main/java/org/spring/springboot/controller/CityRestController.java new file mode 100644 index 0000000..b8b443d --- /dev/null +++ b/springboot-elasticsearch/src/main/java/org/spring/springboot/controller/CityRestController.java @@ -0,0 +1,31 @@ +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.*; + +/** + * 城市 Controller 实现 Restful HTTP 服务 + *

+ * Created by bysocket on 03/05/2017. + */ +@RestController +public class CityRestController { + + @Autowired + private CityService cityService; + + @RequestMapping(value = "/api/city", method = RequestMethod.POST) + public void createCity(@RequestBody City city) { + cityService.saveCity(city); + } + + @RequestMapping(value = "/api/city/search", method = RequestMethod.GET) + public Page searchCity(@RequestParam(value = "pageNumber") Integer pageNumber, + @RequestParam(value = "pageSize", required = false) Integer pageSize, + @RequestParam(value = "searchContent") String searchContent) { + return cityService.searchCity(pageNumber,pageSize,searchContent); + } +} diff --git a/springboot-elasticsearch/src/main/java/org/spring/springboot/domain/City.java b/springboot-elasticsearch/src/main/java/org/spring/springboot/domain/City.java new file mode 100644 index 0000000..20b575e --- /dev/null +++ b/springboot-elasticsearch/src/main/java/org/spring/springboot/domain/City.java @@ -0,0 +1,69 @@ +package org.spring.springboot.domain; + +import org.springframework.data.elasticsearch.annotations.Document; + +import java.io.Serializable; + +/** + * 城市实体类 + * + * Created by bysocket on 03/05/2017. + */ +@Document(indexName = "cityIndex", type = "city") +public class City { + +// implements Serializable +// private static final long serialVersionUID = -1L; + + /** + * 城市编号 + */ + private Long id; + + /** + * 省份编号 + */ + private Long provinceid; + + /** + * 城市名称 + */ + private String cityname; + + /** + * 描述 + */ + private String description; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Long getProvinceid() { + return provinceid; + } + + public void setProvinceid(Long provinceid) { + this.provinceid = provinceid; + } + + public String getCityname() { + return cityname; + } + + public void setCityname(String cityname) { + this.cityname = cityname; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } +} diff --git a/springboot-elasticsearch/src/main/java/org/spring/springboot/service/CityService.java b/springboot-elasticsearch/src/main/java/org/spring/springboot/service/CityService.java new file mode 100644 index 0000000..24646a0 --- /dev/null +++ b/springboot-elasticsearch/src/main/java/org/spring/springboot/service/CityService.java @@ -0,0 +1,26 @@ + +package org.spring.springboot.service; + +import org.spring.springboot.domain.City; +import org.springframework.data.domain.Page; + +public interface CityService { + + /** + * 新增城市信息 + * + * @param city + * @return + */ + Long saveCity(City city); + + /** + * 根据关键词,function score query 权重分分页查询 + * + * @param pageNumber + * @param pageSize + * @param searchContent + * @return + */ + Page searchCity(Integer pageNumber, Integer pageSize, String searchContent); +} \ No newline at end of file diff --git a/springboot-elasticsearch/src/main/java/org/spring/springboot/service/impl/CityServiceImpl.java b/springboot-elasticsearch/src/main/java/org/spring/springboot/service/impl/CityServiceImpl.java new file mode 100644 index 0000000..eb14d79 --- /dev/null +++ b/springboot-elasticsearch/src/main/java/org/spring/springboot/service/impl/CityServiceImpl.java @@ -0,0 +1,66 @@ +package org.spring.springboot.service.impl; + +import org.elasticsearch.index.query.QueryBuilders; +import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder; +import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.spring.springboot.domain.City; +import org.spring.springboot.repository.CityRepository; +import org.spring.springboot.service.CityService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; +import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; +import org.springframework.data.elasticsearch.core.query.SearchQuery; +import org.springframework.stereotype.Service; + +import java.util.List; + +/** + * 城市 ES 业务逻辑实现类 + * + * Created by bysocket on 07/02/2017. + */ +@Service +public class CityServiceImpl implements CityService { + + private static final Logger LOGGER = LoggerFactory.getLogger(CityServiceImpl.class); + + @Autowired + CityRepository cityRepository; + + @Override + public Long saveCity(City city) { + City cityResult = cityRepository.save(city); + return cityResult.getId(); + } + + @Override + public Page searchCity(Integer pageNumber, + Integer pageSize, + String searchContent) { + // 分页参数 + // 按城市编号倒序 + Pageable pageable = new PageRequest(pageNumber, pageSize, Sort.Direction.DESC, "id"); + + // Function Score Query + FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery() + .add(QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("cityname", searchContent)), + ScoreFunctionBuilders.weightFactorFunction(1000)) + .add(QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("description", searchContent)), + ScoreFunctionBuilders.weightFactorFunction(100)); + + // 创建搜索 DSL 查询 + SearchQuery searchQuery = new NativeSearchQueryBuilder() + .withPageable(pageable) + .withQuery(functionScoreQueryBuilder).build(); + + LOGGER.info("\n searchCity(): searchContent [" + searchContent + "] \n DSL = \n " + searchQuery.getQuery().toString()); + + return cityRepository.search(searchQuery); + } + +} diff --git a/springboot-elasticsearch/src/main/resources/application.properties b/springboot-elasticsearch/src/main/resources/application.properties new file mode 100644 index 0000000..4f1ed6d --- /dev/null +++ b/springboot-elasticsearch/src/main/resources/application.properties @@ -0,0 +1,3 @@ +# ES +spring.data.elasticsearch.repositories.enabled = true +spring.data.elasticsearch.cluster-nodes = 127.0.0.1:9300 \ No newline at end of file