028 elasticsearch索引管理-ElasticsearchRestTemplate

server/2024/10/21 4:58:09/

文章目录

    • 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.ppmy.cn/server/133534.html

相关文章

OpenCV高级图形用户界面(14)交互式地选择一个或多个感兴趣区域函数selectROIs()的使用

操作系统&#xff1a;ubuntu22.04 OpenCV版本&#xff1a;OpenCV4.9 IDE:Visual Studio Code 编程语言&#xff1a;C11 算法描述 允许用户在给定的图像上选择多个 ROI。 该函数创建一个窗口&#xff0c;并允许用户使用鼠标来选择多个 ROI。控制方式&#xff1a;使用空格键或…

DFS算法经典题目: Leetcode 51.N皇后

DFS算法经典题目&#xff1a; Leetcode 51.N皇后 题目详情如下 这道题如果使用暴力解法的话&#xff0c;需要对N个皇后放在每个地方都进行枚举并判断是否可行&#xff0c;时间复杂度非常之高&#xff0c;肯定是过不了的&#xff0c;所以需要使用其他解法。 根据题目可以知道每…

text2sql: multi-agent实现思路MAC-SQL

MAC-SQL出自2023年12月的论文《MAC-SQL: A Multi-Agent Collaborative Framework for Text-to-SQL》(github)&#xff0c;它是用基于LLM的multi-agent来实现text2sql。 MAC-SQL的整体思路如论文图2所示&#xff0c;由Decomposer、Selector、Refiner三个agent组成&#xff0c;个…

目标检测系统中需要【重新训练模型】说明

上百种【基于YOLOv8/v10/v11的目标检测系统】目录&#xff08;pythonpyside6界面系统源码可训练的数据集也完成的训练模型&#xff09;-CSDN博客 目标检测系统操作说明【用户使用指南】&#xff08;pythonpyside6界面系统源码可训练的数据集也完成的训练模型&#xff09;-CSDN…

算法专题七: 分治归并

目录 1. 排序数组2. 交易逆序对的总数3. 计算右侧小于当前元素的个数4. 翻转对 1. 排序数组 算法思路: 本道题使用归并的思路进行排序, 先讲数组分为左右两个区间, 然后合并两个有序数组. class Solution {vector<int> tmp; public:vector<int> sortArray(vector&…

wifi、热点密码破解 - python

乐子脚本&#xff0c;有点小慢&#xff0c;试过多线程&#xff0c;系统 wifi 连接太慢了&#xff0c;需要时间确认&#xff0c;多线程的话系统根本反应不过来。 也就可以试试破解别人的热点&#xff0c;一般都是 123456 这样的傻鸟口令 # coding:utf-8 import pywifi from pyw…

python 打包为动态链接库(.so文件)

参考资料&#xff1a;https://medium.com/yeap0022/linux-compress-python-packages-into-shared-library-so-8342bffab001 注意事项&#xff1a;.py, .c后缀文件都可以删掉&#xff0c;cache也可以删掉&#xff0c;但是__init__.py文件不能删&#xff0c;建立的子文件夹也不能…

JavaWeb环境下Spring Boot在线考试系统的优化策略

摘要 随着信息技术在管理上越来越深入而广泛的应用&#xff0c;管理信息系统的实施在技术上已逐步成熟。本文介绍了基于JavaWeb技术的在线考试系统设计与实现的开发全过程。通过分析基于Java Web技术的在线考试系统设计与实现管理的不足&#xff0c;创建了一个计算机管理基于Ja…