Springboot + ElasticSearch构建搜索引擎

描述

通过Springboot+ElasticSearch实现快速的搜索系统。

环境:

  1. 安装ElasticSearch:
  2. 创建一个SpringBoot项目: 需要的依赖
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

创建搜索对象

  1. 创建对象映射ES里要搜索的数据:
import com.fasterxml.jackson.annotation.JsonAlias;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.DateFormat;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import java.sql.Date;

@Data
@Document(indexName = "blog", type = "doc",useServerConfiguration = true,createIndex = false)
public class EsTblog {

    @Id
    private Integer id;

    @Field(type = FieldType.Text,analyzer = "ik_max_word")
    private String title;

    @Field(type = FieldType.Text,analyzer = "ik_max_word")
    private String author;

    @Field(type = FieldType.Text,analyzer = "ik_max_word")
    private String content;

    @JsonAlias(value = "create_time")
    @Field(type = FieldType.Date,format = DateFormat.custom,pattern = "yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || epoch_millis")
    private Date createTime;

    @JsonAlias(value = "update_time")
    @Field(type = FieldType.Date,format = DateFormat.custom,pattern = "yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || epoch_millis")
    private Date updateTime;
}
  1. @Document(indexName = “blog”, type = “doc”,useServerConfiguration = true,createIndex = false): indexName对应ES里面的index,type对应ES里面的type(固定为doc),useServerConfiguration为true表示用线上ES的配置,createIndex 为false表示启动springBoot项目的时候不用重新创建index,这样是避免删除线上的index
  2. @Id: 设置ES的id
  3. @Field(type = FieldType.Text,analyzer = “ik_max_word”): type = FieldType.Text,analyzer表示选择用哪种分词器,建议使用"ik_max_word"分词器,这样对中文支持友好;type = FieldType.Date,format = DateFormat.custom,pattern表示选择时间类型
  4. @JsonAlias(value = “create_time”): ES里面的对象与项目实体类里面的对象名称不同是可以用这个注解进行映射

创建Repository接口

  1. 根据实体对象创建EsTblogRepository接口:
import com.xzb.demo.spring_ik_es.entity.es.EsTblog;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

public interface EsTblogRepository extends ElasticsearchRepository<EsTblog,Integer> {
}
  1. 创建一个Controller类用于调用接口对象:
@PostMapping("/serch")
    public ReturnMessage search(@RequestBody String param) {
        Map hashMap = new HashMap();
        StopWatch watch = new StopWatch();
        watch.start();
        //创建ES搜索语句
        BoolQueryBuilder builder = QueryBuilders.boolQuery();
        builder.should(QueryBuilders.matchPhraseQuery("title",param));
        builder.should(QueryBuilders.matchPhraseQuery("content",param));
        Page<EsTblog> page = (Page<EsTblog>) esTblogRepository.search(builder);
        List<EsTblog> content  = page.getContent();
        hashMap.put("list",content);
        
        watch.stop();
        //获取查询时间
        long date = watch.getTotalTimeMillis();
        hashMap.put("date",date);
        return returnMessage(hashMap);
    }

备注: 创建ES语句可以对照着kibana里面的Dev Tools语句来写,到此一个简单的搜索引擎就搭建完了,mysql与es同步的方法已经在上一次讲了。

经验分享 程序员 微信小程序 职场和发展