01-springboot整合elasticsearch初识
1.elasticsearch
1.es简介
Elasticsearch 是一个分布式、高扩展、高实时的搜索与数据分析引擎。它能很方便的使大量数据具有搜索、分析和探索的能力。充分利用Elasticsearch的水平伸缩性,能使数据在生产环境变得更有价值。Elasticsearch 的实现原理主要分为以下几个步骤,首先用户将数据提交到Elasticsearch 数据库中,再通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据,当用户搜索数据时候,再根据权重将结果排名,打分,再将返回结果呈现给用户。
2.安装es
2.1 docker 安装
1.下载docker镜像
docker pull elasticsearch:7.7.1
2.启动 es
docker run -d --name es -p 9200:9200 -p 9300:9300 -e ES_JAVA_OPTS="-Xms512m -Xmx512m" -e "discovery.type=single-node" 7ec4f35ab452
lucence底层是Java写的,所以增加了参数,不然es开启的时候就会占用内存2G
2.2 linux 安装 es
具体查看官网,有详细过程介绍:https://www.elastic.co/guide/en/elasticsearch/reference/current/targz.html
2.springboot整合es简单测试
springboot官网地址:https://docs.spring.io/spring-data/elasticsearch/docs/4.0.1.RELEASE/reference/html/#elasticsearch.clients
es 官网地址:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-getting-started.html
2.1 添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
2.2 配置客户端
@Configuration
public class RestClientConfig extends AbstractElasticsearchConfiguration {
/**
* 生成rest的高级客户端
* @return
*/
@Override
@Bean
public RestHighLevelClient elasticsearchClient() {
final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
.connectedTo("106.52.137.XX:9200")
.build();
return RestClients.create(clientConfiguration).rest();
}
}
2.3 实体类对象
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Document(indexName="person_index")
public class Person implements Serializable {
@Id
private String id;
private String name;
private Integer age;
}
2.4 测试存储数据
@Autowired
private ElasticsearchOperations operations ;
@Test
public void saveEs(){
Person person = Person.builder().id("66666").name("张三").age(18).build();
IndexQuery indexQuery = new IndexQueryBuilder()
.withId(person.getId())
.withObject(person)
.build();
String documentId = operations.index(indexQuery);
log.info("================ saveEs {} =================",documentId);
}
2.5 测试读取数据
@Test
public void getPerson(){
Person person = operations.queryForObject(GetQuery.getById("66666"), Person.class);
log.info("----------------- getPerson:{} ---------------------------",person);
}
3.模型对象存储
3.1 使用注解标识实体类属性
(以下摘自官方文档: https://docs.spring.io/spring-data/elasticsearch/docs/4.0.1.RELEASE/reference/html/#elasticsearch.mapping)
@Document:在类级别应用,以指示该类是映射到数据库的候选对象。最重要的属性是:
indexName:用于存储此实体的索引的名称
type:映射类型。如果未设置,则使用小写的类的简单名称。(从版本4.0开始不推荐使用)
shards:索引的分片数。
replicas:索引的副本数。
refreshIntervall:索引的刷新间隔。用于索引创建。默认值为“ 1s”。
indexStoreType:索引的索引存储类型。用于索引创建。默认值为“ fs”。
createIndex:配置是否在存储库引导中创建索引。默认值为true。
versionType:版本管理的配置。默认值为EXTERNAL。
@Id:在字段级别应用,以标记用于标识目的的字段。
@Transient:默认情况下,存储或检索文档时,所有字段都映射到文档,此注释不包括该字段。
@PersistenceConstructor:标记从数据库实例化对象时要使用的给定构造函数,甚至是受保护的程序包。构造函数参数按名称映射到检索到的Document中的键值。
@Field:在字段级别应用并定义字段的属性,大多数属性映射到各自的Elasticsearch映射定义(以下列表不完整,请查看注释Javadoc以获取完整的参考):
name:字段名称,将在Elasticsearch文档中表示,如果未设置,则使用Java字段名称。
type:字段类型,可以是文本,关键字,长整数,短整数,字节,双精度,浮点型,半浮点数,标度浮点数,日期,日期_纳米,布尔值,二进制,整数,整数范围,浮点数范围,长范围,双精度范围,日期范围,对象,嵌套,Ip,TokenCount,过滤器,展平,Search_As_You_Type。请参阅Elasticsearch映射类型
format和日期类型的pattern定义。必须为日期类型定义。format
store:标记是否将原始字段值存储在Elasticsearch中,默认值为false。
analyzer,searchAnalyzer,normalizer用于指定自定义自定义分析和正规化。
@GeoPoint:将字段标记为geo_point数据类型。如果字段是GeoPoint类的实例,则可以省略。
@TypeAlias("human") :别名
对象属性,集合可以自动转换成json
3.2 也可以自定义一些通用的对象模型字段映射关系
对于写和读的转换
@WritingConverter
static class AddressToMap implements Converter<Address, Map<String, Object>> {
@Override
public Map<String, Object> convert(Address source) {
LinkedHashMap<String, Object> target = new LinkedHashMap<>();
target.put("ciudad", source.getCity());
// ...
return target;
}
}
@ReadingConverter
static class MapToAddress implements Converter<Map<String, Object>, Address> {
@Override
public Address convert(Map<String, Object> source) {
// ...
return address;
}
}
01-springboot整合elasticsearch初识的更多相关文章
- SpringBoot整合ElasticSearch实现多版本的兼容
前言 在上一篇学习SpringBoot中,整合了Mybatis.Druid和PageHelper并实现了多数据源的操作.本篇主要是介绍和使用目前最火的搜索引擎ElastiSearch,并和Spring ...
- ElasticSearch(2)---SpringBoot整合ElasticSearch
SpringBoot整合ElasticSearch 一.基于spring-boot-starter-data-elasticsearch整合 开发环境:springboot版本:2.0.1,elast ...
- springboot整合elasticsearch入门例子
springboot整合elasticsearch入门例子 https://blog.csdn.net/tianyaleixiaowu/article/details/72833940 Elastic ...
- Springboot整合elasticsearch以及接口开发
Springboot整合elasticsearch以及接口开发 搭建elasticsearch集群 搭建过程略(我这里用的是elasticsearch5.5.2版本) 写入测试数据 新建索引book( ...
- SpringBoot整合Elasticsearch详细步骤以及代码示例(附源码)
准备工作 环境准备 JAVA版本 java version "1.8.0_121" Java(TM) SE Runtime Environment (build 1.8.0_121 ...
- Springboot整合Elasticsearch报错availableProcessors is already set to [4], rejecting [4]
Springboot整合Elasticsearch报错 今天使用SpringBoot整合Elasticsearch时候,相关的配置完成后,启动项目就报错了. nested exception is j ...
- Springboot整合ElasticSearch进行简单的测试及用Kibana进行查看
一.前言 搜索引擎还是在电商项目.百度.还有技术博客中广泛应用,使用最多的还是ElasticSearch,Solr在大数据量下检索性能不如ElasticSearch.今天和大家一起搭建一下,小编是看完 ...
- 😊SpringBoot 整合 Elasticsearch (超详细).md
SpringBoot 整合 Elasticsearch (超详细) 注意: 1.环境搭建 安装es Elasticsearch 6.4.3 下载链接 为了方便,环境使用Windows 配置 解压后配置 ...
- SpringBoot整合elasticsearch
在这一篇文章开始之前,你需要先安装一个ElasticSearch,如果你是mac或者linux可以参考https://www.jianshu.com/p/e47b451375ea,如果是windows ...
- springboot整合elasticsearch(基于es7.2和官方high level client)
前言 最近写的一个个人项目(传送门:全终端云书签)中需要用到全文检索功能,目前 mysql,es 都可以做全文检索,mysql 胜在配置方便很快就能搞定上线(参考这里),不考虑上手难度,es 在全文检 ...
随机推荐
- Node.js环境安装
为其他使用先小小的接触这个环境,如不出意外,未来的一些时候抽时间会系统的学习element-ui, JavaScript, vue, node.js, 稍后也做个简易的ACE Editor体验一下 1 ...
- Python按顺序读取文件夹中文件
参考资料: https://blog.csdn.net/qq_22227123/article/details/79903116 https://blog.csdn.net/merdy_xi/arti ...
- Java 从入门到进阶之路(二十三)
在之前的文章我们介绍了一下 Java 中的 集合框架中的Collection 的迭代器 Iterator,本章我们来看一下 Java 集合框架中的Collection 的泛型. 在讲泛型之前我们先来 ...
- PIP设置镜像源
PIP设置镜像源 pip安装Python包时候,默认是国外的下载源,速度太慢,本文介绍几种设置pip国内镜像源的方法 镜像源 阿里云 http://mirrors.aliyun.com/pypi/si ...
- rust 神奇的特质
pub trait Summary { fn summarize_author(&self) -> String; fn summarize(&self) -> Strin ...
- cb31a_c++_STL_算法_查找算法_(4)find_first_of
cb31a_c++_STL_算法_查找算法_(4)find_first_offind_first_of(b,e,sb,se),sb,second begin, se,second end();find ...
- 套接字TCP控制台服务器程序代码示范
套接字TCP控制台服务器程序代码示范 https://blog.csdn.net/txwtech/article/details/90344081
- 手把手教你利用Docker+jenkins部署你的网站
更新服务器的安装源为阿里的源,参考链接:https://blog.csdn.net/js_xh/article/details/79166655 安装docker; 1 更新资源 sudo apt-g ...
- RocketMQ 内存优化
rocketmq官方文档 RocketMQ 的默认内存占用非常高,调整RocketMQ的内存目前我所知道的有两个方面: MQ的启动脚本可以调整内存mqbroker和mqnamesrv的配置可以调整内存 ...
- Tensorflow 中(批量)读取数据的案列分析及TFRecord文件的打包与读取
内容概要: 单一数据读取方式: 第一种:slice_input_producer() # 返回值可以直接通过 Session.run([images, labels])查看,且第一个参数必须放在列表中 ...