Skip to content

YuJhon/rainbow-search

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

73 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

基于ElasticSearch全局搜索项目实战

1.需求说明

待补充

2.数据库设计

(略)

3.项目工程结构

项目结构

4.开发细节

  • 视图解析器的配置

    /**
       * <pre>静态资源配置</pre>
       *
       * @param registry
       */
      @Override
      public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");
      }
    
      /**
       * 模板资源解释器
       *
       * @return
       */
      @Bean
      @ConfigurationProperties(prefix = "spring.thymeleaf")
      public SpringResourceTemplateResolver templateResolver() {
        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
        templateResolver.setApplicationContext(this.applicationContext);
        templateResolver.setCharacterEncoding("UTF-8");
        templateResolver.setCacheable(this.thymeleafCacheEnable);
        return templateResolver;
      }
    
      /**
       * Thymeleaf标准方言解释器
       *
       * @return
       */
      @Bean
      public SpringTemplateEngine templateEngine() {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver());
        /** 支持Spring EL表达式 **/
        templateEngine.setEnableSpringELCompiler(true);
        /** 支持SpringSecurity方言 **/
        SpringSecurityDialect securityDialect = new SpringSecurityDialect();
        templateEngine.addDialect(securityDialect);
        return templateEngine;
      }
    
      /**
       * Thymeleaf视图解释器
       *
       * @return
       */
      @Bean
      public ThymeleafViewResolver viewResolver() {
        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setTemplateEngine(templateEngine());
        return viewResolver;
      }
  • 返回信息的统一处理

TODO

  • 文件上传

    • 002.DispatcherServlet中的解释 002.DispatcherServlet中的解释
    • 003.MultipartResolver流程梳理 003.MultipartResolver流程梳理
    • 004.文件上传代码和配置 004.文件上传代码和配置
  • 分布式session实现

    • 1.maven 依赖
      <!-- redis session依赖 -->
      <dependency>
          <groupId>org.springframework.session</groupId>
          <artifactId>spring-session</artifactId>
      </dependency>
      
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-data-redis</artifactId>
      </dependency>
    • 2.配置信息
      #session(存储方式)
      spring.session.store-type=redis
      # redis config
      spring.redis.database=0
      spring.redis.host=127.0.0.1
      spring.redis.port=6379
      spring.redis.pool.min-idle=1
      spring.redis.timeout=3000
    • 3.代码实现
      package com.rainbow.house.search.config;
      
      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.Configuration;
      import org.springframework.data.redis.connection.RedisConnectionFactory;
      import org.springframework.data.redis.core.RedisTemplate;
      import org.springframework.data.redis.core.StringRedisTemplate;
      import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
      
      /**
       * <p>功能描述</br></p>
       *
       * @author jiangy19
       * @version v1.0
       * @projectName rainbow-search
       * @date 2018/6/1 9:07
       */
      @Configuration
      @EnableRedisHttpSession(maxInactiveIntervalInSeconds = 86400)
      public class RedisSessionConfig {
      
        @Bean
        public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
          return new StringRedisTemplate(factory);
        }
      }
  • ES的安装和配置

    • 1.安装和启动ES(详细请参考各种文档)

      • github插件Eslaticsearch
      • Es的配置文件配置
      • 安装和配置步骤(略)
    • 2.启动head插件 启动head插件

    • 3.访问http://localhost:9100 访问head插件

    • 4.项目中使用Elasticsearch

      #Es Config
      elasticsearch.cluster.name=JhonRain
      elasticsearch.host=127.0.0.1
      elasticsearch.port=9300
  • 索引的构建

    • 1.索引结构定义(json格式)

    • 2.RestfulAPI 创建索引

      • 2.1.创建索引
      • 通过接口创建索引
      • 2.2.查看索引结构
      • 查看索引结构
      • 2.3.单元测试
      • 测试代码001
      • 2.4.ES配置信息
      • EsConfig代码
    • 3.定义索引结构模板

    • 4.Es索引操作Restful API 使用

      • Elasticsearch标准分词查看
      • 创建一个测试索引
      • 添加索引数据
      • 添加三个测试索引数据
      • 中文分词测试结果
      • 删除之前建立的索引
      • 重新建立基于Ik的索引
      • 使用ik_smart分词器执行
      • 索引数据建立
      • 测试ik_smart的分词效果
    • 5.IK-Analysis插件的安装

      • IK插件的安装
  • Kafka的环境配置【windows】(使用Kafka来异步创建和更新索引)

    • 1.下载kafka

      • Apache官网
    • 2.修改zookeeper的配置文件

      • 存放log日志的地方
      • Kafka之zookeeper配置文件修改
    • 3.修改kafka的配置文件

      • 存放log日志的地方
      • Kafka配置文件的修改
      • 定义消费者的groupid
      • kafka定义cuonsumer的groupid
      • 详细可参考此文:window搭建kafka
    • 4.项目中使用kafka

      # kafka
      spring.kafka.bootstrap-servers=127.0.0.1:9092
      spring.kafka.consumer.group-id=rainbow-search
    • 启动命令

      • zookeeper
        D:\RAIN-MQ\kafka_2.11-1.1.0>bin\windows\zookeeper-server-start.bat config\zookeeper.properties
      • kafka
        D:\RAIN-MQ\kafka_2.11-1.1.0>bin\windows\kafka-server-start.bat config\server.properties
  • 百度地图的集成

    LBS字段定义

    创建LBS云应用

    百度LBS功能实现

