Elasticsearch:使用spring-data-elasticsearch操作Elasticsearch

本文档详细介绍了如何使用Spring Data Elasticsearch进行Elasticsearch的索引管理和文档操作,包括添加pom依赖,配置yml,创建po映射,以及在service层处理索引的新增与删除,文档的新增、删除、查询与修改等操作。
摘要由CSDN通过智能技术生成

pom依赖

<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot<groupId>
			<artifactId>spring-boot-dependencies</artifactId>
			<version>2.1.5.RELEASE</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	<dependencies>
</dependencyManagement>

<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>

	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
	</dependency>

	<dependency>
		<groupId>org.projectlombook</groupId>
		<artifactId>lombook<artifactId>
	</dependency>
</dependencies>

yml配置

spring:
	data:
		elasticsearch:
			cluster-name: elasticsearch
			cluster-nodes: 127.0.0.1:9300

po映射

import lombok.Data;
import java.io.Serializable;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.FieldType;
import org.springframework.data.elasticsearch.annotations.DateFormat;

@Data
@Document(indexName="user_index", type="user_type", shards=1, replicas=0)
public class User implements Serializable{
   
	private static final long serialVersionUID = 627994168197554442L;
	
	@Id
	private long id;
	
	@Field(type=FieldType.Keyword)
	private String name;
	
	@Field(type=FieldType.Integer)
	private int age;
	
	@Field(type=FieldType.Text,analyzer="ik_max_word")
	private String desc;
	
	@Field(type=FieldType.Date, format=DateFormat.custom, pattern="yyyy-MM-dd HH:mm:ss SSS")
	private String createTime;
	
	@Field(type=FieldType.Date, format=DateFormat.custom, pattern="yyyy-MM-dd HH:mm:ss SSS")
	private String updateTime;
}

注意:

  1. 使用@Id表示该字段同时会被映射到Elasticsearch中document的_id字段(也就是说在Elasticearch中_id的值与id值一样);
  2. 如果document字段与实体类字段名称不统一,在Spring Data Elasticsearch 3.2版本及其之后直接使用@Field的name属性指定,3.2版本之前则需要使用@JsonProperty指定
    import org.springframework.data.elasticsearch.annotation.*;
    import java.io.Serializable;
    import com.fasterxml.jackson.annotation.JsonProperty;
    
    @Date
    @Document(indexName="user_index",type="user_type")
    public class User implements Serializable{
         
    	private static final long serialVersionUID = 627994168197554442L;
    	
    	//3.2版本及之后
    	@Field(name="address_city", type=FieldType.Keyword)
    	private String addressCity;
    	
    	//3.2版本之前
    	@Field(type=FieldType.Keyword)
    	@JsonProperty("address_city")
    	private String addressCity;
    }
    

service处理

索引相关

1.新增索引

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值