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

Springboot集成Elastic

Springboot集成Elastic

1.环境准备

Linux中使用docker安装ELK详解
Elastic_search官方文档

2. springboot集成elasticsearch 2.1 导入pom.xml依赖

根据自己es版本选择相应的依赖maven版本

    
        1.8
        7.6.0
    

        
        
            org.springframework.boot
            spring-boot-starter-data-elasticsearch
        
    

2.2 编写es配置
@Configuration
public class ElasticSearchConfig {

    @Bean
    public RestHighLevelClient restHighLevelClient() {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("127.0.0.1", 9200, "http")
                ));
        return client;
    }
}
2.3 编写服务类
public interface EsService {

    void createIndex(String indexName) throws IOException;

    boolean existsIndex(String indexName) throws IOException;

    void delIndex(String indexName) throws IOException;

    void getdocument(String indexName,String docName) throws IOException;

    void adddocument(String indexName,String docName, Object obj) throws IOException;

    void updateDocment(String indexName,String docName, Object obj) throws IOException;

    void deleteDocment(String indexName,String docName) throws IOException;

    void search(String indexName, TermQueryBuilder query) throws IOException;

    void searchHighLight(String indexName, TermQueryBuilder query) throws IOException;
}
@Service
public class EsServiceImpl implements EsService {
    private RestHighLevelClient restHighLevelClient;

    public EsServiceImpl(RestHighLevelClient restHighLevelClient) {
        this.restHighLevelClient = restHighLevelClient;
    }

    @Override
    public void createIndex(String indexName) throws IOException {
        // 1. 创建索引请求
        CreateIndexRequest firstIndex = new CreateIndexRequest(indexName);

        // 2. 客户端执行创建索引的请求
        CreateIndexResponse response = restHighLevelClient.indices().create(firstIndex, RequestOptions.DEFAULT);

        System.out.println(response);
    }

    @Override
    public void delIndex(String indexName) throws IOException {
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(indexName);
        AcknowledgedResponse delete = restHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
        System.out.println(delete);
    }



    @Override
    public boolean existsIndex(String indexName) throws IOException {
        // 1. 创建一个get请求获取指定索引的信息
        GetIndexRequest getIndexRequest = new GetIndexRequest(indexName);

        // 2. 客户端执行请求判断索引是否存在
       return restHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
    }

    @Override
    public void adddocument(String indexName,String docName, Object obj) throws IOException {

        // 2. 创建请求并指定索引
        IndexRequest indexRequest = new IndexRequest(indexName);

        // 3. 创建的规则:put /indexName/_doc/docName
        // 设置ID
        indexRequest.id(docName);
        // 设置超时时间
        indexRequest.timeout(Timevalue.timevalueSeconds(1));

        // 4. 将数据放入到请求中
        indexRequest.source(JSON.toJSONString(obj), XContentType.JSON);

        // 5. 使用客户端发送请求
        IndexResponse index = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);

        System.out.println(JSON.toJSONString(index));
    }


    @Override
    public void updateDocment(String indexName,String docName, Object obj) throws IOException {
        // 1. 创建一个更新请求的信息,绑定索引名称和需要更新的文档ID
        UpdateRequest updateRequest = new UpdateRequest(indexName, docName);
        // 设置超时时间
        updateRequest.timeout(Timevalue.timevalueSeconds(1));
        // 2. 封装需要更新的文档信息
        updateRequest.doc(JSON.toJSONString(obj), XContentType.JSON);

        // 3. 使用update更新文档
        UpdateResponse updateResponse = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);

        System.out.println(JSON.toJSONString(updateResponse));
    }

    @Override
    public void deleteDocment(String indexName, String docName) throws IOException {
        // 1. 创建一个删除的请求,绑定索引名和需要删除的文档ID
        DeleteRequest deleteRequest = new DeleteRequest(indexName, docName);


        // 2. 发起删除请求
        DeleteResponse response = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);

        System.out.println(JSON.toJSONString(response));
    }

    @Override
    public void getdocument(String indexName, String docName) throws IOException {
        GetRequest getRequest = new GetRequest(indexName,docName);
        GetResponse documentFields = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
        System.out.println(documentFields.getSourceAsString());
        System.out.println(documentFields);
    }

    @Override
    public void search(String indexName,TermQueryBuilder query) throws IOException {
        // 1. 创建批量搜索请求,并绑定索引
        SearchRequest searchRequest = new SearchRequest(indexName);

        // 2. 构建搜索条件
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

        sourceBuilder.query(query).timeout(Timevalue.timevalueSeconds(60));

        // 3. 将查询条件放入搜索请求request中
        searchRequest.source(sourceBuilder);

        // 4. 发起查询请求获取数据
        SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        System.out.println(JSON.toJSONString(response));
        System.out.println(JSON.toJSONString(response.getHits().getHits()));

    }

    @Override
    public void searchHighLight(String indexName, TermQueryBuilder query) throws IOException {
        // 1. 创建批量搜索请求,并绑定索引
        SearchRequest searchRequest = new SearchRequest(indexName);

        // 2. 构建搜索条件
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        // 文字高亮
        sourceBuilder.highlighter();

        sourceBuilder.query(query).timeout(Timevalue.timevalueSeconds(60));

        // 3. 将查询条件放入搜索请求request中
        searchRequest.source(sourceBuilder);

        // 4. 发起查询请求获取数据
        SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
    }
}

2.4 测试类
@RunWith(SpringRunner.class)
@SpringBootTest
class ElasticsearchDemoApplicationTests {

    @Autowired
    private EsService esService;


    @Test
    void createIndex() throws IOException {
        esService.createIndex("yx-0176");
    }

    @Test
    void existsIndex() throws IOException {
        esService.existsIndex("yx-0176");
    }
    @Test
    void delIndex() throws IOException {
        esService.delIndex("yx-0176");
    }
    @Test
    void adddocument() throws IOException {
        Map map = new HashMap();
        map.put("name","zs");
        map.put("age","20");
        esService.adddocument("yx-0176","test",map);
    }
    @Test
    void updateDocment() throws IOException {
        Map map = new HashMap();
        map.put("name","zs");
        map.put("age","21");
        esService.updateDocment("yx-0176","test",map);
    }

    @Test
    void deleteDocment() throws IOException {
        esService.deleteDocment("yx-0176","test");
    }

    @Test
    void getDocment() throws IOException {
        esService.getdocument("yx-0176","test");
    }

    @Test
    void search() throws IOException {

        // 【注意:这里有个坑,当中文查询时,如果使用ik分词器会查询不到数据,属性需要使用xxx.keyword才能查询到数据】
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name.keyword", "zs");
//        QueryBuilders.matchAllQuery();
        esService.search("yx-0176",termQueryBuilder);
        esService.searchHighLight("yx-0176",termQueryBuilder);
    }
}

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

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

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