使用Spring Data ElasticSearch框架来处理索引
/**步骤:创建工程,导入相应的包---》配置文件----》创建实体类对象------》创建接口----》测试增删改查的方法
**/
//步骤:创建工程,导入相应的包
<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>
//配置文件
<?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"/>
<!-- 配置扫描器,扫描到的接口-->
<elasticsearch:repositories base-package="com.hope.es.repositories"/>
<!--配置模版-->
<bean id="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
<constructor-arg name="client" ref="esClient"/>
</bean>
</beans>
//创建实体类
package com.hope.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;
/**
* @author newcityman
* @date 2020/1/17 - 16:15
*/
@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 + '\'' +
'}';
}
}
//创建接口
package com.hope.es.repositories;
import com.hope.es.entity.Article;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;
/**
* @author newcityman
* @date 2020/1/17 - 16:21
*/
public interface ArticleRepository extends ElasticsearchRepository<Article,Long> {
List<Article> findByTitle(String title);
List<Article> findByTitleOrContent(String title,String content);
List<Article> findByTitleOrContent(String title, String content, Pageable pageable);
}
//书写测试方法
import com.hope.es.entity.Article;
import com.hope.es.repositories.ArticleRepository;
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.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
import java.util.Optional;
/**
* @author newcityman
* @date 2020/1/17 - 16:29
*/
@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);
}
@Test
public void addDocument() throws Exception{
for (int i = 0; i < 20; i++) {
Article article = new Article();
article.setId(i);
article.setTitle("美国军舰过航台湾海峡 外交部回应"+i);
article.setContent("针对美国军舰过航台湾海峡的情况," +
"中国外交部发言人耿爽在今天(17日)的例行记者会上表示," +
"中方密切关注并且全程掌握美国军舰过航台湾海峡的情况"+i);
articleRepository.save(article);
}
Article article = new Article();
article.setId(2);
article.setTitle("美国军舰过航台湾海峡 外交部回应2");
article.setContent("针对美国军舰过航台湾海峡的情况," +
"中国外交部发言人耿爽在今天(17日)的例行记者会上表示," +
"中方密切关注并且全程掌握美国军舰过航台湾海峡的情况2");
articleRepository.save(article);
}
@Test
public void deleteDocumentById() throws Exception{
articleRepository.deleteById(1l);
}
@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);
if(optional!=null){
Article article = optional.get();
System.out.println(article);
}
}
@Test
public void testFindByTitle() throws Exception{
List<Article> list = articleRepository.findByTitle("外交部");
list.stream().forEach(a-> System.out.println(a));
}
@Test
public void testFindByTitleOrContent() throws Exception{
Pageable pageable = PageRequest.of(0,5);
articleRepository.findByTitleOrContent("外交部", "美国",pageable)
.forEach(a-> System.out.println(a));
/*list.stream().forEach(a-> System.out.println(a));*/
}
@Test
public void testNativeSearchQuery() throws Exception{
NativeSearchQuery query = new NativeSearchQueryBuilder().withQuery(QueryBuilders.queryStringQuery("我想进外交部")
.defaultField("title")).withPageable(PageRequest.of(0, 5)).build();
template.queryForList(query,Article.class).stream().forEach(a-> System.out.println(a));
}
}