ElasticSearch全文搜索小案例

ElasticSearch实战应用

ElasticSearch介绍

ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。
实际上它就是一个非关系型的数据库

实现demo

我用的框架是springboot+JPA,首先去ElasticSearch官网下载ElasticSearch,点击bin/elasticsearch.bat启动.

项目的pom依赖

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

因为elasticsearch 是一个非关系型数据库,所以要实现全文搜索,第一步就是把数据导入elasticsearch 中;
数据库的实体类如下:

@Entity
@Table(name="poetry")
public class Poetry implements Serializable {
	private static final long serialVersionUID = 1L;
	private Integer id;
	private String title;
	private String author;
	private String kind;
	private String intro;
	private String content;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Integer getId() {
		return id;
	}

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

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title=title;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author=author;
	}

	public String getKind() {
		return kind;
	}

	public void setKind(String kind) {
		this.kind=kind;
	}

	public String getIntro() {
		return intro;
	}

	public void setIntro(String intro) {
		this.intro=intro;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content=content;
	}
}	

再新建一个ElasticSearch的实体类

@Document(indexName = "knowledge", type = "poetry")
public class PoetryModel implements Serializable {
	private static final long serialVersionUID = 1L;
	private Integer id;
	private String title;
	private String author;
	private String kind;
	private String intro;
	private String content;

	@Id
	public Integer getId() {
		return id;
	}

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

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getKind() {
		return kind;
	}

	public void setKind(String kind) {
		this.kind = kind;
	}

	public String getIntro() {
		return intro;
	}

	public void setIntro(String intro) {
		this.intro = intro;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

}

一定要加上这个注解
import org.springframework.data.elasticsearch.annotations.Document;
@Document(indexName = “knowledge”, type = “poetry”);
indexName表示库名;poetry表示表名.

执行导入方法,把数据库的内容写到ElasticSearch中;

public void init() {
		List<Poetry> list = poetryRepository.findAll();
		for (Poetry p : list) {
			PoetryModel pModel = new PoetryModel();
			BeanUtils.copyProperties(p, pModel);
			poetryElasticRepository.save(pModel);
			System.out.println("导入-" + pModel.getId() + "-" + pModel.getTitle());
		}
	}

Controller层代码

@Controller
@RequestMapping("/poetry")
public class PoetryController extends BaseController {
    @Autowired
    private PoetryService poetryService;

    @RequestMapping("/init")
    public String init() {
        poetryService.init();
        return "demo/success";
    }

    @RequestMapping("/findByContent")
    @ResponseBody
    public List<PoetryModel> findByContent(String content) {

        return poetryService.findByContent(content);
    }

    @RequestMapping("/findByAuthor")
    @ResponseBody
    public List<PoetryModel> findByAuthor(String author) {
        return poetryService.findByAuthor(author);
    }
}

前段代码用的thymeleaf
代码成果:
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值