Windows环境下搭建 ElasticSearch,并与spring boot整合

ElasticSearch简介

ElasticSearch,简称ES, 是一个基于Lucene的分布式全文搜索服务器,都是基于分词和分段的全文搜索引擎,具有分词,同义词,词干查询的功能,但是ES天生具有分布式和实时的属性。

ElasticSearch安装

1、从官方下载中心 ElasticSearch Download 下载ElasticSearch安装包

2、将压缩包解压到E盘,进入E:\elasticsearch-7.3.0\bin目录,双击elasticsearch.bat

3、打开浏览器,输入 http://localhost:9200 ,显式以下画面,说明ES安装成功。

Windows环境下搭建 ElasticSearch,并与spring boot整合_第1张图片

安装head插件

为了方便管理ES可以使用head插件

1、首先要安装node.js

2、下载  elasticsearch-head-master 并解压到 E:\ELK\elasticsearch-head-master

3、进入elasticsearch-head-master目录执行npm install -g grunt-cli 命令

    grunt 是基于Node.js的项目构建工具,可以进行打包压缩、测试、执行等等的工作,head插件就是通过grunt启动

4、在elasticsearch-head-master目录执行npm install 安装对应的依赖

5、在elasticsearch-head-master目录启动运行head服务, 执行 grunt server 命令。

6、访问head管理页面,地址:http://localhost:9100/ 

Windows环境下搭建 ElasticSearch,并与spring boot整合_第2张图片

 

ElasticSearch与spring boot整合

一、添加对应的maven依赖


    
        org.springframework.boot
        spring-boot-starter-actuator
    
    
        org.springframework.boot
        spring-boot-starter-web
    

    
    
        org.springframework.boot
        spring-boot-starter-data-elasticsearch
    
    
    
        org.elasticsearch.plugin
        transport-netty4-client
        7.3.2
    

    

    
    
        org.projectlombok
        lombok
        true
    
    
    
        com.google.guava
        guava
        19.0
    
    
    
        com.alibaba
        fastjson
        1.2.47
     

    
        org.springframework.boot
        spring-boot-starter-test
        test
        
            
                org.junit.vintage
                junit-vintage-engine
            
        
    
    
        org.springframework.data
        spring-data-commons
        RELEASE
        compile
    

二、对application.properties配置文件进行设置

server.port=3000
spring.application.name=elastic-search-server

#开启 Elasticsearch 仓库(默认值:true)
spring.data.elasticsearch.repositories.enabled=true
#设置es连接
spring.data.elasticsearch.cluster-nodes=127.0.0.1:9200
#集群名(默认值: elasticsearch)
spring.data.elasticsearch.cluster-name=my-realestate
#连接超时的时间
spring.data.elasticsearch.properties.transport.tcp.connect_timeout=120s

三、代码实现

1、新建实体信息

/**
 * @author zhouzenghui
 * @title: Notice
 * @description: TODO index索引就相当于数据库,type就相当于表 
 * @date 2020/5/29 
 */
@Data
@Document(indexName = "books", type = "book")
public class Book implements Serializable {
 
    /**
     * id
     */
    @Id
    private Long id;

    /**
     * 标题
     */
    @Field
    private String title;

    /**
     * 公告标签
     */
    @Field
    private String label;

    /**
     * 公告发布时间
     */
    @Field
    private String createDate;

    /**
     * 公告阅读数量
     */
    @Field
    private Integer readNum;
}

2、基础ElasticsearchRepository接口

@Component
public interface BookRepository extends ElasticsearchRepository {
}

3、建立controller

@RestController
@RequestMapping("book")
public class BookController {

    @Autowired
    private BookRepository bookRepository;

    @ApiOperation(value = "添加保存")
    @GetMapping("save")
    public void save(long id, String title) {
        Book book = new Book();
        book.setId(id);
        book.setReadNum(100);
        book.setTitle(title);
        book.setLabel("java");
        book.setCreateDate(LocalDate.now().toString());
        bookRepository.save(book);
    }

    @ApiOperation(value = "批量添加")
    @GetMapping("saveAll")
    public void saveAll() {
        List lsit = new ArrayList<>();
        for (long i = 1; i < 15; i++) {
            Book book = new Book();
            book.setId(i);
            book.setReadNum(100);
            book.setLabel("java");
            book.setTitle("springboot整合elasticSearch2020年");
            book.setCreateDate(LocalDate.now().toString());
            lsit.add(book);
        }
        this.bookRepository.saveAll(lsit);
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping("findById")
    public Book findById(Long id) {
        Optional optionalNotice = bookRepository.findById(id);
        return optionalNotice.get();
    }

    @ApiOperation(value = "多个id查询")
    @GetMapping("findAllById")
    public List findAllById() {
        List ids = new ArrayList<>();
        ids.add(5L);
        ids.add(6L);
        ids.add(7L);
        Iterable iterable = this.bookRepository.findAllById(ids);
        List list = Lists.newArrayList(iterable);
        return list;
    }

    @ApiOperation(value = "查询总数量")
    @GetMapping("count")
    public Long count() {
        return this.bookRepository.count();
    }

    @ApiOperation(value = "查询所有")
    @GetMapping("findAll")
    public List findAll() {
        Iterable iterable = this.bookRepository.findAll();
        List list = Lists.newArrayList(iterable);
        return list;
    }

    @ApiOperation(value = "根据id删除数据")
    @GetMapping("deleteById")
    public void deleteById(Long id) {
        this.bookRepository.deleteById(id);
    }

    @ApiOperation(value = "删除所有")
    @GetMapping("deleteAll")
    public void deleteAll() {
        this.bookRepository.deleteAll();
    }

    @ApiOperation(value = "删除")
    @GetMapping("delete")
    public void delete(Book notice) {
        this.bookRepository.delete(notice);
    }

    @ApiOperation(value = "条件查询")
    @GetMapping("query")
    public List query(String title, Integer pageSize, Integer pageNumber) {
        //按照标题搜索
        QueryBuilder builder1 = QueryBuilders.matchQuery("title", title);
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.should(builder1);
        // 分页参数
        Pageable pageable = PageRequest.of(pageNumber, pageSize);

        Iterable iterable = this.bookRepository.search(boolQueryBuilder, pageable);
        List list = Lists.newArrayList(iterable);
        return list;
    }

}

以上是最近学习ElasticSearch的一些记录

 

你可能感兴趣的:(Windows环境下搭建 ElasticSearch,并与spring boot整合)