一、使用前设置

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. 源码看React 事件机制

    对React熟悉的同学都知道,React中的事件机制并不是原生的那一套,事件没有绑定在原生DOM上,发出的事件也是对原生事件的包装.那么这一切是怎么实现的呢? 事件注册 首先还是看我们熟悉的代码 &l ...

  2. window-tree命令

    tree 以图形方式显示在驱动器中的目录结构或磁盘的路径. 有时候需要整理文档目录时,而文件太多,一个个去写相应的文件目录结构也不现实,就用到了window下的tree命令 语法 tree [< ...

  3. 剑指offer-两个链表的第一个公共结点-链表-python

    题目描述 输入两个链表,找出它们的第一个公共结点.   class Solution: def FindFirstCommonNode(self, pHead1, pHead2): # write c ...

  4. 【问题解决方案】Xshell连接服务器并实现上传和下载文件

    参考链接: Xshell连接服务器并实现上传和下载文件 第一步:xshell登录完成 略 第二步: 在服务器安装lrzsz 如果服务器的操作系统是 CentOS,则输入命令[yum install l ...

  5. Redis数据结构&命令手册

    Redis数据结构&命令手册 Redis数据结构 Redis可以存储键与5种不同数据结构之间的映射,这五种数据结构类型分别为STRING(字符串).LIST(列表).SET(集合).HASH( ...

  6. MFC学习笔记2---简单计算器

    前言 学习了鸡啄米网页的前三部分后,我们就可以做一个小软件出来了,我选择先做一个计算器. 这是Win7系统自带的计算器: 为了提升成就感,我将计算器的大部分内容去除,于是就变成这样: 这样就只剩下了1 ...

  7. web框架-(四)Django进阶之数据库对象关系映射

    Django ORM基本配置 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MySQLdb 来连接数据库,并编写数据访问层代码 业务逻辑层去 ...

  8. C#基础知识之正则表达式

    正则表达式 是一种匹配输入文本的模式..Net 框架提供了允许这种匹配的正则表达式引擎.模式由一个或多个字符.运算符和结构组成. 实例 下面的实例匹配了以 'S' 开头的单词: using Syste ...

  9. Netty学习第三章 Linux网络编程使用的I/O模型

    一.同步阻塞IO:blocking IO(BIO) 1.过程分析: 当进程进行系统调用时,内核就会去准备数据,当数据准备好后就复制数据到内核缓冲器,复制完成后将数据拷贝到用户进程内存,整个过程都是阻塞 ...

  10. Spring Cloud(2)主要组件应用实例

    SpringCloud SpringCloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.负载均衡.微代理.事件总线.全局锁.决策竞选.分布式会话等等.它运行 ...