120.【ElastiSearch】(九)

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: 120.【ElastiSearch】

5.基本的索引 API操作 ⭐

(1).创建索引
package com.jsxs;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.io.IOException;
import static org.elasticsearch.client.RequestOptions.*;
@SpringBootTest
class JsxsEsApi01ApplicationTests {
    @Resource
    private RestHighLevelClient restHighLevelClient;
    // 测试索引的创建  Request
    @Test
    void  testCreateIndex() throws IOException {
        // 1.创建一个索引叫做 kuang_index的索引对象请求
        CreateIndexRequest request = new CreateIndexRequest("kuang_index");
        // 2.执行创建索引的请求 获得响应
        CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(request, DEFAULT);
        System.out.println(createIndexResponse);
    }
}

(2).获取-删除 索引
package com.jsxs;
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.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.io.IOException;
import static org.elasticsearch.client.RequestOptions.*;
@SpringBootTest
class JsxsEsApi01ApplicationTests {
    @Resource
    private RestHighLevelClient restHighLevelClient;
    @Test
    void contextLoads() {
    }
    // 测试索引的创建  Request
    @Test
    void  testCreateIndex() throws IOException {
        // 1.创建一个索引叫做 kuang_index的索引对象请求
        CreateIndexRequest request = new CreateIndexRequest("kuang_index");
        // 2.执行创建索引的请求 获得响应
        CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(request, DEFAULT);
        System.out.println(createIndexResponse);
    }
    // 测试获取索引的请求
    @Test
    void testExistIndex() throws IOException {
        // 获取一个索引名叫做kuang_index的索引库
        GetIndexRequest request = new GetIndexRequest("kuang_index");
        // 2.判断这个索引库是否存在?
        boolean exists = restHighLevelClient.indices().exists(request, DEFAULT);
        System.out.println(exists);
    }
    // 删除指定的索引
    @Test
    void testDeleteIndex() throws IOException {
        DeleteIndexRequest request = new DeleteIndexRequest("jsxs");
        AcknowledgedResponse delete = restHighLevelClient.indices().delete(request, DEFAULT);
        System.out.println(delete.isAcknowledged());
    }
}

6.基本的文档API操作 ⭐

<!--   引入我们的JSON包     -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>2.0.26</version>
        </dependency>
(1).新增文档
package com.jsxs;
import com.alibaba.fastjson2.JSON;
import com.jsxs.pojo.User;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
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.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.io.IOException;
import static org.elasticsearch.client.RequestOptions.*;
@SpringBootTest
class JsxsEsApi01ApplicationTests {
    @Resource
    private RestHighLevelClient restHighLevelClient;
    // 测试添加文档
    @Test
    void testAddDocument() throws IOException {
        // 1.创建添加的对象
        User user = new User("吉士先生", 3);
        // 2.创建索引请求操作
        IndexRequest request = new IndexRequest("kuang_index");
        // 3.规则 put 添加文档
            //  /索引/表/id
        request.id("1");  // 设置索引的id
        request.timeout(TimeValue.timeValueSeconds(1)); //设置过期时间为1秒
        // 4.将我们的数据放入请求 json
        request.source(JSON.toJSONString(user), XContentType.JSON);
        // 5.客户端发送请求
        IndexResponse index = restHighLevelClient.index(request, DEFAULT);
        // 6.
        System.out.println(index.toString());
        System.out.println(index.status());
    }
}

(2).查看文档 (是否存在)
package com.jsxs;
import com.alibaba.fastjson2.JSON;
import com.jsxs.pojo.User;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
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.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.io.IOException;
import static org.elasticsearch.client.RequestOptions.*;
@SpringBootTest
class JsxsEsApi01ApplicationTests {
    @Resource
    private RestHighLevelClient restHighLevelClient;
    // 获取文档,判断是否存在  get /索引/doc/id
    @Test
    void testIsExists() throws IOException {
        GetRequest getRequest = new GetRequest("kuang_index", "1");
        // 不获取返回的 _source 的上下文了。
        getRequest.fetchSourceContext(new FetchSourceContext(false));
        getRequest.storedFields("_none_");
        // 判断文档是否存在
        boolean flag = restHighLevelClient.exists(getRequest, DEFAULT);
        System.out.println(flag);
    }
}

