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
findAll(); public Page
findAll(Pageable pageable); public List
findByTitle(String title); public Page
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
findAll() { return articleRepository.findAll(new Sort(new Sort.Order( Sort.Direction.ASC, "id"))); } public Page
findAll(Pageable pageable) { return articleRepository.findAll(pageable); } public List
findByTitle(String title) { return articleRepository.findByTitle(title); } public Page
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 extends
        ElasticsearchRepository {

    List
findByTitle(String title); Page
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




    
    

    
    

    
    

    
    
        
    


pom.xml


  4.0.0
  cn.itcast.maven
  elasticsearch_springdata
  0.0.1-SNAPSHOT
  elasticsearch_springdata
  
    
        org.elasticsearch
        elasticsearch
        2.4.0
    
    
        org.springframework.data
        spring-data-elasticsearch
        2.0.4.RELEASE
    
    
        org.springframework
        spring-test
        4.2.8.RELEASE
    
    
        junit
        junit
        4.12
    
    
        org.slf4j
        slf4j-log4j12
        1.7.12
    
  

你可能感兴趣的:(后台编程,elasticsearch,学亮说JAVA)