当前位置: 首页 > article >正文

028 elasticsearch索引管理-ElasticsearchRestTemplate

文章目录

    • pom.xml
    • application.yml
    • CubemallSearchApplication.java
    • RestClientTest.java
    • 使用ElasticsearchRestTemplate对象
      • Blog.java
      • RestTemplateTest.java

pom.xml

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>

application.yml

spring:
  elasticsearch:
    rest:
      uris:
        - 1.1.1.1:9200
        - 2.2.2.2:9200
        - 3.3.3.3:9200

CubemallSearchApplication.java

package com.xd.cubemall.search;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class CubemallSearchApplication{
    public static void main(String[] args) {
        SpringApplication.run(CubemallSearchApplication.class);
    }
}

RestClientTest.java

package com.xd.cubemall.sdes;


import com.xd.cubemall.search.CubemallSearchApplication;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = CubemallSearchApplication.class)
public class RestClientTest {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @Test
    public void testRestClient() throws IOException {
        //原生
        restHighLevelClient.indices().create(new CreateIndexRequest("test"), RequestOptions.DEFAULT);
    }

}

使用ElasticsearchRestTemplate对象

  1. 创建索引库
    template.indexOps(IndexCoordinates.of(“mytest”)).create();
  2. 设置mapping信息
    需要创建一个实体类,其中配置实体类和文档的映射关系,使用注解配置
    可以从实体类中生成mapping信息

Blog.java

package com.xd.cubemall.search.model;



import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

@Data
@Document(indexName = "blog_1", shards = 5, replicas = 1)
public class Blog {
    @Id
    @Field(type = FieldType.Long, store = true)
    private Long id;
    @Field(type = FieldType.Text, analyzer = "ik_max_word", store = true)
    private String title;
    @Field(type = FieldType.Text, analyzer = "ik_max_word", store = true)
    private String content;
    @Field(type = FieldType.Text, analyzer = "ik_max_word", store = true)
    private String comment;
    @Field(type = FieldType.Keyword, store = true)
    private String mobile;
}

RestTemplateTest.java

package com.xd.cubemall.sdes;


import com.xd.cubemall.search.CubemallSearchApplication;
import com.xd.cubemall.search.model.Blog;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = CubemallSearchApplication.class)
public class RestTemplateTest {
    @Autowired
    private ElasticsearchRestTemplate template;

    @Test
    public void createIndex(){
        template.indexOps(IndexCoordinates.of("blog_1")).create();
    }

    @Test
    public void putMapping() {
        Document mapping = template.indexOps(IndexCoordinates.of("blog_1")).createMapping(Blog.class);
        template.indexOps(IndexCoordinates.of("blog_1")).putMapping(mapping);//id类型不对应

    }


    @Test
    public void createIndexWithMapping() {
        template.indexOps(Blog.class).create();
        Document mapping = template.indexOps(IndexCoordinates.of("blog_1")).createMapping(Blog.class);
        template.indexOps(Blog.class).putMapping(mapping);//id类型不对应
    }


    @Test
    public void deleteIndex() {
        template.indexOps(IndexCoordinates.of("hello1")).delete();
    }
}


http://www.kler.cn/news/358569.html

相关文章:

  • 水下侧扫声呐图像数据集,沉船,1.13G。声纳数据集 水下声纳数据集 水下图像声纳数据集
  • C#基于SkiaSharp实现印章管理(11)
  • 【力扣 | SQL题 | 每日3题】力扣1990, 2020, 2051
  • 基于Python实现“吾爱海洋”论坛自动签到
  • HCIE-Datacom题库_10_网络协议【13道题】
  • 分布式锁实现细节:使用Redisson进行并发控制
  • Java Spring的高级装配
  • [OpenGL]使用OpenGL实现Phong、Blinn-Phong模型
  • 深入探讨C++多线程性能优化
  • Day09-数据库服务备份恢复
  • python实战(一)——iris鸢尾花数据集分类
  • 蜜罐技术的出现究竟影响了什么
  • Android GPU Inspector分析帧数据快速入门
  • zh/FAQ/CentOSStream-CentOS Stream 常见问题
  • 探索 JavaScript 中的 AbortController API:不仅仅是中断 HTTP 请求
  • 多特征变量序列预测(一)——CNN-LSTM风速预测模型
  • 搭建一个vue3+vite框架
  • Redis-2
  • word中高亮标题、正文、表格、图表标题不同颜色用于批量排版
  • Debezium和SeaTunnel实现MySQL到Hadoop的实时数据流和全量同步