欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 幼教 > 028 elasticsearch索引管理-ElasticsearchRestTemplate

028 elasticsearch索引管理-ElasticsearchRestTemplate

2024/10/25 15:32:30 来源:https://blog.csdn.net/m0_46695127/article/details/143026276  浏览:    关键词: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();}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com