首页 > 其他分享 >ES操作索引

ES操作索引

时间:2023-09-16 20:47:02浏览次数:21  
标签:索引 client elasticsearch org indices import EsConstant 操作 ES

package cn.itcast.hotel;

import cn.itcast.hotel.constant.EsConstant;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
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.client.indices.GetIndexResponse;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;

@SpringBootTest
class HotelDemoApplicationTests {
    @Autowired
    private RestHighLevelClient client;
    @Test
    void contextLoads() throws IOException {
        client.close();
    }
    /**
     * 创建索引库
     */
    @Test
    void createIndexTest() {
        //1.创建Request对象
        CreateIndexRequest request = new CreateIndexRequest(EsConstant.HOTEL_INDEX);
        //2.指定映射。请求参数,EsConstant.HOTEL_MAPPING是静态常量字符串,内容是创建索引库的DSL语句
        request.source(EsConstant.HOTEL_MAPPING, XContentType.JSON);
        //3.创建索引
        try {
            CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
            String index = response.index();
            boolean acknowledged = response.isAcknowledged();
            System.out.println(index + ":" + acknowledged);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 查询索引 查询不到抛出[hotel] ElasticsearchStatusException[Elasticsearch exception [type=index_not_found_exception, reason=no such index [hotel]]
     * @Description: HotelDemoApplicationTests
     * @Author: regular
     * @Version: 1.0
    */
    @Test
    public void getIndexTest() throws IOException {
        GetIndexRequest getIndexRequest = new GetIndexRequest(EsConstant.HOTEL_INDEX);
        GetIndexResponse getIndexResponse = client.indices().get(getIndexRequest, RequestOptions.DEFAULT);
        System.out.println(getIndexResponse);
    }
    /**
     * 判断索引库是否存在
     * @Description: HotelDemoApplicationTests
     * @Author: regular
     * @Version: 1.0
     */
    @Test
    public void existsIndexTest() throws IOException {
        GetIndexRequest getIndexRequest = new GetIndexRequest(EsConstant.HOTEL_INDEX);
        boolean flag = client.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
        System.out.println(flag);
    }
    /**
     * 删除索引
     * @Description: HotelDemoApplicationTests
     * @Author: regular
     * @Version: 1.0
     */
    @Test
    public void deleteIndexTest() throws IOException {
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(EsConstant.HOTEL_INDEX);
        AcknowledgedResponse acknowledgedResponse = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
        System.out.println(acknowledgedResponse);
    }
}

 

标签:索引,client,elasticsearch,org,indices,import,EsConstant,操作,ES
From: https://www.cnblogs.com/qdcream/p/17707275.html

相关文章

  • 【php基础】php连接mysql数据库及基本操作02
    一、php连接mysql数据库<?php$servername="localhost";$username="root";$password="root";$dbname="mysql";//创建连接$conn=newmysqli($servername,$username,$password,$dbname);//检测连接i......
  • ES相关的增删改查操作示例
    依赖:<dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId></dependency>修改版本: 第一步:条件准备:首先是配置类:@ConfigurationpublicclassEsConfig{......
  • Shell操作符说明
    Shell操作符说明说明[[]][]、test()、(())=用于赋值、判断==数值比较字符串比较文件比较......
  • [Servlet/Tomcat] HttpServletRequest#getHeader(headerNameWithIgnoreCase)(获取heade
    1故事背景最近项目上有个业务需求,翻译成技术需求,即:将request.headers中的几个header入参转换成request.body(pageRequest)中的内置参数。为便于灵活配置,header参数名称是动态可配置的(存放于nacos配置中心),比如:sysCode、Accept-Language技术实现,主要就springmvc的org.spr......
  • 深入了解Elasticsearch搜索引擎篇:倒排索引、架构设计与优化策略
    什么是倒排索引?有什么好处?倒排索引是一种用于快速检索的数据结构,常用于搜索引擎和数据库中。与传统的正排索引不同,倒排索引是根据关键词来建立索引,而不是根据文档ID。倒排索引的建立过程如下:首先,将每个文档拆分成一系列的关键词或词项,然后建立一个词项到文档的映射。对每个关键词......
  • HBase学习6(大量数据的导入及操作java)
    在HBase中,有一个Import的MapReduce作业,可以专门用来将数据文件导入到HBase中。hbaseorg.apache.hadoop.hbase.mapreduce.Import表名HDFS数据文件路径1.导入数据1.将资料中数据文件上传到Linux中2.再将文件上传到hdfs中hadoopfs-mkdir-p/water_bill/output_ept_......
  • BeanUtils.copyProperties用法
    //获取ActionForm表单数据UserActionFormuForm=(UserActionForm)form;//构造一个User对象Useruser=newUser();//赋值(部分==》整体)BeanUtils.copyProperties(uForm,user);注意点  1、UserActionForm==》User:部分到整体;  2、如果User和UserActionFor......
  • CUDA memories
    GlobalThere'salargeamountofglobalmemory.It'sslowertoaccessthanothermemorylikesharedandregisters.AllrunningthreadscanreadandwriteglobalmemoryandsocantheCPU.ThefunctionscudaMalloc,cudaFree,cudaMemcpy,cud......
  • C#实现SSH、SCP、FTP等操作
    C#实现SSH、SCP、FTP等操作C#没有自带的SSH、SCP、FTP等操作的方法库,自己编写又太麻烦,这里将使用第三方Renci.SshNet.dll动态链接库实现这些操作 一、获取RENCI.SSHNET.DLL 二、将RENCI.SSHNET.DLL添加进C#工程我使用visualstudio2015作为IDE,将Renci.SshNet.dll添加进......
  • XMind2TestCase安装问题
     安装完成了XMind2TestCase之后,在命令端检查安装是否成功,报错:C:\Users\Administrator>xmind2testcaseTraceback(mostrecentcalllast): File"C:\Softwares\Python\Scripts\xmind2testcase-script.py",line33,in<module> sys.exit(load_entry_point('......