栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

分布式搜索引擎ElasticSearch之搜索微服务开发(四)

分布式搜索引擎ElasticSearch之搜索微服务开发(四)

继上篇文章:分布式搜索引擎ElasticSearch之IK分词器(三)

4、代码编写  4.1 模块搭建 ( 1 )创建模块 tensquare_search , pom.xml 引入依赖    
       
            com.zwj
            zhao588_common
            1.0-SNAPSHOT
        
        
            org.springframework.data
            spring-data-elasticsearch
            3.0.6.RELEASE
        
(2) application.yml    
server:
  port: 9007
spring:
  application:
    name: zhao588-search
  data:
    elasticsearch:
      cluster-nodes: 127.0.0.1:9300
(3)创建包 com.zhao588.search ,包下创建启动类  
import com.zhao588.util.IdWorker;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class SearchApplication {

    public static void main(String[] args) {
        SpringApplication.run(SearchApplication.class,args);
    }

    @Bean
    public IdWorker idWorker(){

        return new IdWorker();
    }
}
4.2  添加文章 ( 1 )创建实体类 创建 com.zhao588.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="zhao588_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;//审核状态


    public String getId() {
        return id;
    }

    public void setId(String 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;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }
}
(2)创建数据访问接口 创建 com.zhao588.search.dao 包,包下建立接口
import com.zhao588.search.pojo.Article;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

public interface ArticleDao extends ElasticsearchRepository {
 
}
(3)创建业务逻辑类 创建 com.zhao588.search.service 包,包下建立类
import com.zhao588.search.dao.ArticleDao;
import com.zhao588.search.pojo.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ArticleService {

    @Autowired
    private ArticleDao articleDao;

    public void add(Article article){
        article.setId(idWorker.nextId()+"");
        articleDao.save(article);
    }
 
}
(4)创建控制器类 创建 com.zhao588.search.controller 包,包下建立类
import com.zhao588.entity.Result;
import com.zhao588.entity.StatusCode;
import com.zhao588.search.pojo.Article;
import com.zhao588.search.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/article")
@CrossOrigin
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @RequestMapping(method = RequestMethod.POST)
    public Result add(@RequestBody Article article){
        articleService.add(article);
        return new Result(true,StatusCode.OK,"添加成功");
    }

 
}

 测试添加

 返回,添加成功。

4.3 文章搜索 ( 1 ) ArticleSearchRepository 新增方法定义
import com.zhao588.search.pojo.Article;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

public interface ArticleDao extends ElasticsearchRepository {

    public Page findByTitleOrContentLike(String title, String content, Pageable pageable);
}

(2)ArticleSearchService新增方法

import com.zhao588.search.dao.ArticleDao;
import com.zhao588.search.pojo.Article;
import com.zhao588.util.IdWorker;
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 add(Article article){
        article.setId(idWorker.nextId()+"");
        articleDao.save(article);
    }

    public Page findKey(String key, int page, int size) {
        Pageable pageable = PageRequest.of(page-1,size);
        return  articleDao.findByTitleOrContentLike(key,key,pageable);
    }
}
(3) ArticleSearchController 方法
import com.zhao588.entity.PageResult;
import com.zhao588.entity.Result;
import com.zhao588.entity.StatusCode;
import com.zhao588.search.pojo.Article;
import com.zhao588.search.service.ArticleService;
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 add(@RequestBody Article article){
        articleService.add(article);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    @RequestMapping(value = "/{key}/{page}/{size}",method =RequestMethod.GET)
    public Result findKey(@PathVariable String key,@PathVariable int page,@PathVariable int size){
        Page pageDta = articleService.findKey(key,page,size);
        return new Result(true,StatusCode.OK,"查询成功",new PageResult(pageDta.getTotalElements(),pageDta.getContent()));

    }
}

测试 :http://127.0.0.1:9007/article/java/1/1

继下篇:CSDN分布式搜索引擎Elasticsearch与MySQL数据同步(五)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/584521.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号