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

JavaAPI *** 作ES(索引、映射类型、插入Document),第1张

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

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


        org.springframework.boot
        spring-boot-starter-parent
        2.3.0.RELEASE
    

    com.qf
    elasticSearch
    1.0-SNAPSHOT

    
        
        
            org.springframework.boot
            spring-boot-starter-data-elasticsearch
        
        
        
        
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.data
            spring-data-elasticsearch
        
        
            org.projectlombok
            lombok
        
    

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

完成.

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5523007.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-13
下一篇 2022-12-13

发表评论

登录后才能评论

评论列表(0条)

保存