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

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 {@Autowiredprivate RestHighLevelClient restHighLevelClient;@Testpublic 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 {@Autowiredprivate ElasticsearchRestTemplate template;@Testpublic void createIndex(){template.indexOps(IndexCoordinates.of("blog_1")).create();}@Testpublic void putMapping() {Document mapping = template.indexOps(IndexCoordinates.of("blog_1")).createMapping(Blog.class);template.indexOps(IndexCoordinates.of("blog_1")).putMapping(mapping);//id类型不对应}@Testpublic 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类型不对应}@Testpublic void deleteIndex() {template.indexOps(IndexCoordinates.of("hello1")).delete();}
}

http://www.mrgr.cn/news/52414.html

相关文章:

  • 七、【智能体】扣子人设:智能体背后的关键设计,你真的了解吗?
  • 纯css实现瀑布流! 附源码!!!
  • JavaSE——方法引用
  • packaged_task 异步调用函数打包
  • AI驱动的零售未来:打造无缝、智能、个性化的购物新世界
  • Gitea迁移外部代码仓库
  • 嵌入式C语言面试相关知识——常见的四种通信协议:I²C、SPI、USART、CAN;一种数据通信机制:DMA
  • Mamba学习笔记(2)—序列数据处理基础
  • 建筑工程管理软件推荐,2024年最佳选择
  • Linux网络命令:轻量级的、用户友好的、监视每个进程或应用程序网络带宽使用的工具nethogs详解
  • C++【内存管理】(超详细讲解C++内存管理以及new与delete的使用和原理)
  • 【DDPG】DDPG的离散实现(含代码)
  • Android Framework AMS(04)startActivity分析-1(am启动到ActivityThread启动)
  • 点评项目-9-秒杀业务(加锁)、redis分布式锁
  • 联众优车积极应对二手车市场挑战,在变化的市场抓住发展机遇
  • 从零开始的LeetCode刷题日记:515.在每个树行中找最大值
  • mac 录屏(视频+内外部声音)
  • 【建议收藏】两万字总结Git的60个常用操作
  • PCL 点云配准 KD-ICP算法(精配准)
  • LVGL代码移植(裸机+FreeRTOS操作系统+内部SRAM+外部SRAM+内存管理算法+编译错误以及现象显示不正常)