(1)pom.xml
<?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">
<parent>
<artifactId>elasticSearch-demo</artifactId>
<groupId>net.xiaof</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>es-springData</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
</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.25</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>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
(2)spring配置文件: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">
<elasticsearch:repositories base-package="net.xiaof.dao"/>
<elasticsearch:transport-client id="esClient" cluster-nodes="127.0.0.1:9300,127.0.0.1:9301,127.0.0.1:9302" cluster-name="my-elasticsearch"/>
<bean id="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
<constructor-arg name="client" ref="esClient" />
</bean>
</beans>
(3)实体类:Article.java
package net.xiaof.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;
import java.io.Serializable;
@Document(indexName = "index_blog2",type = "article")
public class Article implements Serializable {
@Id
@Field(store = true,index = false,type = FieldType.Long)
private Long id;
@Field(index = true,analyzer = "ik_smart",store = true,searchAnalyzer = "ik_smart",type = FieldType.text)
private String title;
@Field(index = true,analyzer = "ik_smart",store = true,searchAnalyzer = "ik_smart",type = FieldType.text)
private String content;
public Article() {
}
public Article(long id, String title, String content) {
this.id = id;
this.title = title;
this.content = 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层接口:ArticleRepository.java
package net.xiaof.dao;
import net.xiaof.entity.Article;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface ArticleRepository extends ElasticsearchRepository<Article, Long> {
List<Article> findByTitle(String title);
Page<Article> findByTitle(String title, Pageable pageable);
}
(5)测试类:SpringData_ESTest.java
package net.xiaof.test;
import net.xiaof.dao.ArticleRepository;
import net.xiaof.entity.Article;
import org.elasticsearch.index.query.QueryBuilders;
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.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.Optional;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class SpringData_ESTest {
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@Autowired
private ArticleRepository articleRepository;
@Test
public void TestCreateIndex() {
elasticsearchTemplate.createIndex(Article.class);
}
@Test
public void TestAddDocument() {
String title = "ElasticSearch是一个基于Lucene的搜索服务器";
String content = "它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。";
Article article = new Article(1, title, content);
articleRepository.save(article);
}
@Test
public void TestUpdateDocument() {
String title = "(修改后)ElasticSearch是一个基于Lucene的搜索服务器";
String content = "(修改后)它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。";
Article article = new Article(1, title, content);
articleRepository.save(article);
}
@Test
public void TestGetById() {
Optional<Article> articleOptional = articleRepository.findById(new Long(1));
Article article = articleOptional.orElse(null);
System.out.println(article);
}
@Test
public void TestGetAllDocument() {
Iterable<Article> allArticle = articleRepository.findAll();
allArticle.forEach(article -> System.out.println(article));
}
@Test
public void TestDelDocument() {
articleRepository.deleteById(new Long(1));
}
@Test
public void TestBatchAddDocument() {
String title = "ElasticSearch是一个基于Lucene的搜索服务器";
String content = "它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。";
Article article = null;
for (int i = 1; i <= 100; i++) {
article = new Article(i, "(新" + i + ")" + title, "(新" + i + ")" + content);
articleRepository.save(article);
}
}
@Test
public void TestGetForPaging() {
Pageable pageable = PageRequest.of(1, 6);
Page<Article> articlePage = articleRepository.findAll(pageable);
articlePage.forEach(article -> System.out.println(article));
}
@Test
public void TestGetByTitle() {
String title = "新9";
List<Article> articleList = articleRepository.findByTitle(title);
articleList.forEach(article -> System.out.println(article));
}
@Test
public void TestGetByTitleAndPaging() {
String title = "搜索";
Pageable pageable = PageRequest.of(1, 6);
Page<Article> articlePage = articleRepository.findByTitle(title, pageable);
articlePage.forEach(article -> System.out.println(article));
}
@Test
public void findByNativeQuery() {
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(QueryBuilders.queryStringQuery("搜索").defaultField("title"))
.withPageable(PageRequest.of(1, 5)).build();
List<Article> articleList = elasticsearchTemplate.queryForList(searchQuery, Article.class);
articleList.forEach(a -> System.out.println(a));
}
}