一、使用前设置

1、elasticsearch开启

cmd下,进入安装目录

cd D:\developToool\elasticsearch-5.4.3
elasticsearch
# 或者后台运行
elasticsearch -d

校验安装成功:http://127.0.0.1:9200/

查看集群健康:http://127.0.0.1:9200/_cat/health?v

查看集群节点:http://127.0.0.1:9200/_cat/nodes?v

查看索引信息:http://127.0.0.1:9200/_cat/indices?v

2、elasticsearch-header

cd D:\developToool\elasticsearch-head
npm run start

校验:http://127.0.0.1:9100/

二、spring对接

由于服务器端版本使用5.4.3。故客户端使用此版本

环境配置
  ES版本:5.4.3
  spring-data-elasticsearch:3.0.0.RELEASE
  spring:5.0.1.RELEASE

2.1、maven配置

            <dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>${es.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>${spring.es.version}</version>
<exclusions>
<exclusion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</exclusion>
<exclusion>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
</exclusions>
</dependency>

注意:spring自行引入5.0.1.RELEASE,jackson需要2.9.*以上版本

2.2、spring-es.xml配置

<?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:util="http://www.springframework.org/schema/util"
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/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/data/elasticsearch
http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd "> <!-- 扫描DAO包 自动创建实现 -->
<elasticsearch:repositories base-package="com.trace.bi.es.repository" />
<!-- 扫描Service包 -->
<context:component-scan base-package="com.trace.bi.es.service" />
<!-- 配置elasticsearch 连接 -->
<!--<elasticsearch:transport-client cluster-name="${es.clusterName}" id="client" cluster-nodes="${es.transportAddresses}" />-->
<elasticsearch:transport-client cluster-name="elasticsearch" id="client" cluster-nodes="127.0.0.1:9300" />
<!-- spring data elasticsearch DAO 必须依赖 elasticsearchTemplate -->
<bean id="elasticsearchTemplate"
class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
<constructor-arg name="client" ref="client" />
</bean>
</beans>

其他spring配置按照正常配置即可

2.3、代码编写

1>在domain中编写测试用的实体类

索引和映射如何创建 --- 基于 spring data elasticsearch 注解
在使用 spring data elasticsearch 开发, 需要将索引和映射信息 配置实体类上面
@Document 文档对象 (索引信息、文档类型 )
@Id 文档主键 唯一标识
@Field 每个文档的字段配置(类型、是否分词、是否存储、分词器 )

package com.trace.domain;

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 = "blog3", type = "article")
public class Article { /** ID */
@Id
@Field(index = FieldIndex.not_analyzed, store = true, type = FieldType.Integer)
private Integer id; /** 文档标题 */
@Field(index = FieldIndex.analyzed, analyzer = "ik", store = true, searchAnalyzer = "ik", type = FieldType.String)
private String title; /** 文档内容 */
@Field(index = FieldIndex.analyzed, analyzer = "ik", store = true, searchAnalyzer = "ik", type = FieldType.String)
private String content; public Integer getId() {
return id;
} public void setId(Integer 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;
} @Override
public String toString() {
return "Article{" + "id=" + id + ", title='" + title + '\'' + ", content='" + content + '\'' + '}';
} }

2>编写 DAO接口

第二个方法里,
分页条件查询,只需要在查询方法中,添加 Pageable 对象
排序条件查询,只需要在查询方法中,添加 Sort 对象

package com.trace.dao;

import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import com.trace.domain.Article; /**
* 文档的DAO层接口
*/
public interface ArticleRepository extends ElasticsearchRepository<Article, Integer> { /**
* 根据标题查找文档
*
* @param title 文档标题
* @return 文档集合
*/
List<Article> findByTitle(String title); /**
* 根据标题查找文档分页数据
*
* @param title 文档标题
* @param pageable 分页对象
* @return 文档的分页数据
*/
Page<Article> findByTitle(String title, Pageable pageable); }

3>编写 Service接口和实现类

Spring data Search CRUD 操作
CurdRepository 提供增删改查 save、delete、findAll 、findOne
PagingAndSortingRepository 提供分页和排序

package com.trace.service;

import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.trace.domain.Article; /**
* 文档的Service层接口
*/
public interface ArticleService { /**
* 文档保存的方法
*
* @param article 文档对象
*/
void save(Article article); /**
* 文档删除的方法
*
* @param article 文档对象
*/
void delete(Article article); /**
* 根据id查找文档的方法
*
* @param id 文档id
* @return 查找到的文档对象
*/
Article findOne(Integer id); /**
* 查找所有的文档
*
* @return 文档的迭代器
*/
Iterable<Article> findAll(); /**
* 分页显示所有文档
*
* @param pageable 分页对象
* @return 文档的分页数据
*/
Page<Article> findAll(Pageable pageable); /**
* 根据标题查找文档
*
* @param title 文档标题
* @return 文档集合
*/
List<Article> findByTitle(String title); /**
* 根据标题查找文档分页数据
*
* @param title 文档标题
* @param pageable 分页对象
* @return 文档的分页数据
*/
Page<Article> findByTitle(String title, Pageable pageable); }

