當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring data elasticsearch的使用
生活随笔
收集整理的這篇文章主要介紹了
Spring data elasticsearch的使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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 {@Autowiredprivate 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 interface ArticleRepository extendsElasticsearchRepository<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;}@Overridepublic 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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/data/elasticsearchhttp://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>總結
以上是生活随笔為你收集整理的Spring data elasticsearch的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【webservice】spring整合
- 下一篇: Highcharts JS去除Highc