springboot 整合elasticsearch

本文介绍如何在Spring Boot项目中集成Elasticsearch,包括pom文件配置、实体类定义、索引操作及文档增删改查等功能实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.pom文件


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

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

	</dependencies>

2.配置

spring.data.elasticsearch.cluster-nodes = 127.0.0.1:9300

3.model

package com.knife.springbootes2;

import java.io.Serializable;

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.FieldIndex;
import org.springframework.data.elasticsearch.annotations.FieldType;

@Document(indexName = "test", type = "article")
public class Article implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Id
	@Field(index = FieldIndex.not_analyzed, store = true)
	private String id;

	@Field(type = FieldType.String, store = true)
	private String title;

	@Field(type = FieldType.String, store = true)
	private String content;

	@Field(type = FieldType.String, store = true)
	private String author;
	

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String toString() {
		return "{id:" + id + ",title:\"" + title + "\",content:\"" + content + "\"}";
	}
}

4.注入ElasticsearchTemplate

@Autowired
private ElasticsearchTemplate elasticsearchTemplate;

5.增加

/**
	 * 插入或等新,需要有id,id需要自己生成
	 * 
	 * @param tList
	 * @return
	 */
	@RequestMapping("/add")
	@ResponseBody
	public boolean add() {
		String id = UUID.randomUUID().toString();
		Article article = new Article();
		article.setId(id);
		article.setContent("罗浮山下梅花村,玉雪为骨冰为魂。");
		article.setTitle("再用前韵");
		article.setAuthor("苏轼");
		try {
			IndexQuery indexQuery = new IndexQueryBuilder().withId(id).withObject(article).build();
			elasticsearchTemplate.index(indexQuery);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

6.修改

@RequestMapping("/update")
	@ResponseBody
	public boolean update(String id, String content) {
		Article article = new Article();
		article.setId(id);
		article.setContent(content);
		try {
			IndexQuery indexQuery = new IndexQueryBuilder().withId(id).withObject(article).build();
			elasticsearchTemplate.index(indexQuery);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

7.删除

@RequestMapping("delete")
@ResponseBody
public String delete(String id) {
	return elasticsearchTemplate.delete(Article.class, id);
}

8.查找

@RequestMapping("query")
@ResponseBody
public List<Article> query(int page, String title) {
		NativeSearchQueryBuilder searchQuery = new NativeSearchQueryBuilder();
		BoolQueryBuilder bqb = QueryBuilders.boolQuery();
		searchQuery.withIndices("test");
		searchQuery.withPageable(new PageRequest(page, 5));
		bqb.mustNot(QueryBuilders.termQuery("title", title));
		List<Article> list = elasticsearchTemplate.queryForList(searchQuery.build(), Article.class);
		return list;
}

9.创建索引

@RequestMapping("/createIndex")
@ResponseBody
public Object createIndex() {
	return elasticsearchTemplate.createIndex("test");
}

10.创建map

@RequestMapping("/createMap")
@ResponseBody
public Object create() {
	return elasticsearchTemplate.putMapping(Article.class);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值