实现类

package com.trace.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import com.trace.dao.ArticleRepository;
import com.trace.domain.Article;
import com.trace.service.CustomerService; /**
* 文档的Service层实现类
*/
@Service
public class ArticleServiceImpl implements ArticleService { @Autowired
private ArticleRepository articleRepository; @Override
void save(Article article) {
articleRepository.save(article);
} @Override
void delete(Article article) {
articleRepository.delete(article);
} @Override
Article findOne(Integer id) {
return articleRepository.findOne(id);
} @Override
Iterable<Article> findAll() {
return articleRepository.findAll(new Sort(new Sort.Order(Sort.Direction.ASC, "id")));
} @Override
Page<Article> findAll(Pageable pageable) {
return articleRepository.findAll(pageable);
} @Override
List<Article> findByTitle(String title) {
return articleRepository.findByTitle(title);
} @Override
Page<Article> findByTitle(String title, Pageable pageable) {
return articleRepository.findByTitle(title, pageable);
} }

4>测试代码

package com.trace.service.impl;

import org.elasticsearch.client.Client;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.trace.domain.Article;
import com.trace.service.ArticleService; /**
* 文档的service测试类
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class ArticleServiceTest { /** 注入service */
@Autowired
private ArticleService articleService; /** 注入客户端对象 基于原生API */
@Autowired
private Client client; /** 注入es服务器模板 */
@Autowired
private ElasticsearchTemplate elasticsearchTemplate; /**
* 通过 ElasticsearchTemplate 创建索引和添加映射
*/
@Test
public void createIndex() {
elasticsearchTemplate.createIndex(Article.class);
elasticsearchTemplate.putMapping(Article.class);
} /**
* 添加方法测试
*/
@Test
public void testSave() {
Article article = new Article();
article.setId(1001);
article.setTitle("Spring Data Elasticsearch 1.3.1 昨天发布");
article.setContent(
"DATAES-171 - 添加失效查询关键字支持 DATAES-194 - 测试可以清理 data 目录 DATAES-179 - 支持 Attachment 字段类型 DATAES-94 - "
+ "更新到最新版本的 elasticsearch 1.7.3 驱动器"); articleService.save(article);
} /**
* 根据索引删除的方法测试
*/
@Test
public void testDelete() {
Article article = new Article();
article.setId(1001); articleService.delete(article);
} /**
* 根据索引查询的方法测试
*/
@Test
public void testFindOne() {
System.out.println(articleService.findOne(1001));
} /**
* 添加100条测试数据的方法
*/
@Test
public void testSaveBatch() {
for (int i = 1; i <= 100; i++) {
Article article = new Article();
article.setId(i);
article.setTitle(i + "Spring Data Elasticsearch 1.3.1 昨天发布");
article.setContent(
i + "DATAES-171 - 添加失效查询关键字支持 DATAES-194 - 测试可以清理 data 目录 DATAES-179 - 支持 Attachment 字段类型 DATAES-94 -"
+ " 更新到最新版本的 elasticsearch 1.7.3 驱动器"); articleService.save(article);
}
} /**
* 排序分页查询的方法测试
*/
@Test
public void testSortAndPaging() {
Iterable<Article> articles = articleService.findAll();
for (Article article : articles) {
System.out.println(article);
} Pageable pageable = new PageRequest(0, 10);
Page<Article> pageData = articleService.findAll(pageable);
for (Article article : pageData.getContent()) {
System.out.println(article);
}
} /**
* 条件查询的方法测试
*/
@Test
public void testConditionQuery() {
// 查询 标题中含有 “昨天”
// List<Article> articles = articleService.findByTitle("昨天");
// for (Article article : articles) {
// System.out.println(article);
// } // 查询 标题中含有 “昨天” 1-10条
Pageable pageable = new PageRequest(0, 10);
Page<Article> pageData = articleService.findByTitle("昨天", pageable);
System.out.println("总记录数:" + pageData.getTotalElements());
for (Article article : pageData.getContent()) {
System.out.println(article);
}
} }

参考地址:https://www.cnblogs.com/dijia478/p/7839110.html

