您的位置:首页 > 编程语言 > Java开发

002-es5.4.3结合spring-data-elasticsearch3.0.0.0使用

2018-03-03 10:41 489 查看

一、使用前设置

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配置

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);
}
}

}


View Code
参考地址:https://www.cnblogs.com/dijia478/p/7839110.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: