org.springframework.data
spring-data-elasticsearch
3.0.6.RELEASE
server:
port: 9007
spring:
application:
name: tensquare-search
data:
elasticsearch:
cluster-nodes: 192.168.2.10:9300
package com.tensquare.search;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import util.IdWorker;
@SpringBootApplication
public class SearchApplication {
// com.tensquare.search.SearchApplication
public static void main(String[] args) {
SpringApplication.run(SearchApplication.class);
}
@Bean
public IdWorker idWorker(){
return new IdWorker(1,1);
}
}
创建com.tensquare.search.pojo包,包下建立类
package com.tensquare.search.pojo;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import java.io.Serializable;
@Document(indexName = "tensquare_article", type = "article")
public class Article implements Serializable {
@Id
private String id;
//是否索引,就是看该域是否能被搜索。
//是否分词,就表示搜索的时候是整体匹配还是单词匹配
//是否存储,就是是否在页面上显示
@Field(index = true, analyzer="ik_max_word", searchAnalyzer="ik_max_word")
private String title;
@Field(index = true, analyzer="ik_max_word", searchAnalyzer="ik_max_word")
private String content;
private String state;//审核状态
......
}
创建com.tensquare.search.dao包,包下建立接口
package com.tensquare.search.dao;
import com.tensquare.search.pojo.Article;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
/**
* application.yml
*/
public interface ArticleDao extends ElasticsearchRepository<Article, String> {
}
创建com.tensquare.search.service包,包下建立类
package com.tensquare.search.service;
import com.tensquare.search.dao.ArticleDao;
import com.tensquare.search.pojo.Article;
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.stereotype.Service;
@Service
public class ArticleService {
@Autowired
private ArticleDao articleDao;
// @Autowired
// private IdWorker idWorker;
public void save(Article article){
//article.setId(idWorker.nextId()+"");
articleDao.save(article);
}
}
创建com.tensquare.search.controller包,包下建立类
package com.tensquare.search.controller;
import com.tensquare.search.pojo.Article;
import com.tensquare.search.service.ArticleService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/article")
@CrossOrigin
public class ArticleController {
@Autowired
private ArticleService articleService;
@RequestMapping(method = RequestMethod.POST)
public Result save(@RequestBody Article article){
articleService.save(article);
return new Result(true, StatusCode.OK, "添加成功");
}
}
package com.tensquare.search.dao;
import com.tensquare.search.pojo.Article;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
/**
* application.yml
*/
public interface ArticleDao extends ElasticsearchRepository<Article, String> {
public Page<Article> findByTitleOrContentLike(String title, String content, Pageable pageable);
}
package com.tensquare.search.service;
import com.tensquare.search.dao.ArticleDao;
import com.tensquare.search.pojo.Article;
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.stereotype.Service;
@Service
public class ArticleService {
@Autowired
private ArticleDao articleDao;
// @Autowired
// private IdWorker idWorker;
public void save(Article article){
//article.setId(idWorker.nextId()+"");
articleDao.save(article);
}
public Page<Article> findByKey(String key, int page, int size) {
Pageable pageable = PageRequest.of(page-1, size);
return articleDao.findByTitleOrContentLike(key, key, pageable);
}
}
package com.tensquare.search.controller;
import com.tensquare.search.pojo.Article;
import com.tensquare.search.service.ArticleService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/article")
@CrossOrigin
public class ArticleController {
@Autowired
private ArticleService articleService;
@RequestMapping(method = RequestMethod.POST)
public Result save(@RequestBody Article article){
articleService.save(article);
return new Result(true, StatusCode.OK, "添加成功");
}
@RequestMapping(value = "/{key}/{page}/{size}", method = RequestMethod.GET)
public Result findByKey(@PathVariable String key, @PathVariable int page, @PathVariable int size){
Page<Article> pageData = articleService.findByKey(key, page, size);
return new Result(true, StatusCode.OK, "查询成功", new PageResult<Article>(pageData.getTotalElements(), pageData.getContent()));
}
}
http://localhost:9007/article
body
{
"title":"test-title-3",
"content":"test-content-3"
}
放回结果
{
"flag": true,
"code": 20000,
"message": "添加成功",
"data": null
}
根据key来查询文章
http://localhost:9007/article/test/1/10