002-es5.4.3结合spring-data-elasticsearch3.0.0.0使用的更多相关文章

  1. 使用 Spring Data 进行 MongoDB 4.0 事务处理

    使用 Spring Data 进行 MongoDB 4.0 事务处理 原文链接:http://spring.io/blog/2018/06/28/hands-on-mongodb-4-0-transa ...

  2. 深入浅出学Spring Data JPA

    第一章:Spring Data JPA入门 Spring Data是什么 Spring Data是一个用于简化数据库访问,并支持云服务的开源框架.其主要目标是使得对数据的访问变得方便快捷,并支持map ...

  3. Spring Data JPA教程, 第八部分:Adding Functionality to a Repository (未翻译)

    The previous part of my tutorial described how you can paginate query results with Spring Data JPA. ...

  4. Spring Data JPA 教程(翻译)

    写那些数据挖掘之类的博文 写的比较累了,现在翻译一下关于spring data jpa的文章,觉得轻松多了. 翻译正文: 你有木有注意到,使用Java持久化的API的数据访问代码包含了很多不必要的模式 ...

  5. 【Spring Data JPA篇】项目环境搭建(一)

    项目环境: spring4.1.6 hibernate4.3.11 spring-data-jpa1.9.0 1. 创建一个Java Project,将jar导入到lib目录下 #spring spr ...

  6. SpringBoot整合Spring Data Elasticsearch

    Spring Data Elasticsearch提供了ElasticsearchTemplate工具类,实现了POJO与elasticsearch文档之间的映射 elasticsearch本质也是存 ...

  7. CVE-2018-1273 Spring Data Commons 远程命令执行漏洞复现

    一.漏洞描述 Spring Data是一个用于简化数据库访问,并支持云服务的开源框架,Spring Data Commons是Spring Data下所有子项目共享的基础框架.Spring Data ...

  8. Spring Data MongoDB 三:基本文档查询(Query、BasicQuery)(一)

    一.简单介绍 Spring Data  MongoDB提供了org.springframework.data.mongodb.core.MongoTemplate对MongoDB的CRUD的操作,上一 ...

  9. Spring Data MongoDB 三:基本文档查询(Query、BasicQuery

    一.简介 spring Data  MongoDB提供了org.springframework.data.mongodb.core.MongoTemplate对MongoDB的CRUD的操作,上一篇我 ...

  10. Spring Boot + Spring Data + Elasticsearch实例

    Spring Boot + Spring Data + Elasticsearch实例 学习了:https://blog.csdn.net/huangshulang1234/article/detai ...

随机推荐

  1. [LOJ 6253] Yazid 的新生舞会

    link $solution:$ 不知道为什么别人的代码能写的非常短,难道就是写差分的好处? 这种题肯定是算每个众数的贡献,考虑通过暴力众数求出个数. 现在考虑众数 $x$ ,则在序列 $a$ 中将等 ...

  2. hdu1263 简单模拟

    题意:依据水果销量表.依照特定格式输出 格式:首先按产地排序,然后同一产地按水果名排序 注意:第一,设计多级排序           第二.同一产地同一水果可能多次出现,所以须要在前面已经输入的水果里 ...

  3. 剑指offer-删除链表中重复的结点-链表-python ***

    题目描述 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针. 例如,链表1->2->3->3->4->4->5 处理后 ...

  4. vscode 热部署 spring-mvc

    1.添加maven插件 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId> ...

  5. 理解 OutOfMemoryError 异常

    OutOfMemoryError 异常应该可以算得上是一个非常棘手的问题.JAVA 的程序员不用像苦逼的 C 语言程序员手动地管理内存,JVM 帮助他们分配内存,释放内存.但是当遇到内存相关的问题,就 ...

  6. Android数据库使用指南(下)

    前言 上面已经说了,对表进行修改,其实就是对数据库进行升级,删除表也算升级啊,反正就是发生变化,数据库就需要升级. 所以老实说其实有个地方决定了数据库的版本 public class DBHelper ...

  7. IDEA创建maven的web项目时,main文件夹下没有java,resources目录等源文件夹

    https://blog.csdn.net/qq_34377273/article/details/83183307

  8. Cookie、Session和Django分页

    cookie Cookie的由来 大家都知道HTTP协议是无状态的. 无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会受前面的请求响应情况直接影响,也不 ...

  9. Schedule HDU - 6180 (multiset , 贪心)

    There are N schedules, the i-th schedule has start time si and end time ei (1 <= i <= N). Ther ...

  10. Codeforces 959 树构造 暴力求最小字典序互质序列

    A B C 题目给你一个结论 最少需要min((odd,even)个结点可以把一棵树的全部边连起来 要求你输出两颗树 一棵树结论是正确的 另外一棵结论是正确的 正确结论的树很好造 主要是错误的树 题目 ...