栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

springboot整合es

springboot整合es

添加依赖
 
        
            org.elasticsearch.client
            elasticsearch-rest-client
            7.6.2
        
        
            org.elasticsearch.client
            elasticsearch-rest-high-level-client
            7.6.2
            
                
                    elasticsearch-rest-client
                    org.elasticsearch.client
                
                
                    org.elasticsearch
                    elasticsearch
                
            
        
        
            org.elasticsearch
            elasticsearch
            7.6.2
        
配置端口和地址
@Configuration
public class ESConfig {
    @Bean
    public RestHighLevelClient restHighLevelClient(){
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("192.168.8.125", 9200, "http")));
        return client;
    }
}

测试
package com.kv.es.service.impl;

import com.kv.es.model.SearchQuery;
import com.kv.es.service.SearchService;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;


@Service
public class SearchServiceImpl implements SearchService {
    @Autowired
    private RestHighLevelClient restHighLevelClient;
    
    @Override
    public void search(SearchQuery searchQuery) {
        // 创建请求对象
        GetRequest request = new GetRequest().index("knowledge_base").id("1");
        try {
            // 客户端发送请求,获取响应对象
            GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT);
            //打印结果
            System.out.println("_index:"+response.getIndex());
            System.out.println("_type:"+response.getType());
            System.out.println("_id:"+response.getId());
            System.out.println("source:"+response.getSourceAsString());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

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

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

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