目录
第二章 Spring Data ElasticSearch 使用
2.1 Spring Data ElasticSearch简介
2.1.2 什么是Spring Data ElasticSearch
2.2 Spring Data ElasticSearch入门
2.3 Spring Data ElasticSearch的常用操作
2.3.4 使用Elasticsearch的原生查询对象进行查询
第一章 ElasticSearch编程操作
1.1 创建工程,导入坐标
<?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>com.test</groupId>
<artifactId>springdata-elasticsearch</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.9</maven.compiler.source>
<maven.compiler.target>1.9</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>5.6.8</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.6.8</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>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</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>
1.2 创建索引index
1.3 创建映射mapping
package com.test.es;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Before;
import org.junit.Test;
import java.net.InetAddress;
public class ElasticSearchClientTest {
private TransportClient client;
@Before
public void init() throws Exception {
//创建一个Settings对象
Settings settings = Settings.builder()
.put("cluster.name", "my-elasticsearch")
.build();
//创建一个TransPortClient对象
client = new PreBuiltTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9301))
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9302))
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9303));
}
@Test
public void createIndex() throws Exception {
//1、创建一个Settings对象,相当于是一个配置信息。主要配置集群的名称。
Settings settings = Settings.builder()
.put("cluster.name", "my-elasticsearch")
.build();
//2、创建一个客户端Client对象
TransportClient client = new PreBuiltTransportClient(settings);
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9301));
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9302));
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9303));
//3、使用client对象创建一个索引库
client.admin().indices().prepareCreate("index_hello")
//执行操作
.get();
//4、关闭client对象
client.close();
}
@Test
public void setMappings() throws Exception {
//创建一个Settings对象
Settings settings = Settings.builder()
.put("cluster.name", "my-elasticsearch")
.build();
//创建一个TransPortClient对象
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9301))
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9302))
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9303));
//创建一个Mappings信息
/*{
"article":{
"properties":{
"id":{
"type":"long",
"store":true
},
"title":{
"type":"text",
"store":true,
"index":true,
"analyzer":"ik_smart"
},
"content":{
"type":"text",
"store":true,
"index":true,
"analyzer":"ik_smart"
}
}
}
}*/
XContentBuilder builder = XContentFactory.jsonBuilder()
.startObject()
.startObject("article")
.startObject("properties")
.startObject("id")
.field("type","long")
.field("store", true)
.endObject()
.startObject("title")
.field("type", "text")
.field("store", true)
.field("analyzer", "ik_smart")
.endObject()
.startObject("content")
.field("type", "text")
.field("store", true)
.field("analyzer","ik_smart")
.endObject()
.endObject()
.endObject()
.endObject();
//使用client把mapping信息设置到索引库中
client.admin().indices()
//设置要做映射的索引
.preparePutMapping("index_hello")
//设置要做映射的type
.setType("article")
//mapping信息,可以是XContentBuilder对象可以是json格式的字符串
.setSource(builder)
//执行操作
.get();
//关闭链接
client.close();
}
@Test
public void testAddDocument() throws Exception {
//创建一个client对象
//创建一个文档对象
XContentBuilder builder = XContentFactory.jsonBuilder()
.startObject()
.field("id",2l)
.field("title","北方入秋速度明显加快 多地降温幅度最多可达10度22222")
.field("content", "一架客机在机场被隔离")
.endObject();
//把文档对象添加到索引库
client.prepareIndex()
//设置索引名称
.setIndex("index_hello")
//设置type
.setType("article")
//设置文档的id,如果不设置的话自动的生成一个id
.setId("2")
//设置文档信息
.setSource(builder)
//执行操作
.get();
//关闭客户端
client.close();
}
@Test
public void testAddDocument2() throws Exception {
//创建一个Article对象
Article article = new Article();
//设置对象的属性
article.setId(3l);
article.setTitle("公司调十颗卫星去拍摄");
article.setContent("警惕荒唐的死亡游戏");
//把article对象转换成json格式的字符串。
ObjectMapper objectMapper = new ObjectMapper();
String jsonDocument = objectMapper.writeValueAsString(article);
System.out.println(jsonDocument);
//使用client对象把文档写入索引库
client.prepareIndex("index_hello","article", "3")
.setSource(jsonDocument, XContentType.JSON)
.get();
//关闭客户端
client.close();
}
@Test
public void testAddDocument3() throws Exception {
for (int i = 4; i < 100; i++) {
//创建一个Article对象
Article article = new Article();
//设置对象的属性
article.setId(i);
article.setTitle("救人是职责更是本能" + i);
article.setContent("调查" + i);
//把article对象转换成json格式的字符串。
ObjectMapper objectMapper = new ObjectMapper();
String jsonDocument = objectMapper.writeValueAsString(article);
System.out.println(jsonDocument);
//使用client对象把文档写入索引库
client.prepareIndex("index_hello","article", i + "")
.setSource(jsonDocument, XContentType.JSON)
.get();
}
//关闭客户端
client.close();
}
}
1.4 建立文档document
1.4.1 建立文档(通过XContentBuilder)
1.4.2 建立文档(使用Jackson转换实体)
1.5 查询文档操作
1.5.1 关键词查询
1.5.2 字符串查询
1.5.3 使用文档ID查询文档
1.6 查询文档分页操作
1.6.1 批量插入数据
1.6.2 分页查询
1.7 查询结果高亮操作
1.7.1 什么是高亮显示
在进行关键字搜索时,搜索出的内容中的关键字会显示不同的颜色,称之为高亮
1.7.2 高亮显示的html分析
1.7.3 高亮显示代码实现
第二章 Spring Data ElasticSearch 使用
2.1 Spring Data ElasticSearch简介
2.1.1 什么是Spring Data
Spring Data是一个用于简化数据库访问,并支持云服务的开源框架。其主要目标是使得对数据的访问变得方便快捷,并支持map-reduce框架和云计算数据服务。 Spring Data可以极大的简化JPA的写法,可以在几乎不用写实现 的情况下,实现对数据的访问和操作。除了CRUD外,还包括如分页、排序等一些常用的功能。
Spring Data的官网:http://projects.spring.io/spring-data/
2.1.2 什么是Spring Data ElasticSearch
Spring Data ElasticSearch 基于 spring data API 简化 elasticSearch操作,将原始操作elasticSearch的客户端API进行封装 。Spring Data为Elasticsearch项目提供集成搜索引擎。Spring Data Elasticsearch POJO的关键功能区域为中心的模型与Elastichsearch交互文档和轻松地编写一个存储库数据访问层。
官方网站:http://projects.spring.io/spring-data-elasticsearch/
2.2 Spring Data ElasticSearch入门
1)导入Spring Data ElasticSearch坐标
<?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>com.test</groupId>
<artifactId>springdata-elasticsearch</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.9</maven.compiler.source>
<maven.compiler.target>1.9</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>5.6.8</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.6.8</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>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</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>
2)创建applicationContext.xml配置文件,引入elasticsearch命名空间
<?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:9301,127.0.0.1:9302,127.0.0.1:9303"/>
<!--配置包扫描器,扫描dao的接口-->
<elasticsearch:repositories base-package="com.test.es.repositories"/>
<!---->
<bean id="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
<constructor-arg name="client" ref="esClient"/>
</bean>
</beans>
3)编写实体Article
package com.test.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 = "sdes_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;
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;
}
@Override
public String toString() {
return "Article{" +
"id=" + id +
", title='" + title + '\'' +
", content='" + content + '\'' +
'}';
}
}
4)编写Dao
package com.test.dao;
import com.test.domain.Article;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
@Repository
public interface ArticleRepository extends ElasticsearchRepository<Article, Integer> { }
5)编写Service
package com.test.service;
import com.test.domain.Article;
public interface ArticleService {
public void save(Article article);
}
package com.test.service.impl;
import com.test.dao.ArticleRepository;
import com.test.domain.Article;
import com.test.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ArticleServiceImpl implements ArticleService {
@Autowired
private ArticleRepository articleRepository;
public void save(Article article) {
articleRepository.save(article);
}
}
6) 配置applicationContext.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: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
">
<!‐‐ 扫描Dao包,自动创建实例 ‐‐>
<elasticsearch:repositories base‐package="com.test.dao"/>
<!‐‐ 扫描Service包,创建Service的实体 ‐‐>
<context:component‐scan base‐package="com.test.service"/>
<!‐‐ 配置elasticSearch的连接 ‐‐>
<elasticsearch:transport‐client id="client" cluster‐nodes="localhost:9300" cluster‐name="myelasticsearch"/>
<!‐‐ ElasticSearch模版对象 ‐‐>
<bean id="elasticsearchTemplate"
class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
<constructor‐arg name="client" ref="client"></constructor‐arg>
</bean>
</beans>
7)配置实体
基于spring data elasticsearch注解配置索引、映射和实体的关系
package com.test.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 文档对象 (索引信息、文档类型 )
@Document(indexName = "sdes_blog", type = "article")
public class Article {
//@Id 文档主键 唯一标识
@Id
//@Field 每个文档的字段配置(类型、是否分词、是否存储、分词器 )
@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;
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;
}
@Override
public String toString() {
return "Article{" +
"id=" + id +
", title='" + title + '\'' +
", content='" + content + '\'' +
'}';
}
}
8)创建测试类SpringDataESTest
import com.test.es.entity.Article;
import com.test.es.repositories.ArticleRepository;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
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.SearchResultMapper;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringDataElasticSearchTest {
@Autowired
private ArticleRepository articleRepository;
@Autowired
private ElasticsearchTemplate template;
@Test
public void createIndex() throws Exception {
//创建索引,并配置映射关系
template.createIndex(Article.class);
//配置映射关系
//template.putMapping(Article.class);
}
@Test
public void addDocument() throws Exception {
for (int i = 10; i <= 20; i++) {
//创建一个Article对象
Article article = new Article();
article.setId(i);
article.setTitle("寄语青少年" + i);
article.setContent("商务与投资峰会");
//把文档写入索引库
articleRepository.save(article);
}
}
@Test
public void deleteDocumentById() throws Exception {
articleRepository.deleteById(1l);
//全部删除
//articleRepository.deleteAll();
}
@Test
public void findAll() throws Exception {
Iterable<Article> articles = articleRepository.findAll();
articles.forEach(a-> System.out.println(a));
}
@Test
public void testFindById() throws Exception {
Optional<Article> optional = articleRepository.findById(1l);
Article article = optional.get();
System.out.println(article);
}
@Test
public void testFindByTitle() throws Exception {
List<Article> list = articleRepository.findByTitle("maven是一个工程构建工具");
list.stream().forEach(a-> System.out.println(a));
}
@Test
public void testFindByTitleOrContent() throws Exception {
Pageable pageable = PageRequest.of(1, 15);
articleRepository.findByTitleOrContent("maven", "商务与投资", pageable)
.forEach(a-> System.out.println(a));
}
@Test
public void testNativeSearchQuery() throws Exception {
//创建一个查询对象
NativeSearchQuery query = new NativeSearchQueryBuilder()
.withQuery(QueryBuilders.queryStringQuery("maven是一个工程构建工具").defaultField("title"))
.withPageable(PageRequest.of(0, 15))
.build();
//执行查询
List<Article> articleList = template.queryForList(query, Article.class);
articleList.forEach(a-> System.out.println(a));
}
}
2.3 Spring Data ElasticSearch的常用操作
2.3.1 增删改查方法测试
package com.test.service;
import com.test.domain.Article;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
public interface ArticleService {
//保存
public void save(Article article);
//删除
public void delete(Article article);
//查询全部
public Iterable<Article> findAll();
//分页查询
public Page<Article> findAll(Pageable pageable);
}
package com.test.service.impl;
import com.test.dao.ArticleRepository;
import com.test.domain.Article;
import com.test.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
@Service
public class ArticleServiceImpl implements ArticleService {
@Autowired
private ArticleRepository articleRepository;
public void save(Article article) {
articleRepository.save(article);
}
public void delete(Article article) {
articleRepository.delete(article);
}
public Iterable<Article> findAll() {
Iterable<Article> iter = articleRepository.findAll(); return iter;
}
public Page<Article> findAll(Pageable pageable) {
return articleRepository.findAll(pageable);
}
}
package com.test.test;
import com.test.domain.Article;
import com.test.service.ArticleService;
import org.elasticsearch.client.transport.TransportClient;
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;
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations="classpath:applicationContext.xml")
public class SpringDataESTest {
@Autowired
private ArticleService articleService;
@Autowired
private TransportClient client;
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
/**创建索引和映射*/
@Test
public void createIndex(){
elasticsearchTemplate.createIndex(Article.class);
elasticsearchTemplate.putMapping(Article.class);
}
/**测试保存文档*/
@Test
public void saveArticle(){
Article article = new Article();
article.setId(100);
article.setTitle("测试SpringData ElasticSearch");
article.setContent("Spring Data ElasticSearch 基于 spring data API 简化
elasticSearch操作,将原始操作elasticSearch的客户端API 进行封装 \n" + "
Spring Data为Elasticsearch Elasticsearch项目提供集成搜索引擎");
articleService.save(article);
}
/**测试保存*/
@Test
public void save(){
Article article = new Article();
article.setId(1001);
article.setTitle("elasticSearch 3.0版本发布");
article.setContent("ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多
用户能力的全文搜索引擎,基于RESTful web接口");
articleService.save(article);
}
/**测试更新*/
@Test
public void update(){
Article article = new Article();
article.setId(1001);
article.setTitle("elasticSearch 3.0版本发布...更新");
article.setContent("ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多
用户能力的全文搜索引擎,基于RESTful web接口");
articleService.save(article);
}
/**测试删除*/
@Test public void delete(){
Article article = new Article();
article.setId(1001);
articleService.delete(article);
}
/**批量插入*/
@Test
public void save100(){
for(int i=1;i<=100;i++){
Article article = new Article();
article.setId(i);
article.setTitle(i+"elasticSearch 3.0版本发布..,更新");
article.setContent(i+"ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分
布式多用户能力的全文搜索引擎,基于RESTful web接口");
articleService.save(article);
}
}
/**分页查询*/
@Test public void findAllPage(){
Pageable pageable = PageRequest.of(1,10);
Page<Article> page = articleService.findAll(pageable);
for(Article article:page.getContent()){
System.out.println(article);
}
}
}
2.3.2 常用查询命名规则
2.3.3 查询方法测试
1)dao层实现
package com.test.dao;
import com.test.domain.Article;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;
public interface ArticleRepository extends ElasticsearchRepository<Article, Integer> {
//根据标题查询
List<Article> findByTitle(String condition);
//根据标题查询(含分页)
Page<Article> findByTitle(String condition, Pageable pageable);
}
2)service层实现
public interface ArticleService {
//根据标题查询
List<Article> findByTitle(String condition);
//根据标题查询(含分页)
Page<Article> findByTitle(String condition, Pageable pageable);
}
package com.test.service.impl;
import com.test.dao.ArticleRepository;
import com.test.domain.Article;
import com.test.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ArticleServiceImpl implements ArticleService {
@Autowired
private ArticleRepository articleRepository;
public List<Article> findByTitle(String condition) {
return articleRepository.findByTitle(condition);
}
public Page<Article> findByTitle(String condition, Pageable pageable) {
return articleRepository.findByTitle(condition,pageable);
}
}
3)测试代码
package com.test.test;
import com.test.domain.Article;
import com.test.service.ArticleService;
import org.elasticsearch.client.transport.TransportClient;
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 java.util.List;
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations="classpath:applicationContext.xml")
public class SpringDataESTest {
@Autowired private
ArticleService articleService;
@Autowired
private TransportClient client;
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
/**条件查询*/
@Test
public void findByTitle(){
String condition = "版本";
List<Article> articleList = articleService.findByTitle(condition);
for(Article article:articleList){
System.out.println(article);
}
}
/**条件分页查询*/
@Test
public void findByTitlePage(){
String condition = "版本";
Pageable pageable = PageRequest.of(2,10);
Page<Article> page = articleService.findByTitle(condition,pageable);
for(Article article:page.getContent()){
System.out.println(article);
}
}
}
2.3.4 使用Elasticsearch的原生查询对象进行查询
@Test
public void findByNativeQuery() {
//创建一个SearchQuery对象
SearchQuery searchQuery = new NativeSearchQueryBuilder()
//设置查询条件,此处可以使用QueryBuilders创建多种查询
.withQuery(QueryBuilders.queryStringQuery("备份节点上没有数
据").defaultField("title"))
//还可以设置分页信息
.withPageable(PageRequest.of(1, 5))
//创建SearchQuery对象
.build();
//使用模板对象执行查询
elasticsearchTemplate.queryForList(searchQuery, Article.class)
.forEach(a‐> System.out.println(a));
}
第三章 总结
一、使用Java客户端管理ES
1、创建索引库
步骤:
1)创建一个Java工程
2)添加jar包,添加maven的坐标
3)编写测试方法实现创建索引库
1、创建一个Settings对象,相当于是一个配置信息。主要配置集群的名称。
2、创建一个客户端Client对象
3、使用client对象创建一个索引库
4、关闭client对象
2、使用Java客户端设置Mappings
步骤:
1)创建一个Settings对象
2)创建一个Client对象
3)创建一个mapping信息,应该是一个json数据,可以是字符串,也可以是XContextBuilder对象
4)使用client向es服务器发送mapping信息
5)关闭client对象
3、添加文档
步骤:
1)创建一个Settings对象
2)创建一个Client对象
3)创建一个文档对象,创建一个json格式的字符串,或者使用XContentBuilder
4)使用Client对象吧文档添加到索引库中
5)关闭client
4、添加文档第二种方式
创建一个pojo类
使用工具类把pojo转换成json字符串
把文档写入索引库
二、使用es客户端实现搜索
1、根据id搜索
QueryBuilder queryBuilder = QueryBuilders.idsQuery().addIds("1", "2");
2、根据Term查询(关键词)
QueryBuilder queryBuilder = QueryBuilders.termQuery("title", "北方");
3、QueryString查询方式(带分析的查询)
QueryBuilder queryBuilder = QueryBuilders.queryStringQuery("速度与激情")
.defaultField("title");
查询步骤:
1)创建一个Client对象
2)创建一个查询对象,可以使用QueryBuilders工具类创建QueryBuilder对象。
3)使用client执行查询
4)得到查询的结果。
5)取查询结果的总记录数
6)取查询结果列表
7)关闭client
4、分页的处理
在client对象执行查询之前,设置分页信息。
然后再执行查询
//执行查询
SearchResponse searchResponse = client.prepareSearch("index_hello")
.setTypes("article")
.setQuery(queryBuilder)
//设置分页信息
.setFrom(0)
//每页显示的行数
.setSize(5)
.get();
分页需要设置两个值,一个from、size
from:起始的行号,从0开始。
size:每页显示的记录数
5、查询结果高亮显示
(1)高亮的配置
1)设置高亮显示的字段
2)设置高亮显示的前缀
3)设置高亮显示的后缀
(2)在client对象执行查询之前,设置高亮显示的信息。
(3)遍历结果列表时可以从结果中取高亮结果。
三、SpringDataElasticSearch
1、工程搭建
1)创建一个java工程。
2)把相关jar包添加到工程中。如果maven工程就添加坐标。
3)创建一个spring的配置文件
1、配置elasticsearch:transport-client
2、elasticsearch:repositories:包扫描器,扫描dao
3、配置elasticsearchTemplate对象,就是一个bean
2、管理索引库
1、创建一个Entity类,其实就是一个JavaBean(pojo)映射到一个Document上
需要添加一些注解进行标注。
2、创建一个Dao,是一个接口,需要继承ElasticSearchRepository接口。
3、编写测试代码。
3、创建索引
直接使用ElasticsearchTemplate对象的createIndex方法创建索引,并配置映射关系。
4、添加、更新文档
1)创建一个Article对象
2)使用ArticleRepository对象向索引库中添加文档。
5、删除文档
直接使用ArticleRepository对象的deleteById方法直接删除。
6、查询索引库
直接使用ArticleRepository对象的查询方法。
7、自定义查询方法
需要根据SpringDataES的命名规则来命名。
如果不设置分页信息,默认带分页,每页显示10条数据。
如果设置分页信息,应该在方法中添加一个参数Pageable
Pageable pageable = PageRequest.of(0, 15);
注意:设置分页信息,默认是从0页开始。
可以对搜索的内容先分词然后再进行查询。每个词之间都是and的关系。
8、使用原生的查询条件查询
NativeSearchQuery对象。
使用方法:
1)创建一个NativeSearchQuery对象
设置查询条件,QueryBuilder对象
2)使用ElasticSearchTemplate对象执行查询
3)取查询结果