ElasticSearch系列:基本操作(SpringDataElasticSearch)
一.创建工程、导入坐标
1.选择Next

2.填写名称、选择位置、填写公司或组织、选择Finish

3.导入坐标
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>cn.lb</groupId>
<artifactId>elasticsearch</artifactId>
<version>1.0.0.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.3.1</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>6.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.24</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>3.0.5.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.elasticsearch.plugin</groupId>
<artifactId>transport-netty4-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.4.RELEASE</version>
</dependency>
</dependencies>
</project>
4.创建实体类:Article
package cn.lb.es.entity; 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; @Document(indexName = "index_blog",type="article")
public class Article {
@Id
@Field(type= FieldType.Long,store = true)
private long id;
@Field(type =FieldType.text,store = true,analyzer = "ik_smart")
private String title;
@Field(type = FieldType.text,store = true,analyzer = "ik_smart")
private String content; @Override
public String toString() {
return "Article{" +
"id=" + id +
", title='" + title + '\'' +
", content='" + content + '\'' +
'}';
} public long getId() {
return id;
} public void setId(long 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;
}
}
5.创建接口
package cn.lb.es.repositories; import cn.lb.es.entity.Article;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.data.repository.NoRepositoryBean; import java.util.List; public interface ArticleRepository extends ElasticsearchRepository<Article, Long> {
List<Article> findByTitle(String title);
List<Article> findByTitleOrContent(String title, String content);
List<Article> findByTitleOrContent(String title, String content, Pageable pageable);
}
6.添加配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/data/elasticsearch
http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd
">
<!--elastic客户对象的配置-->
<elasticsearch:transport-client id="esClient" cluster-name="my-elasticsearch"
cluster-nodes="127.0.0.1:9300,127.0.0.1:9301,127.0.0.1:9302"/>
<!--配置包扫描器,扫描dao的接口-->
<elasticsearch:repositories base-package="cn.lb.es.repositories"/>
<!---->
<bean id="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
<constructor-arg name="client" ref="esClient"/>
</bean> </beans>
7.测试
import cn.lb.es.entity.Article;
import cn.lb.es.repositories.ArticleRepository;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.web.PageableDefault;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.List; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringDataElasticSearchTest { @Autowired
private ArticleRepository articleRepositrory; @Autowired
private ElasticsearchTemplate template; @Test
public void createIndex() throws Exception{
template.createIndex(Article.class);
} @Test
public void addDocument() throws Exception
{ Article article =new Article();
article.setId(i);
article.setTitle("IntelliJ IDEA 编译Java程序出现 'Error:java: 无效的源发行版: 9' 的解决方案");
article.setContent("最新安装的IntelliJ IDEA 2018.1编译器,创建Java Project,并选择之前安装好的Eclipse配置的JDK,如图所示:");
articleRepositrory.save(article); } @Test
public void deleteDocumentId() throws Exception
{
articleRepositrory.deleteById(2l); //articleRepositrory.deleteAll();
} @Test
public void findByTitle() throws Exception
{
List<Article> list=articleRepositrory.findByTitle("9");
list.stream().forEach(a-> System.out.println(a));
} @Test
public void findByTitleOrContent() throws Exception
{
Pageable pageable=PageRequest.of(0,15);
List<Article> list = articleRepositrory.findByTitleOrContent("9", "n", pageable);
list.forEach(a-> System.out.println(a));
} @Test
public void nativeSearchQuery() throws Exception
{
NativeSearchQuery query=new NativeSearchQueryBuilder()
.withQuery(QueryBuilders.queryStringQuery("Java是最好的语言").defaultField(
"title"
)).withPageable(PageRequest.of(0,15)).build(); List<Article> list = template.queryForList(query, Article.class);
list.forEach(a-> System.out.println(a));
}
}
ElasticSearch系列:基本操作(SpringDataElasticSearch)的更多相关文章
- Elasticsearch CRUD基本操作
前言 本次我们聊一聊Elasticsearch的基本操作CRUD,他跟我们常用的关系型数据库的操作又有什么不一样的地方呢?今天我们就来好好讲解一番. 说明 本次演示用的版本是7.11. 工具可以使用K ...
- Elasticsearch rest-high-level-client 基本操作
Elasticsearch rest-high-level-client 基本操作 本篇主要讲解一下 rest-high-level-client 去操作 Elasticsearch , 虽然这个客户 ...
- 5.ElasticSearch系列之文档的基本操作
1. 文档写入 # create document. 自动生成 _id POST users/_doc { "user" : "shenjian", " ...
- elasticsearch系列七:ES Java客户端-Elasticsearch Java client(ES Client 简介、Java REST Client、Java Client、Spring Data Elasticsearch)
一.ES Client 简介 1. ES是一个服务,采用C/S结构 2. 回顾 ES的架构 3. ES支持的客户端连接方式 3.1 REST API ,端口 9200 这种连接方式对应于架构图中的RE ...
- Elasticsearch 系列文章汇总(持续更新...)
系列文章列表 Query DSL Query DSL 概要,MatchAllQuery,全文查询简述 Match Query Match Phrase Query 和 Match Phrase Pre ...
- Elasticsearch系列(五)----JAVA客户端之TransportClient操作详解
Elasticsearch JAVA操作有三种客户端: 1.TransportClient 2.JestClient 3.RestClient 还有种是2.3中有的NodeClient,在5.5.1中 ...
- SpringBoot整合ElasticSearch:基于SpringDataElasticSearch
0.注意事项 SpringDataElasticSearch可能和远程的ElasticSearch版本不匹配,会宝座 版本适配说明:https://github.com/spring-projects ...
- python对接elasticsearch的基本操作
基本操作 #!/usr/bin/env python # -*- coding: utf-8 -*- # author tom from elasticsearch import Elasticsea ...
- Elasticsearch系列---实现分布式锁
概要 Elasticsearch在文档更新时默认使用的是乐观锁方案,而Elasticsearch利用文档的一些create限制条件,也能达到悲观锁的效果,我们一起来看一看. 乐观锁与悲观锁 乐观锁 E ...
- Elasticsearch系列---生产集群的索引管理
概要 索引是我们使用Elasticsearch里最频繁的部分日常的操作都与索引有关,本篇从运维人员的视角,来玩一玩Elasticsearch的索引操作. 基本操作 在运维童鞋的视角里,索引的日常操作除 ...
随机推荐
- tiptop查询通配符
*:表示任何符合的字符,例:A*,表示要找出全部为 A 开头的资料. ?:表示任一符合的字符,例:A?,表示要找出第一码为 A,第二码为任何 字符,但总共只有二码之数据. 注:以上二功能仅可在文字字段 ...
- 0 基础晋级 Serverless 高手课 — 初识 Serverless(上)
应用 - 无服务器 2017- 2006 函数即服务 类似 云计算 (Serverless) faas 函数服务 + 后端数据库 账号服务 弹性,按量 服务器,客户端的终结 - 弹性 - 按量 优点 ...
- 用go封装和实现扫码登录
用go封装和实现扫码登录 本篇为用go设计开发一个自己的轻量级登录库/框架吧 - 秋玻 - 博客园 (cnblogs.com)的扫码登录业务篇,会讲讲扫码登录的实现,给库/框架增加新的功能,最后说明使 ...
- GameFramework摘录 - 1. ReferencePool
GameFramework是一个结构很优秀的Unity游戏框架,但意图似乎在构建可跨引擎的框架?对要求不高的小型个人(不专业)开发来说有些设计过度了,但其中的设计精华很值得学习. 首先来说一下其中的R ...
- tomcat nio2源码分析
一. 前言 最近在看tomcat connector组件的相关源码,对Nio2的异步回调过程颇有兴趣,平时读源码不读,自己读的时候很多流程都没搞明白,去查网上相关解析讲的给我感觉也不是特别清晰,于 ...
- 一个Docker仓库问题的思考
近期项目有云上部署要求,产品要打包成docker镜像.之前产品已经发布过docker版本本次只需要需要更新下,于是交代组内另外一个同学更新下镜像,想着应该很简单: 中间经过熟悉docker知识点搭建环 ...
- 企业微信获取code
String url="https://open.weixin.qq.com/connect/oauth2/authorize?appid="+AuthUtil.APPID + ...
- Ubuntu 20.04 查看内存信息
输入命令 dmidecode -t memory 输出如下: # dmidecode 3.2 Getting SMBIOS data from sysfs. SMBIOS 2.8 present. H ...
- MySQL-utf8 和 utf8mb4 区别?
版权声明:原创作品,谢绝转载!否则将追究法律责任. ----- 作者:kirin 1.首先说明一下,版本问题.MySQL8.0之后默认:utf8mb4,而8.0之前默认:latin 2.utf8 和 ...
- 通过Span实现高性能数组,实例解析
Span<T> 是 C# 7.2 引入的一个强大的数据结构,用于表示内存中的一块连续数据.它可以用于实现高性能的数组操作,而无需额外的内存分配.在本文中,我将详细介绍如何使用 Span&l ...