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

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

4、代码编写

 4.1 模块搭建

1 )创建模块 tensquare_search pom.xml 引入依赖
   
       <dependency><!--公共类的项目依赖-->
            <groupId>com.zwj</groupId>
            <artifactId>zhao588_common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-elasticsearch</artifactId>
            <version>3.0.6.RELEASE</version>
        </dependency>
(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<Article,String> {
 
}
(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<Article,String> {

    public Page<Article> 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<Article> 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<Article> pageDta = articleService.findKey(key,page,size);
        return new Result(true,StatusCode.OK,"查询成功",new PageResult<Article>(pageDta.getTotalElements(),pageDta.getContent()));

    }
}

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

 

Guess you like

Origin blog.csdn.net/qq_39772439/article/details/121378979