springboot 第章 springboot 与索引
Elasticsearch
elasticserch 可以快速的储存,搜索分析海量数据,Elasticsearch 是一个分布式搜索服务,底层也是基于Lucene ,采用多shard(分片)的方式保证数据安全,提供RestFul API 并且提供自动resharding 功能, 除此之外我还用过solr也是基于lucene ,这个时候可定要对比下
http://i.zhcy.tk/blog/elasticsearchyu-solr/
solr 的确定是他牺牲了建立索引的事件,建立索引的时间长,但是搜索的速度非常的块
https://www.cnblogs.com/xiaoqi/p/solr-vs-elasticsearch.html
我们这里讲springboot 和 elasticsearch 整合
docker 安装 elasticsearch
docker search elasticsearch
docker pull registry.docker-cn.com/library/elasticsearch
docker images
run
默认初始会使用2G 的堆内存空间我们可以使用 -e 命令限制内存空间
elastic search 默认使用的端口是9200 在分布式的情况下,各个节点使用的端口是9300 我们都需要暴漏这两个接口
docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9200:9200 -p 9300:9300 --name ES01 362c5cb1669b
-e 分配栈内存, -p 映射端口 , --name image 名字 id
docker ps -a
然后可以看到elasticsearch 已经启动
我们可以通过浏览器访问返回json 然后确定是否启动
http://192.168.24.136:9200/
我的内网ip 然后返回这样的数据
{"name" : "Xyyh80B","cluster_name" : "elasticsearch","cluster_uuid" : "e3RZPu0pS8uUlxVA6fJQFA","version" : {"number" : "5.6.11","build_hash" : "bc3eef4","build_date" : "2018-08-16T15:25:17.293Z","build_snapshot" : false,"lucene_version" : "6.6.1"},"tagline" : "You Know, for Search"
}
add
https://www.elastic.co/guide/cn/elasticsearch/guide/current/_indexing_employee_documents.html
官网说的很清楚
GET
https://www.elastic.co/guide/cn/elasticsearch/guide/current/_retrieving_a_document.html
如果未发现的话 就会返回这样的情况
{"_index": "megacorp","_type": "employee","_id": "4","found": false
}
getAll
https://www.elastic.co/guide/cn/elasticsearch/guide/current/_search_lite.html
使用查询表达式
https://www.elastic.co/guide/cn/elasticsearch/guide/current/_search_with_query_dsl.html
Springboot整合Elasticsearch
springboot 默认使用springdata 操作elasticsearch ,
elasticsearch 自动配置
springboot提供了两种方式,默认使用springdata ,还整合了最流行的jest 客户端通过http方式操作elasticsearch
JestAutoConfiguration
默认jestAutoConfiguration 是不生效的, 如果想让生效需要导入jest 包
ElasticsearchDataAutoConfiguration
- Client节点信息clusterNodes clusterName
- elasticsearchTemplate 操作es
- 编写一个ElasticsearchRepository的子接口来操作ES
1.创建entity
package com.zzq.springboot03elasticsearch;import io.searchbox.annotations.JestId;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;public class Article {@JestIdprivate Integer id ;private String title ;private String author ;private String content ;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 getContent() {return content;}public void setContent(String content) {this.content = content;}public Article(Integer id, String title, String author, String content) {this.id = id;this.title = title;this.author = author;this.content = content;}
}
这里注意一定要标识主键,如果不标识主键他就会自动生成主键,
然后编写逻辑代码创建索引
@Testpublic void contextLoads() throws IOException {//给es 中保存一个文档Article article = new Article(1, "好消息", "zhnagsan", "Hello Word");Index build = new Index.Builder(article).index("atguigu").type("news").build();jestClient.execute(build);}
注意一定要注入jestClient 客户端对象,然后执行execute方法
然后就ok了,
使用全文检索查询
@Testpublic void search() throws IOException {String json = "{\n" +" \"query\" : {\n" +" \"match\" : {\n" +" \"content\" : \"hello\"\n" +" }\n" +" }\n" +"}" ;Search search = new Search.Builder(json).addIndex("atguigu").addType("news").build();SearchResult res = jestClient.execute(search);System.out.println(res.getJsonString());}
更多的查询方式官网上都是有的 ,玩一个高亮搜索把
@Testpublic void search() throws IOException {String json = "{\n" +" \"query\" : {\n" +" \"match_phrase\" : {\n" +" \"content\" : \"hello\"\n" +" }\n" +" },\n" +" \"highlight\": {\n" +" \"fields\" : {\n" +" \"about\" : {}\n" +" }\n" +" }\n" +"}" ;Search search = new Search.Builder(json).addIndex("atguigu").addType("news").build();SearchResult res = jestClient.execute(search);System.out.println(res.getJsonString());}
SpringdataElasticSearch 操作ElasticSearch
使用springdata 操作 Elastic Search 不是使用9200 通过http通讯的,springdata 操作elastic search 使用9300
springboot 第章 springboot 与索引相关推荐
- 第14章 SpringBoot静态资源处理
第14章 SpringBoot静态资源处理 14.1 WebMvcAutoConfiguration的默认配置 14.2 自定义静态资源映射 14.3 前端资源的引用方法
- SpringBoot | 第一章:第一个SpringBoot应用
2019独角兽企业重金招聘Python工程师标准>>> SpringBoot | 第一章:第一个SpringBoot应用 springboot简单介绍 概述 随着动态语言的流行(Ru ...
- 第15章 SpringBoot集成logging日志
第15章 SpringBoot集成logging日志 15.1 SLF4J与Logback简介 15.2 spring-boot-starter-logging 15.3 logback-spring ...
- 第10章 springboot是什么
第II部分 Kotlin集成 SpringBoot 项目开发实战 第10章 springboot是什么 10.1 简介 10.2 自动配置原理 10.3 常用starter 10.4 actuator
- 第01章 Spring-Boot 应用文件application配置
第01章 Spring-Boot 应用文件application配置 文章目录 第01章 Spring-Boot 应用文件application配置 前言 目标 环境 随机值配置 属性占位符 应用配置 ...
- SpringBoot SpringBoot 基础篇(第一篇) 第2章 SpringBoot 全局配置 2.2 yaml 文件
SpringBoot [千锋教育java教程SpringBoot2全套,springboot快速入门到项目实战视频教程] SpringBoot 基础篇(第一篇) 第2章 SpringBoot 全局配置 ...
- 超详细的springBoot学习教程,springboot学习看这篇就够了
springBoot学习 https://docs.spring.io/spring-boot/docs/2.2.6.RELEASE/reference/html/index.html (官方文档) ...
- apache 配置文件内使用 8080 端口_【SpringBoot 框架】- SpringBoot 配置文件
一.SpringBoot配置文件类型 SpringBoot是基于约定的,所以很多配置都有默认值,但如果想使用自己的配置替换默认配置的话 ,就可以自己编写配置文件进行相应配置,起步依赖spring-bo ...
- 【数据库】第三章 事务、索引和SQL优化
[数据库]第三章 事务.索引和SQL优化 文章目录 [数据库]第三章 事务.索引和SQL优化 一.事务 1.原子性 2.持久性 3.隔离性 4.一致性 二.索引 1.介绍 2.分类 3.底层实现 4. ...
最新文章
- 股市币市:数据分析与交易所最新公告(20190302)
- 推荐几个开源类库,超好用,远离996!
- UML基础教程(内部使用教程) 非常不错的ppt!!强烈推荐
- java并发框架支持锁包括,tip/面试题_并发与多线程.md at master · 171437912/tip · GitHub...
- 12v60ah锂电池组装图_锂电池基本参数,结合电动自行车电池应用分析
- 信息学奥赛一本通(C++)在线评测系统——基础(一)C++语言——1095:数1的个数
- oracle 12c 新特性之不可见字段
- 数据结构与算法 Python语言描述 笔记
- 运维经验分享(三)-- 解决Ubuntu下crontab不能正确执行脚本的问题
- 利用JAVA获取远程文件及使用断点续传 供学习者使用
- Go、Java 和 Rust 的比较
- 【品味人生】毕业十年有感,给年轻人的一点忠告
- REST和RESTful有什么区别
- 并发机制:CSP vs Actor模型以及Golang实现
- Django form模块使用心得
- iOS-Core-Animation-Advanced-Techniques(二)
- 令牌桶算法和漏桶算法python_如何实现漏桶算法与令牌桶算法
- Python三维绘图--Matplotlib
- Template.js
- 日记侠:知识付费创富没赶上,看别人赚钱你急不急?