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

Spring data elasticsearch的使用

2018-01-29 21:31 519 查看

Spring data elasticsearch的使用

ArticleService

package cn.zxl.service;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import cn.zxl.domain.Article;

public interface ArticleService {
public void save(Article article);

public void delete(Article article);

public Article findOne(Integer id);

public Iterable<Article> findAll();

public Page<Article> findAll(Pageable pageable);

public List<Article> findByTitle(String title);

public Page<Article> findByTitle(String title, Pageable pageable);
}


ArticleServiceImpl

package cn.zxl.service;

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 cn.zxl.dao.ArticleRepository;
import cn.zxl.domain.Article;

@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 Article findOne(Integer id) {
return articleRepository.findOne(id);
}

public Iterable<Article> findAll() {
return articleRepository.findAll(new Sort(new Sort.Order(
Sort.Direction.ASC, "id")));
}

public Page<Article> findAll(Pageable pageable) {
return articleRepository.findAll(pageable);
}

public List<Article> findByTitle(String title) {
return articleRepository.findByTitle(title);
}

public Page<Article> findByTitle(String title, Pageable pageable) {
return articleRepository.findByTitle(title, pageable);
}

}


ArticleRepository

package cn.zxl.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 cn.zxl.domain.Article;

public interfac
4000
e ArticleRepository extends
ElasticsearchRepository<Article, Integer> {

List<Article> findByTitle(String title);

Page<Article> findByTitle(String title, Pageable pageable);

}


Article

package cn.zxl.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
@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 + "]";
}

}


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="cn.itcast.dao" />

<!-- 扫描Service包 -->
<context:component-scan base-package="cn.itcast.service" />

<!-- 配置elasticsearch 连接 -->
<elasticsearch:transport-client id="client" cluster-nodes="localhost:9300" />

<!-- spring data elasticsearch DAO 必须依赖 elasticsearchTemplate  -->
<bean id="elasticsearchTemplate"
class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
<constructor-arg name="client" ref="client" />
</bean>

</beans>


pom.xml

<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>cn.itcast.maven</groupId>
<artifactId>elasticsearch_springdata</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>elasticsearch_springdata</name>
<dependencies>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>2.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
</dependency>
</dependencies>
</project>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息