热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

SpringBoot2.x学习笔记十四:搜索引擎ElasticSearch5.6.x

1.SpringBoot2.x学习笔记十四:搜索引擎ElasticSearch5.6.x文章目录1.SpringBoot2.x学习笔记十四:搜索引擎El

1. SpringBoot2.x学习笔记十四:搜索引擎ElasticSearch5.6.x


文章目录

      • 1. SpringBoot2.x学习笔记十四:搜索引擎ElasticSearch5.6.x
        • 1.1. ElasticSearch搜索引擎的介绍
        • 1.2. SpringBoot2.x整合ElasticSearch5.6x


1.1. ElasticSearch搜索引擎的介绍


  1. 如果要检索数据库中的某些东西,对于mysql一般采用mysql的like语句进行模糊查询。
  2. 但是like进行查询对数据量不是大的还可以,数据库的like查询有性能问题,并且like语句无法做索引。
  3. 常用的搜索框架

1、solar

2、ElasticSearch(github,维基百科,stackOverflow网站)

这两个搜索框架底层都是由lucene写的。solar针对数据量不是特别大的企业服务,ElasticSearch针对数据量特别大的互联网企业。



  1. ElasticSearch的特点(纯java开发)

1、全文搜索,结构化搜索,数据统计、分析,接近实时处理,分布式搜索,处理PB级别的数据搜索纠错,自动完成。

使用场景:日志搜索,数据聚合,数据监控,报表统计分析。



  1. 注意

ES6.x不再支持一个索引库里面多个type,6.x里面已经禁止一个index里面多个type,所以一个index索引库只能存在1个type.

mysql : database : table : record(上下相对应)

ES6.x : index : type(只能存在一个) : document


ElasticSearch官方学习文档


  1. 查看集群状态

localhost:9200/_cat/health?v

  1. 查看服务器负载情况

localhost:9200/_cat/nodes?v

  1. 查看索引

localhost:9200/_cat/indices?v //get提交

  1. 创建索引

localhost:9200/customer?pretty //put提交

1.2. SpringBoot2.x整合ElasticSearch5.6x


  1. springData整合ElasticSearch

springData是springBoot用于整合各种框架的中间件,封装了整合各种框架的API



  1. application配置ES

spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300
spring.data.elasticsearch.repositories.enabled=true

  1. 创建domain对象

private static final long serialVersionUID=1L;
private long id;
private String title;
private String summary;
private String content;
private int pv;
public long getId() {return id;
}
public void setId(long id) {this.id = id;
}
public String getTitle() {return title;
}
public void setTitle(String title) {this.title = title;
}
public String getSummary() {return summary;
}
public void setSummary(String summary) {this.summary = summary;
}
public String getContent() {return content;
}
public void setContent(String content) {this.content = content;
}
public int getPv() {return pv;
}
public void setPv(int pv) {this.pv = pv;
}
public static long getSerialversionuid() {return serialVersionUID;
}

  1. 创建接口类似于操作数据库的dao层,该接口封装了crud操作

@Component
//@Repository,这个注解类似于Component
public interface ArticleRepository extends ElasticsearchRepository<Article,Long>{}

  1. 接口:存储和查询

&#64;Autowiredprivate ArticleRepository articleRepository;&#64;GetMapping("save")public Object save() {Article article&#61;new Article();article.setId(2L);article.setContent("this is 内容");article.setPv(888);article.setTitle("I love you");article.setSummary("概要搜索");articleRepository.save(article);return JsonData.buildSuccess();}&#64;GetMapping("search")public Object search(String title) {QueryBuilder queryBuilder&#61;QueryBuilders.matchQuery("title", title);Iterable<Article> list&#61;articleRepository.search(queryBuilder);return JsonData.buildSuccess(list);}

QueryBuilder是ES封装的查询接口。

我们会发现在ElasticSearch启动时&#xff0c;会占用两个端口9200和9300。
他们具体的作用如下&#xff1a;

9200 是ES节点与外部通讯使用的端口。它是http协议的RESTful接口&#xff08;各种CRUD操作都是走的该端口,如查询&#xff1a;http://localhost:9200/user/_search&#xff09;。
9300是ES节点之间通讯使用的端口。它是tcp通讯端口&#xff0c;集群间和TCPclient都走的它。&#xff08;java程序中使用ES时&#xff0c;在配置文件中要配置该端口&#xff09;



推荐阅读
author-avatar
圈闭仔笨仔
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有