一、使用前设置

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

最新文章

  1. ES5 getter setter
  2. html 组装table 指定列自动换行
  3. paip.java UrlRewrite 的原理and实现 htaccess正则表达式转换
  4. [原]Android开发环境搭建
  5. linux定时执行php脚本
  6. vs2012中的小技巧2
  7. Xamarin.Android 上中下布局
  8. 反编译微信小程序
  9. BZOJ 2457 - 双端队列 - [思维题]
  10. Jenkins官方教程地址入口
  11. Python3基础 __repr__ 实例对象的名字,可以显示信息
  12. slf4j+log4j的初次使用
  13. iOS9中怎样在日历App中创建一个随意时间之前開始的提醒(三)
  14. mysql添加和root用户一样的权限
  15. 人脸识别技术大总结1——Face Detection &amp; Alignment
  16. jQuery插件的开发(一)
  17. Problem E: 零起点学算法34——3n+1问题
  18. 【SSH】——spring的控制反转和依赖注入
  19. Oz 创建Windows2008R2镜像
  20. COGS 1507. [IOI2000]邮局

热门文章

  1. 基于BufferedImage的图像滤镜演示
  2. 【问题解决方案】git仓库重构
  3. Show Profile
  4. java创建对象的5种方法
  5. 3.17内存,进程,rpm和yum,python编译安装
  6. CentOS上安装Git及配置远程仓库
  7. Error response from daemon: Container ************** is not running
  8. 【学习】019 SpringBoot
  9. bzoj4819 [Sdoi2017]新生舞会 分数规划+最大费用最大流
  10. Win10离线安装.NET Framework 3.5的方法补充(附cab格式离线安装包下载) - 转载