(3).查看文档 (数据)
// 获取文档,判断是否存在  get /索引/doc/id
    @Test
    void testGetValue() throws IOException {
        GetRequest getRequest = new GetRequest("kuang_index", "1");
        // 1.客户端请求获得数据
        GetResponse getResponse = restHighLevelClient.get(getRequest, DEFAULT);
        // 2.打印我们的信息
        System.out.println(getResponse.getSourceAsString());
    }

(4).修改文档

根据索引和id

// 更新文档数据.
    @Test
    void testUpdateValue() throws IOException {
        // 1.更新索引为kuang_index的id为1的文档
        UpdateRequest request = new UpdateRequest("kuang_index", "1");
        // 2.设置超时的时间
        request.timeout("1s");
        User user = new User("aaaa", 111);
        // 3.第一个参数是: 转换为JSON对象,第二个参数是告诉别人我们是一个JSON类型
        request.doc(JSON.toJSONString(user),XContentType.JSON);
        // 4.客户端请求修改得数据
        UpdateResponse update = restHighLevelClient.update(request, DEFAULT);
        // 5.打印我们的信息
        System.out.println(update.toString());
    }

(5).删除文档
// 删除文档的记录
    @Test
    void testDeleteRequest() throws IOException {
        DeleteRequest request = new DeleteRequest("kuang_index", "1");
        // 1.假如请求超过1秒的话,那么就不再链接
        request.timeout("1s");
        // 2.请求客户端
        DeleteResponse delete = restHighLevelClient.delete(request, DEFAULT);
        // 3.打印信息
        System.out.println(delete.toString());
    }

(6).批量(添加/删除/修改)文档
@Test
    void testBulkRequest() throws IOException {
        BulkRequest bulkRequest = new BulkRequest();
        // 1.设置超时的时间
        bulkRequest.timeout("10s");
        // 2.利用链表存储数据
        ArrayList<User> users = new ArrayList<>();
        users.add(new User("jsxs1",1));
        users.add(new User("jsxs2",2));
        users.add(new User("jsxs3",3));
        users.add(new User("jsxs4",4));
        users.add(new User("jsxs5",5));
        users.add(new User("jsxs6",6));
        users.add(new User("jsxs7",7));
        users.add(new User("jsxs8",8));
        users.add(new User("jsxs9",9));
        users.add(new User("jsxs10",10));
        // 3.批量添加的操作
        for (int i = 0; i < users.size(); i++) {
        // 批量更新和删除就在这里有一个区别 ⭐
            bulkRequest.add(new IndexRequest("kuang_index")
            .id(""+i+1)
            .source(JSON.toJSONString(users.get(i)),XContentType.JSON)
            );
        }
        // 4.客户端请求
        BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, DEFAULT);
        System.out.println(bulk.toString());
    }

相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
相关文章
|
索引
120.【ElastiSearch】(十)
120.【ElastiSearch】
59 1
|
JSON Java 数据格式
120.【ElastiSearch】(六)
120.【ElastiSearch】
59 0
|
存储 监控 关系型数据库
120.【ElastiSearch】(三)
120.【ElastiSearch】
97 0
|
自然语言处理 算法 数据库
120.【ElastiSearch】(四)
120.【ElastiSearch】
55 0
|
自然语言处理 索引
120.【ElastiSearch】(七)
120.【ElastiSearch】
68 0
|
搜索推荐 Java 大数据
120.【ElastiSearch】(一)
120.【ElastiSearch】
67 0
|
SQL JavaScript 数据可视化
120.【ElastiSearch】(二)
120.【ElastiSearch】
56 0
|
5月前
|
存储 自然语言处理 搜索推荐
【技术解析 | 实践】Havenask分析器
本次分享内容为Havenask的分析器,本次课程主要分为3部分内容(分析器介绍、解释分析器主要配置、实战演示),希望本次通过分享帮助大家更好了解和使用Havenask。
52248 3
【技术解析 | 实践】Havenask分析器
|
4月前
|
网络安全 数据库 Python
常用百宝箱——日志处理
常用百宝箱——日志处理
|
NoSQL Java
120.【ElastiSearch】(八)
120.【ElastiSearch】
57 0