028 elasticsearch索引管理-ElasticsearchRestTemplate

时间:2024-10-21 21:55:01

文章目录

    • 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();
    }
}