`
zysnba
  • 浏览: 179626 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

elasticsearch7.6.2集成springboot2.2.6基本语法---3

阅读更多
package com.zys.es.demo762;

import com.alibaba.fastjson.JSON;
import com.zys.es.demo762.confing.User;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@SpringBootTest
class Demo762ApplicationTests {
    @Autowired
    @Qualifier("restHighLevelClient")
    private RestHighLevelClient client;
    /**
     * 创建索引
     * @throws IOException
     */
    @Test
    public void testAdd() throws IOException {
        // 创建索引请求
        CreateIndexRequest request = new CreateIndexRequest("zysnba");
        request.setTimeout(TimeValue.timeValueMinutes(2));//设置创建索引超时2分钟
        CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
        System.out.println("createIndex: " + JSON.toJSONString(createIndexResponse));
        client.close();
    }

    //索取索引
    @Test
    void testExistIndex() throws IOException {
        //1,创建索引请求
        GetIndexRequest request = new GetIndexRequest("zysnba");
        //2,执行请求,
        boolean bool= client.indices().exists(request, RequestOptions.DEFAULT);
        System.out.println(bool);
    }
    //删除索引
    @Test
    void testDeleteIndex() throws IOException {
        //1,创建索引请求
        DeleteIndexRequest request = new DeleteIndexRequest("zysnba");
        //2,执行请求,
        AcknowledgedResponse response= client.indices().delete(request,RequestOptions.DEFAULT);
        System.out.println("createIndex: " + JSON.toJSONString(response));
    }


    //添加文档
    @Test
    void testAddDocument() throws IOException {
        User user = new User("张三",12);
        //创建链接
        IndexRequest request=new IndexRequest("zysnba");
        request.id("1");
        request.timeout("2s");
        //将我们的数据放到es中,json
        request.source(JSON.toJSONString(user), XContentType.JSON);
        /* 客服端发送请求 */
        IndexResponse indexResponse= client.index(request,RequestOptions.DEFAULT);
        System.out.println(indexResponse.toString());
        System.out.println(indexResponse.status());
    }
    //获取判断是否存在文档
    @Test
    void testIsExistsDocument() throws IOException {
        //创建链接
        GetRequest request=new GetRequest("zysnba","1");
        request.fetchSourceContext(new FetchSourceContext(false));
        request.storedFields("_none_");
        boolean bool= client.exists(request,RequestOptions.DEFAULT);
        /* 客服端发送请求 */
        System.out.println(bool);
    }
    //获取判断是否存在文档
    @Test
    void testGetDocument() throws IOException {
        //创建链接
        GetRequest request=new GetRequest("zysnba","1");
        GetResponse response=client.get(request,RequestOptions.DEFAULT);
        System.out.println(response.getSourceAsString());
        System.out.println(response);
    }

    //更新文档信息
    @Test
    void testUpdateDocument() throws IOException {
        //创建链接
        UpdateRequest request= new UpdateRequest("zysnba","1");
        request.timeout("1s");
        User user = new User("张三好修改了",14);
        request.doc(JSON.toJSONString(user), XContentType.JSON);
        UpdateResponse update = client.update(request, RequestOptions.DEFAULT);
        System.out.println(update.getGetResult());
        System.out.println(update.status());
    }

    //删除文档信息
    @Test
    void testDeleteDocument() throws IOException {
        //创建链接
        DeleteRequest request= new DeleteRequest("zysnba","1");
        request.timeout("1s");
        DeleteResponse delete = client.delete(request, RequestOptions.DEFAULT);
        System.out.println(delete.status());
    }


    //添加文档
    @Test
    void testBulkAddDocument() throws IOException {
        //创建链接
        BulkRequest request=new BulkRequest();
        request.timeout("2s");
        List<User> list= new ArrayList();
        User user1 = new User("张三好1",12);
        User user2 = new User("张三好2",12);
        User user3 = new User("张三好3",12);
        list.add(user1);
        list.add(user2);
        list.add(user3);
       //批处理请求
        for(int i =0;i<list.size();i++  ){
            //批量更新和批量删除,就在这里修改对应的请求即可
            request.add(new IndexRequest("zysnba")
                    //如果setId,自带生成id
//                    .id(""+(i+1))
                    .source(JSON.toJSONString(list.get(i)),XContentType.JSON));
        }
        BulkResponse bulk = client.bulk(request, RequestOptions.DEFAULT);
        System.out.println(bulk.status());
        //是否失败,返回false代表成功
        System.out.println(bulk.hasFailures());
    }

    //查询
    @Test
    void testSearch() throws IOException {
        SearchRequest searchRequest = new SearchRequest("zysnba");
        //构造搜索条件
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //查询条件,我们可以使用queryBuilder 工具来实现
        //termQuery 精确匹配,注意中文的时候一定要加上name.keyword 坑
        //matchAllQuery() 匹配所有
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "zys1");
        searchSourceBuilder.query(termQueryBuilder);
        searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
        searchRequest.source(searchSourceBuilder);
        SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
        System.out.println(JSON.toJSONString(search));
        System.out.println(JSON.toJSONString(search.getHits()));
        System.out.println("--------------------");
        for (SearchHit hit : search.getHits().getHits()) {
            System.out.println(hit.getSourceAsMap());
        }
    }
}
分享到:
评论

相关推荐

    elasticsearch-analysis-dynamic-synonym-7.6.2

    `elasticsearch-analysis-dynamic-synonym-7.6.2`是一个专为ES7.6.2版本设计的动态同义词插件,旨在实现同义词的动态加载和管理,使得同义词库可以随着业务需求的变化而实时更新。 **一、动态同义词插件的作用** 1...

    elasticsearch-analysis-ik-7.6.2.zip

    此插件在版本 7.6.2 中提供了对 Elasticsearch 7.6.2 的支持,旨在提供更加精准和灵活的中文分词功能。 IK 分词器的核心特性包括: 1. **智能切分**:IK 分词器能够根据上下文智能地进行词语切分,处理多义词问题...

    最新版 elasticsearch-analysis-ik-7.6.2.zip

    "elasticsearch-analysis-ik-7.6.2.zip" 是针对Elasticsearch 7.6.2版本的一个专门用于中文分词的插件,其目的是优化对中文文本的索引和查询性能。 IK分析器是Elasticsearch社区中非常流行的一个中文分词插件,由...

    springboot整合es-springboot-elasticsearch.zip

    本项目"springboot-elasticsearch-master"提供了如何将SpringBoot与Elasticsearch集成的示例。 一、SpringBoot简介 SpringBoot是由Pivotal团队提供的全新框架,其设计目标是简化Spring应用的初始搭建以及开发过程。...

    elasticsearch-7.6.2-windows-x86_64安装包,无需积分下载

    3. **自动索引管理**:Elasticsearch支持自动创建、管理和更新索引,索引是存储和检索文档的基本单位。每个索引都有一个或多个分片,分片可以分布在不同的节点上,从而实现水平扩展。 4. **全文搜索**:基于Lucene...

    springboot-elasticsearch-master.rar

    在项目中,"springboot-elasticsearch-master"的实现主要依赖于Spring Data Elasticsearch库,这是一个Spring Data模块,它提供了对Elasticsearch的集成,包括对Elasticsearch的操作接口和实体映射功能。通过这个库...

    es整合springboot-elasticsearch-demo.zip

    **标题解析:** "es整合springboot-elasticsearch-demo" 这个标题暗示了这是一个关于如何将Elasticsearch集成到Spring Boot应用的示例项目。Elasticsearch是一个强大的分布式搜索引擎,而Spring Boot则是一个用于...

    ES7.6.2+ik7.6.2+head7.6.2+logstash-7.6.2.zip

    3. **Kibana (head7.6.2)**:Kibana 是一个数据可视化工具,它允许用户通过 Web 界面与 Elasticsearch 数据交互,创建仪表板、图表和报告。虽然通常 Kibana 的官方版本被称为 "Kibana" 而非 "head",但 "head" 在...

    Elasticsearch 7.6.2(已集成ik分词器)

    Elasticsearch 7.6.2 已集成ik分词器 解压后运行bin目录下,elasticsearch.bat文件

    Java开发案例-springboot-04-整合Elasticsearch-源代码+文档.rar

    Java开发案例-springboot-04-整合Elasticsearch-源代码+文档.rar Java开发案例-springboot-04-整合Elasticsearch-源代码+文档.rar Java开发案例-springboot-04-整合Elasticsearch-源代码+文档.rar Java开发案例-...

    128元尚硅谷Java视频教程_Spring Boot视频教程(下)整合篇

    本视频《SpringBoot高级》属于下部,着重介绍SpringBoot的与各大场景的整合使用,内容包括:缓存(整合Redis),消息中间件(整合RabbitMQ),检索(整合ElasticSearch),任务(异步任务,定时任务,邮件任务),...

    springboot-elasticsearch-pom

    springboot-elasticsearch-pom文件,可以参考

    springboot和一些主流框架的整合的各个基本demo

    hello word 可能性版本 Springboot-web 网页版本 ...Springboot-elasticsearch 与Elasticsearch整合 Springboot-i18n SpringBoot国际化配置 SpringBoot-多源 SpringBoot多数据源配置,全局异常自定义处理

    springboot-elasticsearch-example

    本项目"springboot-elasticsearch-example"旨在演示如何将Spring Boot与Elasticsearch整合,以实现快速、便捷的数据检索功能。以下是关于这两个技术及其整合的详细知识: **Spring Boot** Spring Boot是由Pivotal...

    Java开发案例-springboot-05-整合Easy-Es实现搜索-源代码+文档.rar

    Java开发案例-springboot-05-整合Easy-Es实现搜索-源代码+文档.rarJava开发案例-springboot-05-整合Easy-Es实现搜索-源代码+文档.rarJava开发案例-springboot-05-整合Easy-Es实现搜索-源代码+文档.rarJava开发案例-...

    elasticsearch-7.6.2.rar

    这个压缩包"elasticsearch-7.6.2.rar"包含了该版本的完整安装文件,适用于对Elasticsearch进行快速部署和测试。 在Elasticsearch中,分词器扮演了至关重要的角色,它负责将输入的文本拆分成可索引的基本单元——...

    elasticsearch-analysis-ik-7.6.2

    IK分词器

    springboot集成各种技能 activiti security-cas-sso rabbitmq

    springboot-activiti springboot-actuator springboot-elasticsearch springboot-flowable springboot-rabbitmq springboot-security-oauth2-sso springboot-shiro ... 各种集成 纯手写 java 开发者可以看一下

    springboot-0.0.1-SNAPSHOT.jar

    springboot-0.0.1-SNAPSHOT.jar

    Springboot-的各种demo.rar

    Springboot-elasticsearch 与 Elasticsearch 整合 Springboot-i18n SpringBoot 国际化配置 SpringBoot-multi-source SpringBoot 多数据源配置,全局异常自定义处理 SpringBoot2-mybatisplus SpringBoot 与mybatis-...

Global site tag (gtag.js) - Google Analytics