springboot基于elasticsearch(es)做搜索

    es的安装我这里不做详细介绍了,我是通过logstash将mysql的数据导入到es中的,具体怎么做的话后续会写相应的博客。以下是具体的代码实现SpringBoot和es的整合。

第一步、导入maven依赖:

springBoot的版本

  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
  </parent>

es依赖:

	  <dependency>
		  <groupId>org.springframework.boot</groupId>
		  <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
	  </dependency>

第二步、SpringBoot的配置

spring.data.elasticsearch.cluster-name=my-application
spring.data.elasticsearch.properties.transport.tcp.connect_timeout=120s
spring.data.elasticsearch.repositories.enabled=true
spring.data.elasticsearch.cluster-nodes=这是ip:9300

注意:spring.data.elasticsearch.cluster-name=my-application中的配置要和es中的配置一致,不然会报错的。即配置文件中cluster.name: my-application。

第四步、Controller:

package com.yarm.blog.controller;

import com.yarm.blog.pojo.es.EsBlog;
import com.yarm.blog.service.BlogSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.elasticsearch.index.query.QueryBuilders.queryStringQuery;

@Controller
public class SearchController {

    @Autowired
    private BlogSearchService blogSearchService;

    @GetMapping(value = "search/q")
    @ResponseBody
    public Map<String,Object> add(String q,Pageable pageable){
        //使用queryStringQuery完成单字符串查询
        Page<EsBlog> search = blogSearchService.search(q, q, pageable);
        Map<String, Object> map = new HashMap<>();
        map.put("data",search);
        return map;
    }
}

第五步、Service接口

package com.yarm.blog.service;

import com.yarm.blog.pojo.es.EsBlog;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

public interface BlogSearchService {
    //保存EsBlog实体
    void save (EsBlog eb);

    //基于title和content进行搜索,返回分页
    Page<EsBlog> search(String title, String content, Pageable pageable);

    //基于content进行搜索,返回分页
    Page<EsBlog> search(String content,Pageable pageable);

    //返回所有数据集合
    Page<EsBlog> findAll(Pageable pageable);
}

第六步、Service实现类

package com.yarm.blog.service.impl;

import com.yarm.blog.pojo.es.EsBlog;
import com.yarm.blog.repository.BlogRepository;
import com.yarm.blog.service.BlogSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
public class BlogSearchServiceImpl implements BlogSearchService {

    @Autowired
    private BlogRepository blogRepository;

    @Override
    public void save(EsBlog poem) {
        blogRepository.save(poem);
    }

    @Override
    public Page<EsBlog> search(String title, String content, Pageable pageable) {
        return blogRepository.findByTitleLikeOrContentLike(title,content,pageable);
    }

    @Override
    public Page<EsBlog> search(String content, Pageable pageable) {
        return blogRepository.findByContentLike(content,pageable);
    }

    @Override
    public Page<EsBlog> findAll(Pageable pageable) {
        return blogRepository.findAll(pageable);
    }
}

第七步、比较关键,继承ElasticsearchRepository

package com.yarm.blog.repository;

import com.yarm.blog.pojo.es.EsBlog;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

public interface BlogRepository extends ElasticsearchRepository<EsBlog, Long> {
    Page<EsBlog> findByTitleLikeOrContentLike(String title, String content, Pageable pageable);
    Page<EsBlog> findByContentLike(String content,Pageable pageable);
}

第八步、实体类,也是很重要的、容易出错,要搞好

package com.yarm.blog.pojo.es;

import org.springframework.data.elasticsearch.annotations.Document;

import java.util.Date;
@Document(indexName="blog",type="doc",indexStoreType="fs",shards=5,replicas=1,refreshInterval="-1")
public class EsBlog {
    private long id;

    private String uid;

    private String title;

    private String content;

    private Date createDate;

    private Date updateDate;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    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 Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public Date getUpdateDate() {
        return updateDate;
    }

    public void setUpdateDate(Date updateDate) {
        this.updateDate = updateDate;
    }
}

其中indexName="blog",type="doc"要对应好,可以在es中对应,如果是通过mysql同步过来的数据,可以看看脚本。

最后是源码,源码中是写在自己做的一个项目中,es是其中的一个模块。

地址:https://github.com/15902124763/entreprice-blog

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值