栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

Spring Data Elasticsearch入门

Spring Data Elasticsearch入门

Spring Data ElasticSearch是Spring对原生JAVA操作Elasticsearch封装之后的产物。它通过对原生API的
封装,使得JAVA程序员可以简单的对Elasticsearch进行操作。

1.搭建项目

创建SpringBoot项目,加入Spring Data Elasticsearch起步依赖:


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

        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
    

写配置文件:

spring:
  elasticsearch:
    rest:
      uris: http://node0:9200

创建实体类:
一个实体类的所有对象都会存入ES的一个索引中,所以我们在创建实体类时关联ES索引。如果ES中没有
该索引则会自动建索引。

package com.neu.springdataes.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
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
@AllArgsConstructor
@NoArgsConstructor
@document(indexName = "product",shards = 1,replicas = 1,createIndex = true)
public class Product {
    @Id
    @Field(type = FieldType.Integer,store = true,index = true)
    private Integer id;
    @Field(type = FieldType.Text,store = true,index = true,analyzer = "ik_smart",searchAnalyzer = "ik_smart")
    private String name;
    @Field(type = FieldType.Text,store = true,index = true,analyzer = "ik_smart",searchAnalyzer = "ik_smart")
    private String desc;
}

@document:标记在类上,标记实体类为文档对象,一般有如下属性:
indexName:对应索引的名称
shards:分片数量
replicas:副本数量
createIndex:是否自动创建索引
@Id:标记在成员变量上,标记一个字段为主键,该字段的值会同步到ES该文档的id值。
@Field:标记在成员变量上,标记为文档中的域,一般有如下属性:
type:域的类型
index:是否索引,默认是 true
store:是否单独存储,默认是 false
analyzer:分词器
searchAnalyzer:搜索时的分词器

创建Repository接口
创建Repository接口继承ElasticsearchRepository,该接口提供了文档的增删改查方法

package com.neu.springdataes.model;

import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;


public interface ProductRepository extends ElasticsearchRepository {
}

测试方法
编写测试类,注入Repository接口并测试Repository接口的增删改查方法

package com.neu.springdataes;

import com.neu.springdataes.model.Product;
import com.neu.springdataes.model.ProductRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Optional;

@SpringBootTest
class SpringdataesApplicationTests {
    @Autowired
    ProductRepository repository;
    @Test
    void adddocument() {
        Product product = new Product(1, "iPhone 12", "iPhone 12是本年度销售最佳手机");
        repository.save(product);
    }
    @Test
    void findAlldocument() {
        Iterable products = repository.findAll();
        for (Product product:products)
            System.out.println(product);
    }
    @Test
    void updatedocument() {
        Product product = new Product(1, "iPhone 13", "iPhone 13是本年度销售最佳手机");
        repository.save(product);
    }
    @Test
    void finddocumentById() {
        Optional product = repository.findById(1);
        System.out.println(product.get());
    }
    @Test
    void deletedocumentById(){
        repository.deleteById(1);
    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/629159.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号