前言

本文介绍了如何整合搜索引擎elasticsearch与springboot,对外提供数据查询接口。

业务介绍

我的个人网站需要对mysql数据库内存储的京东商品进行模糊查询(模仿淘宝商品搜索),所以选择了将数据导入elasticsearch随后使用他来进行关键词查询。前端只需发送用户搜索的关键词和分页参数(可选),即可返回商品数据(json格式)

开发环境

组件介绍:

  • elasticsearch:搜索引擎,用于存储待搜索数据
  • logstash:用于将mysql中的商品数据同步到搜索引擎中
  • elasticsearch-head(可选):elasticsearch可视化工具
  • kibana(可选):elasticsearch可视化工具

本文测试环境:

  • springboot:1.5.16
  • elasticsearch:2.3.5(springboot1.5仅支持2.x的es)
  • logstash:6.5.4

开发步骤

使用Docker部署elasticsearch

  • docker下一键启动es,可根据需要的版本号对语句做修改
sudo docker run -it --rm --name elasticsearch -d -p 9200:9200 -p 9300:9300 elasticsearch:2.3.5

注意到该命令:

  • –rm参数:容器终止后销毁
  • -d:后台进程
  • -p 9200:9200 -p 9300:9300:开放了9200端口和9300端口

得到如图:

此时打开网页localhost:9200即可查看状态,显示类似为:

{
  "name" : "Ant-Man",
  "cluster_name" : "elasticsearch",
  "version" : {
    "number" : "2.3.5",
    "build_hash" : "90f439ff60a3c0f497f91663701e64ccd01edbb4",
    "build_timestamp" : "2016-07-27T10:36:52Z",
    "build_snapshot" : false,
    "lucene_version" : "5.5.0"
  },
  "tagline" : "You Know, for Search"
}

注意:docker的es默认对0.0.0.0公网开放

下载并使用logstash并导入数据

本文中要导入的是pm_backend下的表pm_jd_item内的全部京东商品数据

详细步骤参考:

http://blog.codecp.org/2018/04/16/Elasticsearch%E4%B9%8B%E4%BD%BF%E7%94%A8Logstash%E5%AF%BC%E5%85%A5Mysql%E6%95%B0%E6%8D%AE/

最终编写的jdbc.conf为:

schedule => "* * * * *"默认为每分钟同步一次

input {
  jdbc {
    jdbc_connection_string => "jdbc:mysql://localhost:3306/pm_backend"
    jdbc_user => "root"
    jdbc_password => "xxxxxxxxxx"
    jdbc_driver_library => "xxxxxxxx/mysql-connector-java-5.1.6.jar"
    jdbc_driver_class => "com.mysql.jdbc.Driver"
    jdbc_paging_enabled => "true"
    jdbc_page_size => "5000"
    statement=> "select * from pm_jd_item"
    schedule => "* * * * *"
    type => "pm_jd_item"
  }
}

output {
  elasticsearch {
    hosts => "localhost:9200"
    index => "pm_backend"
    document_type => "%{type}"
    document_id => "%{id}"
  }
  stdout {
    codec => json_lines
  }
}

在logstash目录下执行命令,完成数据的导入:

bin/logstash -f jdbc.conf

得到如图:

同步完成后,使用elasticsearch-head查看(或者用kibana,请随意):

整合进springboot

  1. 添加pom.xml
<!-- 搜索引擎:elastic-search-->
<dependency>
	<groupId>org.elasticsearch</groupId>
	<artifactId>elasticsearch</artifactId>
	 <version>2.4.6</version>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.data</groupId>
	<artifactId>spring-data-elasticsearch</artifactId>
</dependency>
  1. 修改application.properties
# elasticsearch
spring.data.elasticsearch.cluster-name=elasticsearch
#节点地址,多个节点用逗号隔开
spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300
#spring.data.elasticsearch.local=false
spring.data.elasticsearch.repositories.enable=true
  1. 在需要进行搜索的实体类上添加@Document、@Id、@Field等标注,本例为JdItem.java
@Document(indexName = "pm_backend", type = "pm_jd_item")
public class JdItem implements Serializable {

    @Id
    private Integer id;

    @Field(type = FieldType.Long)
    private Long itemId;

    @Field(type = FieldType.Long)
    private Long categoryId;

    @Field(type = FieldType.String)
    private String name;
  1. 添加JdItemRepository继承ElasticsearchRepository
public interface JdItemRepository extends ElasticsearchRepository<JdItem, Integer>{
}

  1. 编写JdItemController中的查询接口findJdItemByName

代码截取自个人项目京东价格监控,仅供参考!

    /** * 根据商品名在pm_jd_item中搜索商品 * @param itemName * @param startRow * @param pageSize * @return */
    @ApiOperation(value="查询商品", notes="查询商品")
    @RequestMapping(value = "/findJdItemByName", method = {
   RequestMethod.GET})
    public ResponseData<List<JdItem>> findJdItemByName(
            @ApiParam("用户输入的商品名") @RequestParam(value = "itemName") String itemName,
            @ApiParam("页码索引(默认为0)") @RequestParam(value = "startRow", required = false, defaultValue = "0") int startRow,
            @ApiParam("每页的商品数量(默认为10)") @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize
    ){
   
        ResponseData<List<JdItem>> responseData = new ResponseData<>();
        try {
   

            FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery().add(QueryBuilders.matchPhraseQuery("name", itemName), ScoreFunctionBuilders.weightFactorFunction(100)).scoreMode("sum").setMinScore(10);
            Pageable pageable = new PageRequest(startRow, pageSize);
            SearchQuery searchQuery = new NativeSearchQueryBuilder().withPageable(pageable).withQuery(functionScoreQueryBuilder).build();
            Page<JdItem> jdItems = jdItemRepository.search(searchQuery);
            // Page分页getTotalPages()返回了应有的页数,临时放在errorMsg传给前端
            responseData.jsonFill(1, String.valueOf(jdItems.getTotalPages()), jdItems.getContent());
        } catch (Exception e) {
   
            e.printStackTrace();
            responseData.jsonFill(2, e.getMessage(), null);
        }
        return responseData;
    }
}
  1. 运行springboot

调用findJdItemByName接口,得到:

整合分词器功能

请参考:https://github.com/medcl/elasticsearch-analysis-ik

参考

Docker安装ES & Kibana:

https://www.jianshu.com/p/fdfead5acc23

Elasticsearch之使用Logstash导入Mysql数据:

http://blog.codecp.org/2018/04/16/Elasticsearch%E4%B9%8B%E4%BD%BF%E7%94%A8Logstash%E5%AF%BC%E5%85%A5Mysql%E6%95%B0%E6%8D%AE/

关注我

我是蛮三刀把刀,后端开发。主要关注后端开发,数据安全,爬虫等方向。

来微信和我聊聊:yangzd1102

Github个人主页:

https://github.com/qqxx6661

原创博客主要内容

  • Java知识点复习全手册
  • Leetcode算法题解析
  • 剑指offer算法题解析
  • Python爬虫相关技术实战
  • 后端开发相关技术实战
  • SpringCloud实战

同步更新公众号及以下全部博客:

1. Csdn

http://blog.csdn.net/qqxx6661

2. 知乎

https://www.zhihu.com/people/yang-zhen-dong-1/

3. 掘金

https://juejin.im/user/5b48015ce51d45191462ba55

4. 简书

https://www.jianshu.com/u/b5f225ca2376

个人公众号:Rude3Knife

如果文章对你有帮助,不妨收藏起来并转发给您的朋友们~