您当前的位置: 首页 > 

java持续实践

暂无认证

  • 1浏览

    0关注

    746博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

黑马十次方项目day04-10之保存文章到索引库

java持续实践 发布时间:2019-01-20 16:09:42 ,浏览量:1

文章目录
      • 关于建立索引的三点考虑
      • 创建建立索引的实体类
      • dao
      • service
      • Controller
      • 测试

关于建立索引的三点考虑
  1. 是否建立索引 判断的依据是该域是否能被搜索到
  2. 是否分词 表示搜索的时候,是整体搜索匹配,还是分词的匹配
  3. 是否存储 判断依据是是否要在页面显示,以及是否要用到该数据.
创建建立索引的实体类

根据上面三点的判断, 将文章的id,标题,内容和状态进行了索引的存储.只要是在下面实体类中的属性, 都会进行存储. 由于id和状态虽然不会在页面显示,但在进行其他操作时,可能会用到该值,因此进行了存储.

文章的标题和内容由于是文本, 需要进行分词,无需完全的匹配.

在com.tensquare.search.pojo.Article下建立实体类

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;

/**
 * 类名称:Article
 * 类描述:文章存储到索引库的实体类
 * indexName = "tensequare_article" 表明该实体类,建立在tensequare_article索引上
 * type = "article" 表明索引的类型为article
 * @author: taohongchao
 * 创建时间:2019/1/20 15:18
 * Version 1.0
 */
@Document(indexName = "tensequare_article",type = "article")
public class Article implements Serializable {
    /**
     * 文章id
     */
    @Id
    private String id;

    /**
     * 文章标题
     * index = true 代表建立索引,
     * analyzer = "ik_max_word" 代表建立索引的分词,采用ik的最细粒度划分
     * searchAnalyzer = "ik_max_word" 代表搜索标题的时候,采用ik分词器的最细粒度的划分
     * 注意建立索引时的分词和搜索时的分词,必须一致,否则不能搜索到内容
     */
    @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;

}
dao

在com.tensquare.search.dao包下进行dao的编写

package com.tensquare.search.dao;

import com.tensquare.search.pojo.Article;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

/**
 * 类名称:ArticleDao
 * 类描述:文章的dao层
 *
 * @author: taohongchao
 * 创建时间:2019/1/20 15:32
 * Version 1.0
 */
public interface ArticleDao extends ElasticsearchRepository{
}
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.stereotype.Service;
import util.IdWorker;

/**
 * 类名称:ArticleService
 * 类描述:文章的service
 *
 * @author: taohongchao
 * 创建时间:2019/1/20 15:36
 * Version 1.0
 *
 */

@Service
public class ArticleService {

    @Autowired
    private ArticleDao articleDao;

    /**
     * 方法名: add
     * 方法描述: 保存文章
     * 修改日期: 2019/1/20 15:43
      * @param article
     * @return void
     * @author taohongchao
     * @throws
     */
    public void save(Article article) {
        articleDao.save(article);
    }
}

Controller
package com.tensquare.search.controller;

import com.tensquare.search.pojo.Article;
import com.tensquare.search.service.ArticleService;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 类名称:ArticleController
 * 类描述:文章的Controller层
 *
 * @author: taohongchao
 * 创建时间:2019/1/20 15:40
 * Version 1.0
 */

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

    @Autowired
    private ArticleService articleService;

    /**
     * 方法名: save
     * 方法描述: 保存文章
     * 修改日期: 2019/1/20 15:42
      * @param article
     * @return entity.Result
     * @author taohongchao
     * @throws
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result save(@RequestBody Article article){
        articleService.save(article);
        return new Result(true, StatusCode.OK, "新增成功!");
    }

}

测试

启动项目. 打开postman进行测试 在postman中,发送如下的post请求 http://localhost:9007/article 请求体中的json数据如下

 {
	"title": "测试新增索引",
	"content": "spring项目教程"
}

响应的数据如下

{
    "flag": true,
    "code": 20000,
    "message": "新增成功!",
    "data": null
}

在head插件中,进行文档的查看 可以看到有新增的数据,代表测试成功. 其中_id为elasticsearch自动生成的id, 而id为实体类中对应的id,由于在测试时,没有给该字段,因此为空

关注
打赏
1658054974
查看更多评论
立即登录/注册

微信扫码登录

0.0431s