黑马十次方项目day04-11之索引库中搜索文章

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33229669/article/details/86563548

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;

/**
 * 类名称:ArticleDao
 * 类描述:文章的dao层
 *
 * @author: taohongchao
 * 创建时间:2019/1/20 15:32
 * Version 1.0
 */
public interface ArticleDao extends ElasticsearchRepository<Article,String> {

    /**
     * 方法名: findByTitleOrContentLike
     * 方法描述: 根据文章的标题 内容 进行模糊分页查询
     * 修改日期: 2019/1/20 16:16
      * @param title
     * @param content
     * @param pageable
     * @return org.springframework.data.domain.Page<com.tensquare.search.pojo.Article>
     * @author taohongchao
     * @throws
     */
    public Page<Article> findByTitleOrContentLike(String title, String content, Pageable pageable);

}


service

/**
     * 方法名: searchPageData
     * 方法描述: 关键词分页查询
     * 修改日期: 2019/1/20 16:25
      * @param keyword
     * @param page
     * @param size
     * @return org.springframework.data.domain.Page<com.tensquare.search.pojo.Article>
     * @author taohongchao
     * @throws
     */
    public Page<Article> searchPageData(String keyword, int page, int size) {
        //创建分页对象
        Pageable pageAble = PageRequest.of(page-1,size);

        //调用dao层,进行返回
        return articleDao.findByTitleOrContentLike(keyword,keyword,pageAble);
    }

Controller

/**
     * 方法名: searchByKeyWords
     * 方法描述: 根据搜索的关键词分页查询
     * 修改日期: 2019/1/20 16:22
      * @param keyword 搜索的关键词
     * @param page 当前页
     * @param size   每页显示条数
     * @return entity.Result
     * @author taohongchao
     * @throws
     */
    @RequestMapping(value = "/{page}/{size}/{keyword}",method = RequestMethod.GET)
    public Result searchByKeyWords(@PathVariable String keyword,
                                   @PathVariable int page,
                                   @PathVariable int size) {

        Page<Article> pageDate =  articleService.searchPageData(keyword, page, size);
        return new Result(true, StatusCode.OK, "查询成功", new PageResult<>(pageDate.getTotalElements(), pageDate.getContent()));
    }

测试

在postman中发送如下的get请求
http://localhost:9007/article/1/10/spring
返回的数据如下

{
    "flag": true,
    "code": 20000,
    "message": "查询成功",
    "data": {
        "total": 1,
        "rows": [
            {
                "id": "AWhqS0ytQ4-ZJZrBuY-b",
                "title": "测试新增索引",
                "content": "spring项目教程",
                "state": null
            }
        ]
    }
}

待解决的问题

当搜索的关键词为中文的时候,
例如
http://localhost:9007/article/1/10/项目
请求的状态码为400了

后台报错如下

java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986
at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:476) ~[tomcat-embed-core-8.5.29.jar:8.5.29

暂未找到解决方案

猜你喜欢

转载自blog.csdn.net/qq_33229669/article/details/86563548