栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Springboot接入ES并实现查询

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Springboot接入ES并实现查询

文章目录
  • setp1 POM文件
  • step2 yaml配置
  • step3 注入工具类
  • step4 使用

setp1 POM文件

版本约定:springboot2.1.6.RELEASE + es7.6.2



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.6.RELEASE
    
    org.example
    esLearn
    1.0-SNAPSHOT

    
        8
        8
        7.6.2
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.projectlombok
            lombok
            1.18.20
            provided
        
        
        
            org.springframework.data
            spring-data-elasticsearch
            3.2.5.RELEASE
        
        
        
            com.alibaba
            fastjson
            2.0.2
        

    


step2 yaml配置
es:
  host: {your es host}
  port: {your es port}
step3 注入工具类
@Configuration
public class EsConfig {
    @Value("${es.host}")
    private String esHost;
    @Value("${es.port}")
    private int esPort;

    @Bean
    public RestHighLevelClient restHighLevelClient() {
        return new RestHighLevelClient(RestClient.builder(new HttpHost(esHost, esPort, "http")));
    }
}

step4 使用
@Service
public class EsService {

    @Resource
    private RestHighLevelClient restHighLevelClient;

    public void es() throws IOException {
        // 创建索引(不恰当的类似于mysql的table)
        CreateIndexRequest create = new CreateIndexRequest("x_index");
        CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(create, RequestOptions.DEFAULT);
        System.out.println(createIndexResponse.toString());

        // 获取索引(只能判读是否存在)
        GetIndexRequest getIndexRequest = new GetIndexRequest("x_index");
        boolean exists = restHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
        System.out.println(exists);

        // 删除索引
        DeleteIndexRequest xu_index = new DeleteIndexRequest("x_index");
        AcknowledgedResponse delete = restHighLevelClient.indices().delete(xu_index, RequestOptions.DEFAULT);
        System.out.println(delete.isAcknowledged());

        // 文档添加数据
        User user = new User(1, "xiaohong", 12);
        IndexRequest docAdd = new IndexRequest("x_index");
        docAdd.id("1");
        docAdd.timeout(TimeValue.timeValueSeconds(1));
        docAdd.source(JSON.toJSONString(user), XContentType.JSON);
        IndexResponse response = restHighLevelClient.index(docAdd, RequestOptions.DEFAULT);
        System.out.println(response.toString());
        System.out.println(response.status());
        
        // 文档查询数据
        GetRequest getReq = new GetRequest("x_index", "1");
        boolean exists = restHighLevelClient.exists(getReq, RequestOptions.DEFAULT);
        if(exists) {
        	GetResponse documentFields = restHighLevelClient.get(getReq, RequestOptions.DEFAULT);
		}
        System.out.println(documentFields.getSourceAsString());
    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/962409.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号