JavaAPI操作ES(索引、映射类型、插入Document)

给自己做笔记记录
建一个maven工程
1.导入依赖包

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
 </parent>

    <groupId>com.qf</groupId>
    <artifactId>elasticSearch</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!--全文检索依赖,需要注意,如果springboot工程的依赖在2.3.0以下,需要导入,这里我不需要导入-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        
        <!--springboot2.3.0以下需要导入的包-->
        <!--<dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>6.8.5</version>
        </dependency>-->
        
        <!--web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--实体类工程的全文检索依赖-->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

2.编写启动类

package com.qf.elasticsearch.application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages = "com.qf")
public class ElasticSearchApplication {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(ElasticSearchApplication.class,args);
    }
}

3.编写entity

package com.qf.elasticsearch.entity;

import lombok.Data;
import lombok.experimental.Accessors;
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;

import java.util.Date;

//shards 分片数量    replicas副本数量  indexName索引
@Document(indexName = "myindex2",shards = 1,replicas = 0)
@Data
@Accessors(chain = true) //开启链式编程
public class Student {
    
    
    @Id  //主键
    private Integer id;
    //FieldType.Auto根据字段类型自动注入,例如这里是String
    @Field(type = FieldType.Auto)
    private String name;
    //analyzer分词器 FieldType.Text分词
    @Field(type = FieldType.Text,analyzer = "ik_max_word")
    private String address;
    //FieldType.Keyword 不会进行分词
    @Field(type = FieldType.Integer,index = false)
    private Integer age;
    @Field(type = FieldType.Long)
    //@Field(type = FieldType.Date,pattern = "yyyy:yy:dd HH:mm:ss")时间差8个小时
    private Date createTime;
}

4.编写controller

package com.qf.elasticsearch.controller;

import com.qf.elasticsearch.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
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.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/es")
public class EsController {
    
    
    @Autowired
    private ElasticsearchRestTemplate restTemplate;

    @RequestMapping("/index/create")
    public boolean insertIndex(){
    
    
        IndexOperations operations = restTemplate.indexOps(Student.class);
        if(!operations.exists()){
    
    
            System.out.println("myindex2不存在,则进行创建");
            //创建索引库
            operations.create();
            //创建映射关系
            Document document=operations.createMapping();
            operations.putMapping(document);
            return true;
        }
        return false;
    }

    @RequestMapping("/doc/insert")
    public boolean saveDocument(){
    
    
       Student student = new Student().
       setId(1).setName("小明")
                .setAddress("广东省深圳市瑞祥公寓")
                .setCreateTime(new Date()).setAge(23);
       restTemplate.save(student);
       return true;
    }
}

5.启动ElasticSearchApplication
访问:http://localhost:8080/es/index/create
http://localhost:8080/es//doc/insert
创建索引并且添加了数据
在这里插入图片描述查询一下数据

GET /myindex2/_search

在这里插入图片描述完成.

おすすめ

転載: blog.csdn.net/qq_44982110/article/details/121365197