5.单元测试

  • 测试基类的配置

    package com.rainbow.house.search;
    
    import org.junit.runner.RunWith;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.ActiveProfiles;
    import org.springframework.test.context.junit4.SpringRunner;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    @ActiveProfiles(value = "test")
    public class RainbowSearchApplicationTests {
    
    }
  • 测试类(用户测试类)

    package com.rainbow.house.search.entity;
    
    import com.rainbow.house.search.RainbowSearchApplicationTests;
    import com.rainbow.house.search.repository.UserRepository;
    import org.junit.Assert;
    import org.junit.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    
    /**
     * <p>功能描述</br>用户测试类</p>
     *
     * @author jiangy19
     * @version v1.0
     * @projectName rainbow-search
     * @date 2018/5/30 11:50
     */
    public class UserRepositoryTest extends RainbowSearchApplicationTests {
    
      @Autowired
      private UserRepository userRepository;
    
      @Test
      public void queryUserTest() {
        UserDO user = userRepository.findOne(1L);
        Assert.assertEquals(user.getName(),"jhonrain");
      }
    }
  • 测试类(ES)

    package com.rainbow.house.search.service.search;
    
    import com.rainbow.house.search.RainbowSearchApplicationTests;
    import com.rainbow.house.search.base.ServiceMultiResult;
    import com.rainbow.house.search.base.rent.RentSearchCondition;
    import com.rainbow.house.search.service.EsSearchService;
    import org.junit.Assert;
    import org.junit.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    
    /**
     * <p>功能描述</br>Es的测试类</p>
     *
     * @author jiangy19
     * @version v1.0
     * @projectName rainbow-search
     * @date 2018/6/2 14:46
     */
    public class EsServiceTest extends RainbowSearchApplicationTests {
    
      @Autowired
      private EsSearchService esSearchService;
    
      @Test
      public void indexTest() {
        Long houseId = 16L;
        esSearchService.indexVersionOne(houseId);
      }
    
      @Test
      public void removeIndexTest() {
        Long houseId = 16L;
        esSearchService.removeVersionOne(houseId);
      }
    
      @Test
      public void query() {
        RentSearchCondition rentSearchCondition = new RentSearchCondition();
        rentSearchCondition.setCityEnName("bj");
        rentSearchCondition.setStart(0);
        rentSearchCondition.setSize(10);
        ServiceMultiResult<Long> houseIds = esSearchService.query(rentSearchCondition);
        System.out.print(houseIds.getResults());
        Assert.assertEquals(2, houseIds.getTotal());
      }
    }

6.结束语