SpringBoot整合Elasticsearch

安装流程: https://blog.csdn.net/qq_37598011/article/details/103140376

Elasticsearch与Solr的比较

  • 当单纯的对已有数据进行搜索时,Solr更快。
  • 当实时建立索引时, Solr会产生io阻塞,查询性能较差, Elasticsearch具有明显的优势。
  • 随着数据量的增加,Solr的搜索效率会变得更低,而Elasticsearch却没有明显的变化。
  • 综上所述,Solr的架构不适合实时搜索的应用。
  • Solr 利用 Zookeeper 进行分布式管理,而 Elasticsearch 自身带有分布式协调管理功能;
  • Solr 支持更多格式的数据,而 Elasticsearch 仅支持json文件格式;
  • Solr 官方提供的功能更多,而 Elasticsearch 本身更注重于核心功能,高级功能多有第三方插件提供;
  • Solr 在传统的搜索应用中表现好于 Elasticsearch,但在处理实时搜索应用时效率明显低于 Elasticsearch。
  • Solr 是传统搜索应用的有力解决方案,但 Elasticsearch 更适用于新兴的实时搜索应用。

ES的主要应用分为两大类:

  1. 搜索类(带上聚合),考虑事务性,频繁更新,与现有数据库进行同步,通过ES进行查询聚合。
  2. 日志类,包括日志收集,指标性收集,通过beats等工具收集到kafka等Q中,通过logstash进行转换,输送到ES中,然后通过Kibana进行展示。
     

1.POM依赖添加/YML配置



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.6.RELEASE
         
    
    com.zzf
    es-demo
    0.0.1-SNAPSHOT
    es-demo
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-data-elasticsearch
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
        
    


访问: http://127.0.0.1:9200/

SpringBoot整合Elasticsearch_第1张图片

 

spring:
  data:
    elasticsearch:
      cluster-name: elasticsearch
      cluster-nodes: 127.0.0.1:9300

 2.启动类添加@EnableElasticsearchRepositories注解

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;

@SpringBootApplication
@EnableElasticsearchRepositories(basePackages = "com.zzf.es")
public class EsDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(EsDemoApplication.class, args);
    }

}

3.实体类创建

@Field(type=FieldType.Text, analyzer="ik_max_word")     表示该字段是一个文本,并作最大程度拆分,默认建立索引

@Field(type=FieldType.Text,index=false)             表示该字段是一个文本,不建立索引

@Field(type=FieldType.Date)                                表示该字段是一个文本,日期类型,默认不建立索引

@Field(type=FieldType.Long)                               表示该字段是一个长整型,默认建立索引

@Field(type=FieldType.Keyword)                         表示该字段内容是一个文本并作为一个整体不可分,默认建立索引

@Field(type=FieldType.Float)                               表示该字段内容是一个浮点类型并作为一个整体不可分,默认建立索引

 

import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;

@Data
@Document(indexName = "my_user", type = "user")
public class UserEntity {
    @Id
    private String id;
    private String name;
    private int sex;
    private int age;
}

4.Reposiory接口类添加

import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

public interface UserReposiory extends ElasticsearchRepository {

}

5.测试

SpringBoot整合Elasticsearch_第2张图片


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@RestController
public class EsController {

    @Autowired
    private UserReposiory userReposiory;

    @PostMapping("/addUser")
    public UserEntity addUser(@RequestBody UserEntity user) {
        return userReposiory.save(user);
    }

    @GetMapping("/findUser")
    public Optional findUser(@RequestParam("id") String id) {
        return userReposiory.findById(id);
    }
}

SpringBoot整合Elasticsearch_第3张图片

SpringBoot整合Elasticsearch_第4张图片

 

你可能感兴趣的:(Elasticsearch,SpringBoot,elasticsearch,java,spring,boot)