登录    关于
马育民老师的博客

马育民的博客

QQ:65242847

springboot 使用 Spring Data Elasticsearch 创建索引、判断索引存在、删除索引

上接:springboot 使用 Spring Data Elasticsearch 操作 Elasticsearch 7.9-创建工程

索引操作

package top.malaoshi;

import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.annotations.FieldType;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.test.context.junit4.SpringRunner;
import top.malaoshi.es.entity.Toutiao;

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestIndex {

    //注入 ElasticsearchRestTemplate
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @org.junit.Test
    public void create(){
        //创建索引
        elasticsearchRestTemplate.indexOps(Toutiao.class);
        System.out.println("创建索引");
    }


    @Test
    public void create2(){
        //查看某索引映射
//        Map map=elasticsearchRestTemplate.indexOps(IndexCoordinates.of("toutiao_v6")).getMapping();
        /*
        通过map构建出映射json结构
        可指定索引名
         */
        Map mapping=new HashMap();

        Map properties=new LinkedHashMap();
        mapping.put("properties",properties);

        Map author=new LinkedHashMap();
        author.put("type","keyword");
        properties.put("author",author);

        Map content=new LinkedHashMap();
        content.put("type","text");
        content.put("analyzer","ik_smart");
        properties.put("content",content);

        Map date=new LinkedHashMap();
        date.put("type","date");
        properties.put("date",date);

        Map like=new LinkedHashMap();
        like.put("type","integer");
        properties.put("like",like);

        Map title=new LinkedHashMap();
        title.put("type","text");
        title.put("analyzer","ik_smart");
        properties.put("title",title);

        Map titleFields=new LinkedHashMap();
        Map titleFieldsKeyword=new LinkedHashMap();
        titleFieldsKeyword.put("type","keyword");
        titleFieldsKeyword.put("ignore_above",256);
        titleFields.put("keyword",titleFieldsKeyword);
        title.put("fields",titleFields);


        Document doc=Document.from(mapping);

        IndexOperations io=elasticsearchRestTemplate.indexOps(IndexCoordinates.of("toutiao_v8"));
        io.create();
        io.putMapping(doc);
        System.out.println("创建索引");
    }


    @org.junit.Test
    public void exists(){
        //不要使用 elasticsearchRestTemplate.indexOps(Toutiao.class),会自动创建
        boolean b=elasticsearchRestTemplate.indexOps(IndexCoordinates.of("toutiao_v4")).exists();
        System.out.println(b);
    }
    @org.junit.Test
    public void delete(){
        //删除索引
        boolean b = elasticsearchRestTemplate.indexOps(IndexCoordinates.of("toutiao_v5")).delete();
        System.out.println("删除索引 = " + b);
    }
}

原文出处:https://malaoshi.top/show_1IX1tORTVEZI.html