Project2_过渡1_一个小型ElasticSearch项目

一、项目环境

  1. 前端技术栈:BootStrap; Jqgrid
  2. 后端技术栈:Mybatis, SpringBoot-2.2.5.RELEASE
  3. 数据库:mySql; Redis
  4. 中间件:ElasticSearch-6.8.0; IK分词器
  5. 软体:jdk1.8; IntelliJ IDEA2019; Centos7虚拟机; MobaXterm; RDM; Kibana

二、文章主题

  1. 内容概述:为实现SpringCloud_Video项目,对ElasticSearch进行学习,本文是不良人_ElasticSearchP37~39小节项目的复现。
  2. 项目源码:videoProject01_pub : dev01
  3. 项目参考:不良人_ElasticSearch不良人_BootStrap
  4. 功能展示:
    (1) 后台(管理员侧)功能: 1) item词条分页展示; 2) 基于基础数据库重建ES索引库; 3) 清空ES中的文档; 4)添加/删除远程词典; 5) 全网热搜排行
    (2) 前台(用户侧)功能: item词条检索并高亮展示。

三、文章内容

内容1:ElasticSearch注意事项

  1. ES支持Restful风格,是一种全文检索技术。ES中的重要概念:(1)索引(index);(2)类型(type);(3)映射(mapping);(4)文档(document)。
  2. 查看ES是否启动,可通过外部访问 http://xxx.xxx.xxx.xxx:9200,有值则成功。
  3. ES必须以普通用户身份安装(本文在虚拟机安装)。
  4. ES刚启动时不能外部访问远程链接。只能本机访问,需要开启远程链接权限。
  5. 需要以管理员用户身份安装Kibana客户端来操作ElasticSearch(本文在虚拟机安装),Kibana访问地址: http://xxx.xxx.xxx.xxx:5601/。
  6. ES默认的分词器只支持英文关键词检索,中文关键词检索会按一个字一个字检索。故需要在ES中加入IK分词器插件。(1)注意1:在线安装和本地安装IK分词器,config文件夹的位置不一样; (2)安装IK分词器时必须将ES中原始数据删除(进入ES安装目录中将data目录数据删除— rm -rf data)。
  7. ES集成java的实现原理如图1,2所示。
    在这里插入图片描述
图1 ES集成java(单体应用)

在这里插入图片描述

图2 ES集成java(微服务应用)

  1. ES集成SpringBoot的两种客户端操作:
    (1) RestHighLevelClient:强大,灵活,但不能进行友好的对象操作,可用于检索关键词并高亮处理。
    (2) ElasticSearchRepository: 对象操作友好(用到entity类),原理如图3所示。
    在这里插入图片描述
图3 ES集成SpringBoot(ElasticSearchRepository客户端)

内容2:项目主要功能代码说明

  1. 项目运行前提:(1) 开启ElasticSearch(xxx.xxx.xxx.xxx,普通用户身份);(2)开启redis(xxx.xxx.xxx.xxx,管理员用户身份);(3)开启kibana(xxx.xxx.xxx.xxx,管理员用户身份)。
  2. 配置ElasticSearch远程词典:
    Step1: 在本机cmd上,输入ipconfig查看本机ip,
    Step2: SpringBoot配置EditConfigures下的Working directory为 $MODULE_WORKING_DIR$
    Step3: SpringBoot远程词典位置如图4所示
    在这里插入图片描述
图4 SpringBoot远程词典位置

Step4: 启动springboot应用。

Step5: 虚拟机中ElasticSearch进行如下配置,配置完成后重启ElasticSearch:

vim elasticsearch-6.8.0/plugins/ik/config/IKAnalyzer.cfg.xml
# 而后配置如下:
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>IK Analyzer 扩展配置</comment>
<!--用户可以在这里配置自己的扩展字典 -->
<entry key="ext_dict"></entry>
 <!--用户可以在这里配置自己的扩展停止词字典-->
<entry key="ext_stopwords"></entry>
<!--用户可以在这里配置远程扩展字典 -->
<entry key="remote_ext_dict">http://192.168.1.104:8989/mypoem/init.dic</entry>
        <!--用户可以在这里配置远程扩展停止词字典-->
        <entry key="remote_ext_stopwords"></entry>
</properties>
  1. 功能:“基于基础数据重建ES索引库”(借此说明ES与SpringBoot的整合)
    Step1: 注意porm文件中,SpringBoot版本为2.2.5.RELEASE
    Step2: 配置entity类
package com.salieri.entity;

import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

@Document(type = "poem", indexName = "poems")
public class Poem {

    @Id
    private String id;
    @Field(analyzer = "ik_max_word", type = FieldType.Text)
    private String name;
    @Field(type = FieldType.Keyword)
    private String author;
    @Field(type = FieldType.Keyword)
    private String type;
    @Field(analyzer = "ik_max_word", type = FieldType.Text)
    private String content;
    @Field(type = FieldType.Keyword)
    private String href;
    @Field(analyzer = "ik_max_word", type = FieldType.Text)
    private String authordes;
    @Field(type = FieldType.Keyword)
    private String origin;
    @Field(type = FieldType.Nested)
    private Category category = new Category();

    ...
}
=================================================================
package com.salieri.entity;

import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

public class Category {

    @Field(type=FieldType.Keyword, index=false)
    private String id;
    @Field(type=FieldType.Keyword)
    private String name;

    ...
}

Step3: 配置SpirngBoot连接ES
新建com.salieri.config.RestClientConfig并进行如下配置:

package com.salieri.config;

import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;

@Configuration
public class RestClientConfig extends AbstractElasticsearchConfiguration {

    @Value("${elasticsearch.rest.uri}")
    private String uri;

    //RestHighLevelClient dao PeomRepository
    @Override
    @Bean
    public RestHighLevelClient elasticsearchClient() {
        final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
                .connectedTo(uri)
                .build();
        return RestClients.create(clientConfiguration).rest();
    }
}

Step4: 新建com.salieri.elastic.repository.PoemRepository并进行如下配置

package com.salieri.elastic.repository;

import com.salieri.entity.Poem;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

public interface PoemRepository extends ElasticsearchRepository<Poem,String> {
}
  1. 功能:“远程词典CRUD”
    controller->DicController实现远程词典的CURD
  2. 功能:“Redis热词统计“
    Step1: 引入segment的jar包:
<!--segment-->
<dependency>
     <groupId>com.github.houbb</groupId>
     <artifactId>segment</artifactId>
     <version>0.1.2</version>
</dependency>

segment的使用如图5所示:
在这里插入图片描述

图5 segment分词的使用

Step2:引入redis的jar包
<!--redis-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

Step3: 配置文件中配置redis:

spring.redis.host=192.168.94.140
spring.redis.port=7000

Step4: PoemController中的相关代码:

// 放入redis
if (!StringUtils.isEmpty(content)) {
    // 对搜索进行分词处理
    List<String> segment = SegmentHelper.segment(content, SegmentResultHandlers.word());

    log.info("当前搜索分词结果为:[{}]",segment);
    segment.forEach(word->{
      if (word.length()>1) {
          stringRedisTemplate.opsForZSet().incrementScore("keywords",word,0.5);
      }
  });
}

========================
     // 获取redis热词排行榜
    @RequestMapping("findRedisKeywords")
    public Set<ZSetOperations.TypedTuple<String>> findRedisKeywords(){
        Set<ZSetOperations.TypedTuple<String>> keywords = stringRedisTemplate.opsForZSet().reverseRangeWithScores("keywords", 0, 20);
        return keywords;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值