分布式搜索引擎ElasticSearch-搜索微服务开发

分布式搜索引擎ElasticSearch-搜索微服务开发

  • 模块搭建
    • (1)创建模块tensquare_search ,pom.xml引入依赖
    • (2)application.yml
    • (3)创建包com.tensquare.search ,包下创建启动类
  • 添加文章
    • (1)创建实体类
    • (2)创建数据访问接口
    • (3)创建业务逻辑类
    • (4)创建控制器类
  • 文章搜索
    • (1)ArticleSearchRepository新增方法定义
    • (2)ArticleSearchService新增方法
    • (3)ArticleSearchController方法
  • 测试
    • 创建记录
    • 搜索

模块搭建

(1)创建模块tensquare_search ,pom.xml引入依赖

        
            org.springframework.data
            spring-data-elasticsearch
            3.0.6.RELEASE
        

(2)application.yml

server:
  port: 9007
spring:
  application:
    name: tensquare-search
  data:
     elasticsearch:
       cluster-nodes: 192.168.2.10:9300

(3)创建包com.tensquare.search ,包下创建启动类

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);
    }
}

添加文章

(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;//审核状态

......

}

(2)创建数据访问接口

创建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> {
    
}

(3)创建业务逻辑类

创建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);
    }
}

(4)创建控制器类

创建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, "添加成功");
    }
}

文章搜索

(1)ArticleSearchRepository新增方法定义

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);
}

(2)ArticleSearchService新增方法

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);
    }
}

(3)ArticleSearchController方法

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()));
    }
}

测试

创建记录

分布式搜索引擎ElasticSearch-搜索微服务开发_第1张图片

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 

分布式搜索引擎ElasticSearch-搜索微服务开发_第2张图片

你可能感兴趣的:(【微服务】,【ELK】)